如何在没有侦听器或 requestLocationUpdates 的情况下获取任何 GPS 坐标

发布于 2024-12-10 05:55:27 字数 864 浏览 1 评论 0原文

我只需要来自任何提供商的最后已知 GPS 位置
无需通知任何提供商进行更新。

在不添加侦听器或 requestLocationUpdates 的情况下是否可以实现这一点?

问题是我只需要一个大概的位置而无需阻塞延迟

我从 Activity onCreate 尝试此代码,但 providers.size() 始终是零

LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);  
List<String> providers = lm.getProviders(true);

    Location l = null;

    for (int i=providers.size()-1; i>=0; i--) {
       l = lm.getLastKnownLocation(providers.get(i));
       if (l != null) 
        break;
    }

 <uses-permission android:name="android.permission.ACCESS_GPS" /> 
    <uses-permission android:name="android.permisssion.ACCESS_FINE_LOCATION"></uses-permission>
    <uses-permission android:name="android.permisssion.ACCESS_COARSE_LOCATION"></uses-permission> 

I only need the last known gps location from any provider
without telling any provider to update.

Is this possible without adding listeners or requestLocationUpdates?

The thing is i need only an approximate position without blocking delay

Im trying this code from an Activity onCreate but the providers.size() is always zero

LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);  
List<String> providers = lm.getProviders(true);

    Location l = null;

    for (int i=providers.size()-1; i>=0; i--) {
       l = lm.getLastKnownLocation(providers.get(i));
       if (l != null) 
        break;
    }

 <uses-permission android:name="android.permission.ACCESS_GPS" /> 
    <uses-permission android:name="android.permisssion.ACCESS_FINE_LOCATION"></uses-permission>
    <uses-permission android:name="android.permisssion.ACCESS_COARSE_LOCATION"></uses-permission> 

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

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

发布评论

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

评论(2

萌化 2024-12-17 05:55:27

尽管您可以从位置管理器获取最后一个已知位置,但它可能已经过时,因此最好获取所有提供商列表并检查最新位置,以便更准确

LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        List<String> providers = manager.getAllProviders();
        Location loc;

        for (int i = 0; i < providers.size(); i++) {

            loc = manager.getLastKnownLocation(providers.get(i));
            loc.getTime();
            /*
             * put your code here
             * compare loc from providers to get the most 
             * recent location
             */
        }
}

Even though you can get the last known location from location manager its may be outdated so its better to get all providers list and check the most current location so that it can be more accurate

LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        List<String> providers = manager.getAllProviders();
        Location loc;

        for (int i = 0; i < providers.size(); i++) {

            loc = manager.getLastKnownLocation(providers.get(i));
            loc.getTime();
            /*
             * put your code here
             * compare loc from providers to get the most 
             * recent location
             */
        }
}
脸赞 2024-12-17 05:55:27

尝试代码片段。

Try this code snippet.

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