简体   繁体   中英

How to get Location with Wifi in Android?

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.

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

thanks..

Say, can I setup "request Location Update" for GPS_PROVIDER and NETWORK_PROVIDER (or PASSIVE_PROVIDER) to same function call like this:

// 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.

If you are connected to WIFI then simply use NETWORK PROVIDER for your location updates. they will be fast and enough accurate too.

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.

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 Updates are available good under open sky only. GPS Updates takes time , takes battery but Are More Accurate.

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.

It all depends on your requirement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM