I used LocationManager to get user address by "Longitude" and "Latitude" but when i call location.getLongitude() and location.getLatitude(), it return 0 back to me. I manage to Google but have nobody meet my issue.
Mr.Jeremy Edwards suggest me that:
Make sure that network based location is enabled in the phone's settings. You can detect this situation and prompt the user to enable it if you really need it.
I believe this is the line of code to launch this activity.
I do that but it can help. Sometime that give me Longitude and Latitude, some time not.
Here is my code:
String locationProvider = LocationManager.NETWORK_PROVIDER;
LocationManager locationManager = (LocationManager) this
.getSystemService(Context.LOCATION_SERVICE);
Location location = locationManager
.getLastKnownLocation(locationProvider);
The permission in the AndroidMainfest.xml:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
Can anyone help me? Thank you in advance!
Related
I want to track GPS status in API 23 mobile, but the above method returing false everytime.
My code:
LocationManager locationManager;
boolean isEnabled;
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
isEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
UPDATE :
I have both the permission in manifest.I have proper code to ask permmision also.
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>
# I have observed that if the location is turned on in app opeing the locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); is returning true, **but if I'm turning off location when app is opened and again turning on the location it is returning false. It is a big headache to get user current location. Also fused Location is freezing.
Below is my code:
private Location getLastKnownLoaction(boolean enabledProvidersOnly){
LocationManager manager = mActivityContext.getSystemService(Context.LOCATION_SERVICE);
Location location = null;
List<String> providers = manager.getProviders(enabledProvidersOnly)
for(String provider : providers){
location = manager.getLastKnownLocation(provider);
//maybe try adding some Criteria here
if(location != null) return location;
}
//at this point we've done all we can and no location is returned
return null;
}
I have seen tower location being displayed in older Nokia phones. How do I implement similar functionality in Android?"
Make sure you have this on your manifest
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
And also must try it on real device not simulator.
It is not possible to get only tower location in android. What you can do is following :
In your manifest, add permission :
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
and in your code, use
Location location = manager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
It helps you retrieve network based location. If its course, It will provide you with Tower based location most probably.
Also if you get a null in getting last location, you can request for a new location.
I have got total wrong location in my LocationManager
locationService = (LocationManager) getSystemService(LOCATION_SERVICE);
Log.i(log_tag, "get best provider");
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
provider = locationService.getBestProvider(criteria, true);
clearLocationCache(provider);
Location location = locationService.getLastKnownLocation(provider);
Once I was in Moscow and got a location in my application, but when I arrived in London I have got a same location on my device. I got correct location only after restarting my android device.
The reason comes from this line
locationService.getLastKnownLocation(provider);
as it always returns the last known location
Best way to get location either by GPS or network use these permissions.
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
Have you given these permissions?
LocationManager location = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
location.getLastKnownLocation(LocationManager.GPS_PROVIDER);
How can i get current location using WiFi?
In my project using gps I can get current location ...
How can I use network service in android?
LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener currentlocation = new CurrentLocation();
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
currentlocation);
But I want current location WiFi?
Can anyone help me for getting current location using WiFi?
Try to use NETWORK_PROVIDER.
Please note, that you will have to amend your AndroidManifest.xml as well by adding:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
I enabled the GPS in my device but I am not getting the loaction:
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
if(lm.isProviderEnabled(LocationManager.GPS_PROVIDER))
{
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(location!=null)
{
showCurrentAddress(location);
}
}
But here I am gettin location is null and I added the following permissions to my code:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
Could you please help?
lm.getLastKnownLocation can return null because that is a getter for the last known location.
Try using lm.requestLocationUpdates(LocationListener).
Hi check this tutorial for GPS location , i am also using this code for getting latitude and longitudeUsing GPS to get current location – Android tutorial check this on your device not on emulator first time just wait for few seconds..