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

前端之家收集整理的这篇文章主要介绍了如何在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,(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" />

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

谢谢..

解决方法

比方说,我可以为GPS_PROVIDER和NETWORK_PROVIDER(或PASSIVE_PROVIDER)设置“请求位置更新”到相同的函数调用,如下所示:
// SETUP Location Manager
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,this);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,this);
locationManager.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER,this);

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

猜你在找的Android相关文章