LocationManager don't get the right results

后端 未结 2 1416
醉梦人生
醉梦人生 2020-12-22 08:28

I am using LocationManager to convert from lat,longi to get the city name, but I am getting wrong results, I think there is something wrong in this code here:



        
相关标签:
2条回答
  • 2020-12-22 08:45

    Here is the full code of getting location. This code will automatically give you the best provider available(GPS or Network). Moreover, it won't give you the last known location but the current location.

    protected String currentLatitude, currentLongitude;
    double lat, lon;
    protected LocationManager locationManager;
    private LocationListener ll;
    locationManager = (LocationManager) getSystemService(
                Context.LOCATION_SERVICE);
        if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
    
                Toast.makeText(this,
                        "GPS enabled. Finding fine location. ",
                        Toast.LENGTH_SHORT).show();
            ll = new GpsListener();
            locationManager.requestLocationUpdates(
                    LocationManager.GPS_PROVIDER, 0, 0, ll);
    
        } else {
            if (locationManager
                    .isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
    
                    Toast.makeText(this,
                            "GPS disabled. Finding coarse location.",
                            Toast.LENGTH_SHORT).show();
                ll = new GpsListener();
                locationManager.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER, 0, 0, ll);
    
            } else {
                Toast.makeText(this,
                        "Enable location settings to get current location.",
                        Toast.LENGTH_LONG).show();
    
            }
        }
    
    private class GpsListener implements LocationListener {
    
        public void onLocationChanged(Location location) {
            // TODO Auto-generated method stub
            if (location != null) {
                try {
                    Toast.makeText(getApplicationContext(), "Location Found",
                            Toast.LENGTH_SHORT).show();
                } catch (Exception e) {
    
                }
    
                lat = location.getLatitude();
                lon = location.getLongitude();
                                Geocoder gcd = new Geocoder(context, Locale.getDefault());
                                try{
                                List<Address> addresses = gcd.getFromLocation(lat, lon, 1);
                                if (addresses.size() > 0) 
                                String cityName=addresses.get(0).getLocality();
                                }
                                 catch (IOException e) {}
                                 catch (NullPointerException e) {}
                currentLatitude = Double.toString(lat);
                currentLongitude = Double.toString(lon);
                try {
                    if (ll != null)
                        locationManager.removeUpdates(ll);
                } catch (Exception e) {
    
                }
    
                locationManager = null;
    
            } else {
                Toast.makeText(getApplicationContext(), "No Location Found",
                        Toast.LENGTH_LONG).show();
    
            }
    
        }
    
        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub
    
        }
    
        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub
    
        }
    
        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub
    
        }
    
    }// end of Class
    

    Edit- Be sure to have the following permissions in your Android Manifest:

    android.permission.INTERNET
    
    android.permission.ACCESS_NETWORK_STATE
    
    android.permission.ACCESS_COARSE_LOCATION
    
    android.permission.ACCESS_FINE_LOCATION
    
    0 讨论(0)
  • 2020-12-22 08:53

    Did you try turning on your gps to get a location fix? According to the docs, this method returns:

    the last known location for the provider, or null

    Make sure you had a location fix on your phone before for this to work. Also, you might want to consider this post which covers a good strategy to get the user location when there is one available or request a new one otherwise.

    Hope it helps.

    0 讨论(0)
提交回复
热议问题