[英]android Location Listener
i want to listen to both GPS and NETWORK location provider from the same listener and implementation 我想从同一个监听器和实现中同时收听GPS和NETWORK位置提供程序
is this ok for doing that: 这样做可以:
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,metersToUpdate,this);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,metersToUpdate,this);
will it user the same methods for both providers ? 它会为两个提供商使用相同的方法吗?
You can also request location updates from both the GPS and the Network Location Provider by calling requestLocationUpdates() twice—once for NETWORK_PROVIDER and once for GPS_PROVIDER.
您还可以通过两次调用requestLocationUpdates()来为GPS和网络位置提供商请求位置更新,一次为NETWORK_PROVIDER,一次为GPS_PROVIDER。
It's simple as 1.2.3 look at my example... 这很简单,因为1.2.3看看我的例子......
try {
Criteria criteria = new Criteria();
mLocationManagerHelper.SetLocationManager((LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE));
mLocationManagerHelper.GetLocationManager().requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 500.0f, mLocationManagerHelper.GetLocationListener());
String provider = mLocationManagerHelper.GetLocationManager().getBestProvider(criteria, false);
Location location = mLocationManagerHelper.GetLocationManager().getLastKnownLocation(provider);
if (location != null) {
mLongitude = location.getLongitude();
mLatitude = location.getLatitude();
}
} catch (Exception ex) {
Log.e(TAG, "GPS", ex);
}
Location helper 位置助手
public class LocationManagerHelper {
private static final String TAG = LocationManagerHelper.class.getSimpleName();
private Context mContext;
private LocationManager mLocationManager;
private GeoUpdateHandler mLocationListener = new GeoUpdateHandler();
public LocationManagerHelper(Context context) {
this.mContext = context;
}
public GeoUpdateHandler GetLocationListener() {
return mLocationListener;
}
public void SetLocationManager(LocationManager locationManager) {
mLocationManager = locationManager;
}
public LocationManager GetLocationManager() {
return mLocationManager;
}
public void Stop() {
if (mLocationManager != null) {
mLocationManager.removeUpdates(mLocationListener);
}
}
private class GeoUpdateHandler implements LocationListener {
@Override
public void onLocationChanged(Location loc) {
String longitude = "Longitude: " + loc.getLongitude();
Log.v(TAG, longitude);
String latitude = "Latitude: " + loc.getLatitude();
Log.v(TAG, latitude);
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
}
} }
Google provides a better API, using fused location API: Google使用融合位置API提供了更好的API:
https://developers.google.com/location-context/fused-location-provider/ https://developers.google.com/location-context/fused-location-provider/
https://developers.google.com/android/reference/com/google/android/gms/location/FusedLocationProviderClient https://developers.google.com/android/reference/com/google/android/gms/location/FusedLocationProviderClient
https://developer.android.com/training/location/receive-location-updates https://developer.android.com/training/location/receive-location-updates
https://github.com/sakurabird/Android-Fused-location-provider-example https://github.com/sakurabird/Android-Fused-location-provider-example
https://youtu.be/eHjHlujp3Tg?list=WL&t=2129 https://youtu.be/eHjHlujp3Tg?list=WL&t=2129
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.