我正在我的应用程序中实现类似导航的功能。在这里,我必须知道用户是否达到了特定的纬度/经度(比如在 XX 米之前)。我并不完全依赖 Location API 中可用的 distanceTo 方法。我必须知道移动的方向。
那么,我该如何实现呢?请帮我。
注意:我使用的是谷歌地图 v2 android。
我正在我的应用程序中实现类似导航的功能。在这里,我必须知道用户是否达到了特定的纬度/经度(比如在 XX 米之前)。我并不完全依赖 Location API 中可用的 distanceTo 方法。我必须知道移动的方向。
那么,我该如何实现呢?请帮我。
注意:我使用的是谷歌地图 v2 android。
使用接近警报:
我创建了一个小服务,指定警报的半径。
public class ProximityService extends Service {
String proximitysd = "com.apps.ProximityService";
int n = 0;
private BroadcastReceiver mybroadcast;
private LocationManager locationManager;
MyLocationListener locationListenerp;
private static final String PREFERENCE_NAME = "MyPreferenceFileName";
double lat;
double lng;
private static final String POINT_LATITUDE_KEY = "POINT_LATITUDE_KEY";
private static final String POINT_LONGITUDE_KEY = "POINT_LONGITUDE_KEY";
public ProximityService() {
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
mybroadcast = new ProximityIntentReceiver();
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
SharedPreferences prefs = this.getSharedPreferences(PREFERENCE_NAME,
Context.MODE_PRIVATE);
lat = prefs.getFloat(POINT_LATITUDE_KEY, 0);
lng = prefs.getFloat(POINT_LONGITUDE_KEY, 0);
float radius = 50f;
long expiration = -1;
// lat = cursor.getInt(MyDBAdapter.LATITUDE_COLUMN)/1E6;
// lng = cursor.getInt(MyDBAdapter.LONGITUDE_COLUMN)/1E6;
// lat = 12.923574064148527;
// lng = 77.54726361483335;
String what = "What";
String how = "How";
String proximitys = "ProximityService";
IntentFilter filter = new IntentFilter(proximitys);
registerReceiver(mybroadcast, filter);
Intent intent = new Intent(proximitys);
intent.putExtra("alert", what);
intent.putExtra("type", how);
PendingIntent proximityIntent = PendingIntent.getBroadcast(this, n,
intent, PendingIntent.FLAG_CANCEL_CURRENT);
locationManager.addProximityAlert(lat, lng, radius, expiration,
proximityIntent);
// sendBroadcast(new Intent(intent));
}
@Override
public void onDestroy() {
Toast.makeText(this, "Proximity Service Stopped", Toast.LENGTH_LONG)
.show();
try {
unregisterReceiver(mybroadcast);
} catch (IllegalArgumentException e) {
Log.d("reciever", e.toString());
}
}
@Override
public void onStart(Intent intent, int startid) {
// lat = id.getInt("lat");
/*
* Toast.makeText(this, "Proximity Service Started", Toast.LENGTH_LONG)
* .show();
*/
// IntentFilter filter = new IntentFilter(proximitys);
// registerReceiver(mybroadcast,filter);
}
public class ProximityIntentReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context arg0, Intent intent) {
// String key = LocationManager.KEY_PROXIMITY_ENTERING;
final String key = LocationManager.KEY_PROXIMITY_ENTERING;
final Boolean entering = intent.getBooleanExtra(key, false);
// Boolean entering = arg1.getBooleanExtra(key, false);
String here = intent.getExtras().getString("alert");
String happy = intent.getExtras().getString("type");
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent pendingIntent = PendingIntent.getActivity(arg0, 0,
intent, 0);
Notification notification = createNotification();
if (entering) {
Toast.makeText(getApplicationContext(), "entering",
Toast.LENGTH_SHORT).show();
notification.setLatestEventInfo(arg0, "Entering Proximity!",
"", pendingIntent);
} else {
Toast.makeText(getApplicationContext(), "exiting",
Toast.LENGTH_SHORT).show();
notification.setLatestEventInfo(arg0, "Existing Proximity!",
"", pendingIntent);
}
notificationManager.notify((int) System.currentTimeMillis(),
notification);
}
private Notification createNotification() {
Notification notification = new Notification();
notification.icon = R.drawable.ic_launcher;
notification.when = System.currentTimeMillis();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.defaults |= Notification.DEFAULT_LIGHTS;
notification.ledARGB = Color.WHITE;
notification.ledOnMS = 1500;
notification.ledOffMS = 1500;
return notification;
}
// make actions
}
public class MyLocationListener implements LocationListener {
public void onLocationChanged(Location location) {
Toast.makeText(getApplicationContext(), "I was here",
Toast.LENGTH_LONG).show();
}
public void onProviderDisabled(String s) {
}
public void onProviderEnabled(String s) {
}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
}
}
}