如何获取用户的当前位置?

我想根据 Network Provider 获取用户的当前位置,但是没有成功,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);
}


慕沐林林
浏览 895回答 2
2回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java