How to get Wi-Fi location in Android?

I want to get a place with Wi-Fi and work on a Google map, and this does not work for me, but Gps is fine and not a problem.

my code is:

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 connecting Wi-Fi to Intenet .. How to solve the problem

thanks..

+4
source share
2 answers

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

in general, if location updates are not required so often, then location updates are simultaneously requested from both GPS and NETWORK together. whenever you receive a location update of the desired accuracy, unregister from location updates.

But if location updates are often required, then a GPS call may be KILLER OF BATTERY, so be careful using GPS PROVIDER.

GPS updates are only available outdoors. GPS updates take time, take a battery, but are more accurate.

Network updates are faster, consume less battery, but relatively less accurate. But if we are talking about WIFI accuracy, it will be close to 50 or 100, which can satisfy many needs in real time.

It all depends on your requirements.

+4
source

Say, can I configure "Update request location" for GPS_PROVIDER and NETWORK_PROVIDER (or PASSIVE_PROVIDER) for the same function call as 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 only get GPS data.

+3
source

Source: https://habr.com/ru/post/1502393/


All Articles