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

发布于 2024-12-09 00:06:36 字数 482 浏览 2 评论 0原文

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

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),该代码将产生当前位置。这个假设正确吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝颜夕 2024-12-16 00:06:36

我正在使用以下代码来确定当前位置及其工作情况。查看dis代码...

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

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;
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文