简体   繁体   中英

Get GPS location precisely

`private void getLocation() {

        String context = Context.LOCATION_SERVICE;
        LocationManager locationManager = (LocationManager)getSystemService(context);
        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_COARSE);
        criteria.setAltitudeRequired(false);
        criteria.setBearingRequired(false);
        criteria.setCostAllowed(true);
        criteria.setPowerRequirement(Criteria.POWER_LOW);

        String provider = locationManager.getBestProvider(criteria, true);
        Location location = locationManager.getLastKnownLocation(provider);
        updateWithNewLocation(location);

        locationManager.requestLocationUpdates(provider, 2000, 5, locationListener);

    }

    private final LocationListener locationListener=new LocationListener()
    {
        public void onLocationChanged(Location location)
        {
            updateWithNewLocation(location);
        }
        public void onProviderDisabled(String provider)
        {
            updateWithNewLocation(null);
        }
        public void onProviderEnabled(String provider)
        {

        }
        public void onStatusChanged(String provider,int status,Bundle extras)
        {

        }
    }`  

My code for getting location above. But the result of my application is not accurate. I want to get precisely the my location. Is there any other way?

Getting accurate location fix depends on if the signals are available. For eg. if gps signal is present there or not. If GPS is not present phone uses wifi or service provider tracking which is less accurate. So try using google maps and see your location, if you are getting an more accurate location fix using Google maps then you are missing something in your code.

One error I can see is you are using ACCURACY_COARSE, try using ACCURACY_FINE. Also you will have to add those permissions in your manifest if you haven't already.

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