我正在尝试基于网络提供程序获取用户位置,但事件永远不会发生,并且永远不会调用UpdateLocation()
方法。
这是代码:
public void UpdateLocation(Location location)
{
GeoPoint geoPoint = new GeoPoint((int)(location.getLatitude() * 1E6),(int)(location.getLongitude() * 1E6));
MapController controller = mapView.getController();
controller.setCenter(geoPoint);
}
public void GetLocation()
{
// 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.
UpdateLocation(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);
}
可能是什么问题?我在这里做错了吗?
答案 0 :(得分:1)
如果我没记错的话,NETWORK_PROVIDER使用wifi信号来确定位置。
要验证的事情:
答案 1 :(得分:0)
在请求地点之前,您应该检查您的位置提供商是否已启用:
locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
看一下本教程。它包括sample code