【发布时间】:2013-12-26 16:47:58
【问题描述】:
我正在使用此代码来获取用户的地理位置
public Location getLocation() {
Location location = null;
double lat;
double lng;
try {
LocationManager mLocationManager = (LocationManager) con.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) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
} else {
// First get location from Network Provider
if (isNetworkEnabled) {
mLocationManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, 0, 0, this);
Log.d("Network", "Network");
if (mLocationManager != null) {
location = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
lat = location.getLatitude();
lng = location.getLongitude();
System.out.println(lat);
}
}
}
//get the location by gps
if (isGPSEnabled) {
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0, this);
Log.d("GPS Enabled", "GPS Enabled");
if (mLocationManager != null) {
location = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
lat = location.getLatitude();
lng = location.getLongitude();
System.out.println(lat);
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
这是我得到的:
网络 39.68967 Gps 已启用 (无)
我无法理解启用 Gps 的原因,我仍然无法从 gps 提供商处获取坐标
【问题讨论】:
-
因为你没有缓存的最后一个位置,如果你启动谷歌地图或使用你的位置的东西你可能会得到一个然后你可以再次运行你的应用程序
-
我做了..同样的事情发生了
-
无论如何你都应该使用新的位置 API developer.android.com/google/play-services/location.html
-
哦,我不知道。有安卓版本限制吗?
-
如果您使用 google play 服务进行 froyo,则您拥有 2.2 及更高版本,否则为 2.3 及更高版本
标签: android geolocation gps android-location