接近警报触发时如何获取当前位置

时间:2011-10-10 03:29:06

标签: android locationmanager proximity

我正在使用以下代码确定接近警报触发时的当前位置:

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("");
    }
}

当我查看我回来的位置时,我会在我不应该得到它们的位置输入警报。由于接近警报在内部轮询GPS和网络提供商 - 因此更新LastKnownLocation - 此代码将产生当前位置,因此我受到了印象。这个假设是否正确?

1 个答案:

答案 0 :(得分:1)

我使用以下代码来确定当前位置及其工作原理。查看解码...

        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;
        }