Android:如果手机通过 WiFi 连接,则无法从网络获取地理位置
我正在尝试从这样的网络获取地理位置:
boolean network_enabled = false;
LocationManager lm;
lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
try{
network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
} catch(Exception ex){
Log.v("my_log_tag", "network location exception: " + ex.toString());
}
if(network_enabled){
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListenerNetwork);
}
如果我的手机仅通过手机网络连接,那么效果很好,但我有一个问题:如果我的手机不仅通过手机网络连接,而且还连接到某些 WiFi 接入点(比如说我的家庭接入点),然后我就没有收到任何位置信息。 Android 似乎尝试使用我的 WiFi 接入点获取地理定位,但当然不能。我怎样才能让Android从手机网络获取位置并忽略任何WiFi连接?
我绝对知道这是可能的,因为,例如,Yandex 地图应用程序 就可以很好地做到这一点。
我需要多说一个细节:这个 Yandex 地图应用程序可以通过网络接收地理位置,即使我没有启用复选框设置 -> 位置和安全 -> 使用无线网络。我不知道它是如何工作的,但也许它使用完全不同的方法。
事实是:如果我启用了 GPS,它就会使用 GPS。如果我禁用了 GPS,但启用了蜂窝网络连接,则它会使用网络进行地理定位,而与 WiFi 连接和复选框“使用无线网络”无关。
我怎样才能做同样的事情?
谢谢。
I'm trying to get geolocation from network like that:
boolean network_enabled = false;
LocationManager lm;
lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
try{
network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
} catch(Exception ex){
Log.v("my_log_tag", "network location exception: " + ex.toString());
}
if(network_enabled){
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListenerNetwork);
}
It works nice if my phone is connected only via cell network, but i have a problem: if my phone is connected not only via cell network, but to some WiFi access point too (say, my home access point), then i receive no location. It seems like Android tries to get geolocation using my WiFi access point, but of course it can't. How can i make Android get location from cell network and ignore any WiFi connection?
I definitely know it is possible, because, for example, Yandex Maps application does it fine.
I need to say one more detail: this Yandex Maps application can receive geolocation via network even if i have NOT enabled checkbox Settings -> Location and security -> Use wireless networks. I have no idea how does it work, but maybe it uses completely different approach.
The fact is: if i have GPS enabled, it uses GPS. If i have GPS disabled, but cell network connection is enabled, then it uses network for geolocation, independently of WiFi connection and checkbox "Use wireless networks".
How can i do the same?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在我检索地理位置方面所做的工作中,wifi 接入点只会定期发送其位置(30 秒到 5 分钟)。但是,请了解
NETWORK_PROVIDER
也从手机信号塔接收信号(速度慢且不准确,但在建筑物中信号更好)。这就是我如何使用手机的 GPS 找到最快的地理定位结果,但是使用 NETWORK_PROVIDER 可以工作,它真的很慢,而且我无法找到一个好的解决方案来强制位置更新得更快。
Out of the work I've done with retrieving a geolocation, wifi access points only will send their location periodically (30 seconds to 5 minutes). But, understand that
NETWORK_PROVIDER
recieves from cell towers as well (which is slow and inaccurate, but has better signal in buildings).that is how I have found the fastest results for geolocation using the phone's gps, however using
NETWORK_PROVIDER
works, it is really slow and I have not been able to find a good solution to force the location to update faster.