如何在Android中使用Wifi获取位置?

我希望通过Wifi获取位置并在谷歌地图中工作,这对我不起作用,但Gps是好的而不是问题。

我的代码:

locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); if (locationManager != null) { boolean gpsIsEnabled = locationManager .isProviderEnabled(LocationManager.GPS_PROVIDER); boolean networkIsEnabled = locationManager .isProviderEnabled(LocationManager.NETWORK_PROVIDER); if (gpsIsEnabled) { locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 5000L, 10F, (android.location.LocationListener) this); } else if (networkIsEnabled) { locationManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, 5000L, 10F, (android.location.LocationListener) this); } else { // Show an error dialog that GPS is disabled... } } else { // Show some generic error dialog because something must have gone // wrong with location manager. } 

AndroidManifest:

                      

当wifi连接到Intenet时,Newtork_Provider为null。如何解决问题

谢谢..

如果您已连接到WIFI,则只需使用NETWORK PROVIDER进行位置更新。 他们会很快,也足够准确。

通常,如果不经常需要位置更新,则同时从GPS和NETWORK一起询问位置更新。 每当您获得所需精度的位置更新时,请从侦听位置更新中取消注册。

但如果经常需要更新位置,那么调用GPS也可能是杀手电池,所以要小心使用GPS PROVIDER。

GPS更新仅在开放天空下可用。 GPS更新需要时间,需要电池但更准确。

网络更新速度更快,耗电量更少但准确度相对较低。 但是,如果我们谈论的是WIFI准确度,它将接近50或100,可以满足许多实时要求。

这一切都取决于您的要求。

比方说,我可以为GPS_PROVIDER和NETWORK_PROVIDER(或PASSIVE_PROVIDER)设置“请求位置更新”到相同的函数调用,如下所示:

 // SETUP Location Manager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this); locationManager.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER, 0, 0, this); 

在这种情况下,我只获得GPS数据。