Android:如何检查我是否到达/接近我的目的地?
我正在开发一个应用程序,当您到达/接近目的地时,它会唤醒您,并向您发送类似警报的通知 我是否可以持续检查用户的当前位置以及用户何时接近/到达目的地 如果可能的话,请给我一些示例/教程,这样我就可以很容易地实现它 到目前为止,我实现了下面的代码,表示“通知”Android:如何检查我是否到达/接近我的目的地?,android,google-maps-android-api-2,locationmanager,Android,Google Maps Android Api 2,Locationmanager,我正在开发一个应用程序,当您到达/接近目的地时,它会唤醒您,并向您发送类似警报的通知 我是否可以持续检查用户的当前位置以及用户何时接近/到达目的地 如果可能的话,请给我一些示例/教程,这样我就可以很容易地实现它 到目前为止,我实现了下面的代码,表示“通知” 您需要实现LocationListener来获得位置更新,但请注意,它可能会疯狂地耗尽设备的电池电量。获得当前位置后,可以使用distanceTo()方法,使用获得的位置实例来计算与目标的距离,前提是您具有目标lat long值。现在,您可以
您需要实现
LocationListener
来获得位置更新,但请注意,它可能会疯狂地耗尽设备的电池电量。获得当前位置后,可以使用distanceTo()
方法,使用获得的位置实例来计算与目标的距离,前提是您具有目标lat long值。现在,您可以检查distanceTo()
方法返回的值是否在靠近目标的可接受范围内,例如。。我到达目的地的可接受范围是,如果当前位置和目的地之间的间隔在40米以内。您需要实现LocationListener
以获得位置更新,但请注意,它可能会疯狂地耗尽设备的电池。获得当前位置后,可以使用distanceTo()
方法,使用获得的位置实例来计算与目标的距离,前提是您具有目标lat long值。现在,您可以检查distanceTo()
方法返回的值是否在靠近目标的可接受范围内,例如。。我到达目的地的可接受范围是,如果当前位置和目的地之间的距离在40米以内。您应该创建一个地理围栏,并将其添加到google location services,当用户进入或退出某个特定地理围栏时,它将通知您(回调)
遵循以下步骤:
您应该创建一个地理围栏,并将其添加到google location services,当用户进入或退出某个特定地理围栏时,它将通知您(回调) 遵循以下步骤:
public class ProximityReceiver extends BroadcastReceiver {
private static final int ALARM_DURATION = 6000;
public static final String INTENT_NAME = "com.xxx.xxx.intent.PROXIMITY";
private static final long VIBRATION_PATTERN[];
static {
long al[] = new long[6];
al[1] = 1000L;
al[2] = 500L;
al[3] = 1000L;
al[4] = 500L;
al[5] = 1000L;
VIBRATION_PATTERN = al;
}
@Override
public void onReceive(Context context, Intent intent) {
MyMainApplication wakeappapplication = (MyMainApplication) context
.getApplicationContext();
if (wakeappapplication.getStatus().equals(WakeAppStatus.STARTED)) {
wakeappapplication.setStatus(WakeAppStatus.APPROACHING);
long l = 6000L + System.currentTimeMillis();
((AlarmManager) context.getSystemService("alarm")).setRepeating(0,
l, 6000L, wakeappapplication.getProximityPendingIntent());
}
NotificationManager notificationmanager = (NotificationManager) context
.getSystemService("notification");
notificationmanager.cancel("ProximityReceiver", 0);
Notification notification = new Notification(
R.drawable.ic_stat_notify_wakeapp,
context.getString(R.string.text_wake_title),
System.currentTimeMillis());
PendingIntent pendingintent = PendingIntent.getActivity(context, 0,
new Intent(context, MainWakeMeUpActivity.class), 0);
notification.setLatestEventInfo(context,
context.getString(R.string.text_wake_title),
context.getString(R.string.text_wake_descr), pendingintent);
android.net.Uri uri = wakeappapplication.getChosenRingtone();
if (uri != null)
notification.sound = uri;
notification.vibrate = VIBRATION_PATTERN;
if (wakeappapplication.isRaiseVolume())
((AudioManager) context.getSystemService("audio")).adjustVolume(1,
0);
notificationmanager.notify("ProximityReceiver", 0, notification);
}}