i want to get current coordinates, i try next
GoogleMap gm=getMap();
gm.setMyLocationEnabled(true);
Location location=getMap().getMyLocation();
But getMap() returns null,
and i try :
Criteria criteria = new Criteria();
String provider = service.getBestProvider(criteria, false);
Location location = service.getLastKnownLocation(provider);
if (location.getLatitude()!=0){
LatLng userLocation = new LatLng(location.getLatitude(), location.getLongitude());
But this work's in Android 2.3, but in 4.2. it returns null.
I have pretty much the same code in my app and it works well on version 4 :
String provider;
LocationManager locManager = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
provider = locManager.getBestProvider(criteria, false);
Location location = locManager.getLastKnownLocation(provider);
if (location != null) {
latitude = location.getLatitude();
mLongitude = location.getLongitude();
}
But your problem must be related to your getMap() error. What exactly is the error message ?
The location properties are reset when the phone is rebooted.
So if the device hasn't requested a new location by any means(Maps or any other apps that request a location), getMyLocation will return null.
What you should do, if getMyLocation returns null, is to request a new Location.
Here is a thread on how to get/request a location:
Good way of getting the user's location in Android
Look for the requestLocationUpdates() method.
Then when a location has been found(It can take some time...especially indoor), the callBack OnLocationChanged is called with the new location in parameter.
Hope this helps!
Related
I have tried FusedLocationAPI, however, the accuracy it returns is too high as compared to setMyLocationEnabled(true), so the result is as follows, where the Blue circle is plotted by setMyLocationEnabled and the Black one is the accuracy as plotted by FusedLocationAPI.
I also used this piece of code
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location myLocation = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (myLocation == null) {
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
String provider = lm.getBestProvider(criteria, true);
myLocation = lm.getLastKnownLocation(provider);
}
LatLng myPos = new LatLng(myLocation.getLatitude(), myLocation.getLongitude());
googleMap.addMarker(new MarkerOptions().position(myPos).title("Start"));
But the marker plotted is again, too far away from he setMyLocationEnabled(true) like this
So I was wondering if there was a better way to get location more accurately?
Try using ACCURACY_FINE instead of ACCURACY_COARSE in your criteria.setAccuracy(Criteria.ACCURACY_COARSE); Hope that helps.
Hello this is a very bizzare error I am having. I am trying to get the distanceTo() from my location to a fixed location which I know how far it is. When my WiFi is turned off I get a very strange number but when the WiFi is on, even if it is not connected to any network I get the correct number. What would be the cause of this. I am testing on a Samsung Galaxy S3.
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
Criteria crit = new Criteria();
str = locationManager.getBestProvider(crit, false);
locationManager.requestLocationUpdates(str , 0, 1, this);
#Override
public void onLocationChanged(Location location) {
LatLng myLatLng = new LatLng(location.getLatitude(), location.getLongitude());
Location destination = new Location("dest");
destination.setLatitude(54.008447);
destination.setLongitude(-2.783383);
float distance = location.distanceTo(destination);
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(myLatLng, 15);
mMap.animateCamera(cameraUpdate);
String destLocation;
destLocation = distance/2 +"";
Log.e("distance", destLocation);
locationManager.removeUpdates(this);
}
I don't use the getLastKnownLocation because I read somewhere that it is not reliable and it is better to get a location update before showing your location.
When you see the blue dot on the map, use mMap.getMyLocation() or mMap.setOnMyLocationChangeListener(...) instead of all the code that uses LocationManager. You will get the correct location.
I have this code for getting the long and lat coordinates.
String bestProvider;
LocationManager lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
bestProvider = lm.getBestProvider(criteria, true);
Location location = lm.getLastKnownLocation(bestProvider);
if (location == null){
}else{
geocoder = new Geocoder(this);
try {
setLat((double)location.getLatitude());
setLng((double)location.getLongitude());
}catch (Exception e) {
e.printStackTrace();
}
}
I tried using different devices. Some returns a value for lat and long. Some do not. All devices were connected to the internet and gps enabled. Do you guys have any idea on what is causing this? Thanks!
It is due to getLastKnownLocation which may or may not exists. I suggest you to request single location update. Also remember that lastknownlocation is not necessarily accurate one it may belong to 2 days before.
dev guide:
http://developer.android.com/reference/android/location/LocationManager.html
A guide about single update
http://androidexperinz.wordpress.com/2012/04/19/current-location-update/
If there is no previous captured location, then getlastknown location does not return any value.So you need to implement the location listener to capture the location.
I am working on android application. I am stuck in problem.
getLastKnownLocation(provider) returns null for Android version 4.1.1, while for other versions it is fine. Provider is enabled and the rest is ok. I don't know where is the problem. Here is the code.
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, true);
location = locationManager.getLastKnownLocation(provider);
double latitude = location.getLatitude();
double longitude = location.getLongitude();
LatLng latLng = new LatLng(latitude, longitude);
mMap.clear();
System.out.println("Current Location = "+latLng);
locationManager.getLastKnownLocation will return null if the provider is disabled, and here is the documentation
http://developer.android.com/reference/android/location/LocationManager.html#getLastKnownLocation(java.lang.String)
try this its working for me...
mGoogleMap.setOnMyLocationButtonClickListener(new OnMyLocationButtonClickListener() {
#Override
public boolean onMyLocationButtonClick()
{
Location myLocation = mGoogleMap.getMyLocation();
onLocationChanged(myLocation);
return false;
}
});
I'm trying to get the location of the device, but the code
Criteria criteria = new Criteria();
String provider = myLocationManager.getBestProvider(criteria, false);
myLocationManager.requestLocationUpdates(provider, 0, 0,
myLocationListener);
Location lastLocation = myLocationManager.getLastKnownLocation(provider);
"lastLocation" returns null. if restart the device, the same null is obtained. is there any other standard way to obtain latitude and longitude from the device?
You will receive your location asynchonously in your myLocationListener. After that (when listener is invoked) gatLastKnownLocation will return something not null.
Here is a complete example of how it should be implemented.