【发布时间】:2015-05-29 12:45:10
【问题描述】:
一年前,我搜索了如何让设备经久耐用。我在这里的一篇文章中发现,正确的方法是让我的活动实现LocationListener,并且我已经使用提供的代码来实现它并且效果很好。
public Location getLocation() {
LocationManager mLocationManager;
Location mLocation = null;
try {
mLocationManager = (LocationManager) this.getSystemService(LOCATION_SERVICE);
// getting GPS status
boolean isGPSEnabled = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
boolean isNetworkEnabled = mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
enableLocationProvider();
} else {
// First get location from Network Provider
if (isNetworkEnabled) {
mLocationManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, 20000, 1, this);
Log.d("Network", "Network");
if (mLocationManager != null) {
mLocation = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (mLocation != null) {
double lat = mLocation.getLatitude();
double lng = mLocation.getLongitude();
}
}
}
//get the location by gps
if (isGPSEnabled) {
if (mLocation == null) {
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,20000,1, this);
Log.d("GPS Enabled", "GPS Enabled");
if (mLocationManager != null) {mLocation = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (mLocation != null) {
double lat = mLocation.getLatitude();
double lng = mLocation.getLongitude();
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return mLocation;
}
我现在再次尝试执行此操作,并使用了一段时间未使用的手机,我可以获取位置。顺便说一下我打开的时候
谷歌地图
几秒钟后,应用程序找到了我的坐标。我确定我做错了什么。我怎样才能像谷歌地图一样高效地定位设备的坐标?
【问题讨论】:
-
我无法得到您真正想要的,但是..FusedLocationAPI 可能是最好的解决方案。
标签: android geolocation location coordinates