Get user current position with gps/network android

2019-06-10 10:52发布

问题:

Hello I've been using this guide to determine the locationof the user: http://developer.android.com/guide/topics/location/obtaining-user-location.html

and my current code looks like this:

    LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

    LocationListener locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
            test = new GeoPoint((int)location.getLatitude()*1000000, (int)location.getAltitude()*1000000);
            mapController.animateTo(test);
        }

        @Override
        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub

        }
        @Override
        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub

        }
        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub

        }
    };
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
}

I only get error and the app shuts down though when I'm using this activity.

Thankful for help

回答1:

PLease confirm that you have alloted the following permission to your manifest file

ACCESS_COARSE_LOCATION ACCESS_FINE_LOCATION



回答2:

I guess you are executing this code in the device

try this on device

Setting -> Location -> Use GPS

AND

Setting -> Application Settings -> Development -> Allow Mock Location



回答3:

I solved it, I didn't instantiate mapContoller :>. But I've another problem now. which I will post in another question.