有没有可能在不使用GPS或互联网的情况下获得用户的当前位置?我的意思是在移动网络提供商的帮助下。
发布于 2011-07-14 22:55:45
你要做的是使用LocationManager.NETWORK_PROVIDER
而不是LocationManager.GPS_PROVIDER
来获得这个职位。NETWORK_PROVIDER
将在GSM或wifi上解析。显然,在关闭wifi的情况下,将使用GSM。请记住,使用蜂窝网络基本上可以精确到500米。
http://developer.android.com/guide/topics/location/obtaining-user-location.html有一些非常棒的信息和示例代码。
在完成OnCreate()
中的大部分代码后,添加以下内容:
// Acquire a reference to the system Location Manager
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
makeUseOfNewLocation(location);
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
您还可以让您的活动实现LocationListener
类,从而在您的活动中实现onLocationChanged()
。
发布于 2016-12-14 17:45:26
boolean gps_enabled = false;
boolean network_enabled = false;
LocationManager lm = (LocationManager) mCtx
.getSystemService(Context.LOCATION_SERVICE);
gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
Location net_loc = null, gps_loc = null, finalLoc = null;
if (gps_enabled)
gps_loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (network_enabled)
net_loc = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (gps_loc != null && net_loc != null) {
//smaller the number more accurate result will
if (gps_loc.getAccuracy() > net_loc.getAccuracy())
finalLoc = net_loc;
else
finalLoc = gps_loc;
// I used this just to get an idea (if both avail, its upto you which you want to take as I've taken location with more accuracy)
} else {
if (gps_loc != null) {
finalLoc = gps_loc;
} else if (net_loc != null) {
finalLoc = net_loc;
}
}
发布于 2016-04-16 14:54:57
在这里有可能获得用户的当前位置,而不使用GPS和网络提供商。
1.Convert cellLocation to real location (Latitude and Longitude), using "http://www.google.com/glm/mmap"
https://stackoverflow.com/questions/6694391
复制相似问题