Java 如何在 Android 中使用 Wifi 获取位置?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18826718/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get Location with Wifi in Android?
提问by sr.farzad
I Want get location with Wifi and work in Google map, and it's not work for me but Gps is okay and not problem.
我想通过 Wifi 获取位置并在 Google 地图上工作,这对我不起作用,但 Gps 还可以,没有问题。
my code:
我的代码:
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:
安卓清单:
<uses-library android:name="com.google.android.maps" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyB4C_zHeHYMTWqW4_Wpin-5jLF9S54KDSQ" />
</application>
<permission
android:name="com.karyan.karyanmap.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.karyan.karyanmap.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />
<uses-feature android:name="android.hardware.wifi" android:required="true"/>
<uses-feature
android:name="android.hardware.camera"
android:required="false" >
</uses-feature>
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
Newtork_Provider is null when wifi in connected to Intenet.. How to Resolve Problem
当wifi连接到互联网时Newtork_Provider为空..如何解决问题
thanks..
谢谢..
回答by Piyush
If you are connected to WIFI then simply use NETWORK PROVIDER for your location updates. they will be fast and enough accurate too.
如果您已连接到 WIFI,则只需使用 NETWORK PROVIDER 即可进行位置更新。它们也将很快且足够准确。
generally if location updates are not required so frequently then location updates are asked from both GPS and NETWORK together at the same time. whenever you get a location update of desired accuracy unregister from listening location updates.
通常,如果不需要如此频繁的位置更新,则同时从 GPS 和 NETWORK 请求位置更新。每当您获得所需精度的位置更新时,就从收听位置更新中取消注册。
But if location updates are required frequently then calling GPS can be a KILLER OF BATTERY too so be careful of using GPS PROVIDER.
但是如果经常需要位置更新,那么调用 GPS 也可能是电池的杀手,所以使用 GPS 提供商时要小心。
GPS Updates are available good under open sky only. GPS Updates takes time , takes battery but Are More Accurate.
GPS 更新仅在开阔的天空下可用。GPS 更新需要时间,需要电池,但更准确。
Network Updates are quicker, consumes less battery but are comparatively less accurate. But if we are talking about a WIFI accuracy it will be near to 50 or 100 that can serve many real time requirement.
网络更新更快,消耗更少的电池,但相对来说不太准确。但如果我们谈论 WIFI 精度,它将接近 50 或 100,可以满足许多实时要求。
It all depends on your requirement.
这一切都取决于您的要求。
回答by Ifgeny87
Say, can I setup "request Location Update" for GPS_PROVIDER and NETWORK_PROVIDER (or PASSIVE_PROVIDER) to same function call like this:
说,我可以将 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);
In this case, I get only GPS data.
在这种情况下,我只得到 GPS 数据。