我試圖根據網絡提供程序獲取用戶位置,但該事件從未發生,並且UpdateLocation()
方法從未被調用。 代碼如下:獲取用戶位置Android
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);
}
可能是什麼問題?我在這裏做錯了什麼?
你在一個設備或模擬器上使用它? – Sam
您是否在清單中添加了正確的權限?像INTERNET和ACCESS_COARSE_LOCATION一樣? –
@Sam Emulatorrr – idish