【发布时间】:2012-08-19 12:13:06
【问题描述】:
我正在尝试根据网络提供商获取用户位置,但该事件从未发生,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);
}
可能是什么问题?我在这里做错了吗?
【问题讨论】:
-
你是在设备还是模拟器上使用这个?
-
您是否在清单中添加了正确的权限?喜欢 INTERNET 和 ACCESS_COARSE_LOCATION?
标签: java android gps locationmanager android-location