MarcFasel
MarcFasel

Reputation: 1158

How to get current location when proximity alert fires

I am using the following code to determine the current location when a proximity alert fires:

Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location == null) {
    location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
    if (location == null) {
        // We'll just write an empty location
        location = new Location("");
    }
}

When I look at the locations that I get back I get entering alerts in locations where I should't get them. I was under the impression since proximity alerts internally poll the GPS and network provider - thus updating the LastKnownLocation - that this code would yield the current location. Is this assumption correct?

Upvotes: 0

Views: 517

Answers (1)

Ayush Verma
Ayush Verma

Reputation: 264

I am using the following code to determine the current location and its working too. Check out dis code...

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

        if (null != provider) 
        {
            Location location = locationManager.getLastKnownLocation(provider);
            if (null != location) {
                GpsFound=true;
                currentLat = location.getLatitude();
                currentLon = location.getLongitude();
                System.out.println("Current Lat,Long :"+currentLat+" ,"+currentLon);
            }
            else
            {
                //GpsFound=false;
                gpsMsg="Current Location can not be resolved!";
            }

        } 
        else 
        {
            gpsMsg="Provider is not available!";
            //GpsFound=false;
        }

Upvotes: 1

Related Questions