简体   繁体   中英

Can't find user location Google Maps Api v2

I need to find the Current latitude and longitude to get the address, but its not working.

here is my permisions:

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/> 

my java code:

public void GetCurrentLocal() throws IOException{

    LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    String provider = locationManager.getBestProvider(criteria, true);
    Location myLocation = locationManager.getLastKnownLocation(provider);

    double latitude = myLocation.getLatitude();
    double longitude = myLocation.getLongitude();

    Geocoder geocoder;
    List<Address> addresses;
    geocoder = new Geocoder(this, Locale.getDefault());
    addresses = geocoder.getFromLocation(latitude, longitude, 1);

    String address = addresses.get(0).getAddressLine(0);
    System.out.println(address);
}

thanks.

If it fails on myLocation.getLatitude(); as you mentioned in the comment then it means that locationManager.getLastKnownLocation(provider); doesn't return a last known location because it doesn't have one so it return null and this is the reason you have a NullPointerExeception error. you need to implement a LocationListener in this case and to run this method after you have received at least one location update.

To implement LocationListener you can check this example:

http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/

I guess that this reading material could be really handy as well for you:

http://android-developers.blogspot.de/2011/06/deep-dive-into-location.html

I have this code, for me works

private Location getMyLocation() {
    // Get location from GPS if it's available
    LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    Location myLocation = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);

    // Location wasn't found, check the next most accurate place for the
    // current location
    if (myLocation == null) {
        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_COARSE);
        // Finds a provider that matches the criteria
        String provider = lm.getBestProvider(criteria, true);
        // Use the provider to get the last known location
        myLocation = lm.getLastKnownLocation(provider);
    }
    return myLocation;
}

Location location = this.getMyLocation();               
LatLng locationGPS = new LatLng(location.getLatitude(), location.getLongitude());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM