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.
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.
I am new to android, can anyone help me for my question.... How to get current Location and getting the registered user's location available within 3km or 5km distance??????
It's like a Find Taxi to show the user location nearby the taxi available.
I can get current location by using this code.
// Getting LocationManager object from System Service LOCATION_SERVICE
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
// Creating a criteria object to retrieve provider
Criteria criteria = new Criteria();
// Getting the name of the best provider
String provider = locationManager.getBestProvider(criteria, true);
// Getting Current Location From GPS
Location location = locationManager.getLastKnownLocation(provider);
if(location!=null){
onLocationChanged(location);
}
locationManager.requestLocationUpdates(provider, 20000, 0, this);
final LatLng latLng = new LatLng(mLatitude, mLongitude);
Toast.makeText(MainActivity.this, " "+latLng, Toast.LENGTH_SHORT).show();
#Override
public void onLocationChanged(Location location) {
mLatitude = location.getLatitude();
mLongitude = location.getLongitude();
}
#Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
#Override
public void onProviderEnabled(String s) {
}
#Override
public void onProviderDisabled(String s) {
}
Use the distanceBetween method as it sounds like you already have the coordinates and that's all you need with this method:
Location.distanceBetween() Javadoc
This link might be helpful to you, as it details the use of the Haversine formula to calculate the distance.
Excerpt:
This script [in Javascript] calculates great-circle distances between the two points – that is, the shortest distance over the earth’s surface – using the ‘Haversine’ formula.
Use geocoder.getFromLocationName, you get back a list of Address objects, in those objects you will find all the information you need about the location.
To test this on the simulator you need an image with the Google APIs included. In a device should be fine if you have the market installed.
There are two steps to this:
Get the current location - latitude & longitude, using the GPS, network, last-known location etc. The Android location documentation includes sample code.
Use the Android Geocoder class to request a lookup to convert the lat/long to an Address (from which you can easily extract city, country, street, etc). Specifically, you need to use the getFromLocation() method
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 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!
My app has been ported from Android to blackberry and it works fine...
the only thing not working is the GPS location which works on the android....
the app displays a message to the using that we are trying to find the location and if no location is found we tell the user that we could not retrieve a location..
here is the code
private void gpsLocation()
{
//Get the location manager
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
//Provide Criteria
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(provider);
locationManager.requestLocationUpdates(provider, 400, 1, this);
if(location != null){
onLocationChanged(location);
} else{
Log.i(TAG, "no GPS location available - waiting for GPS fix");
Toast.makeText(getApplicationContext(), R.string.gpsWaitingToast, Toast.LENGTH_SHORT).show();
if (Debug.GPS_USE_MONTREAL) {
currentLatitude = Double.valueOf(Debug.GPS_LAT_MONTREAL); currentLongitude = Double.valueOf(Debug.GPS_LNG_MONTREAL); // montreal
}
waitForGpsFix(GPSFIX_DELAY);
}
its not working,, I restarted the app ,, did it next to a window and all that stuff..
nothing works..
your advise and help is greatly appreciated.
Regards
There was previously a bug where LocationManager.NETWORK_PROVIDER returned null, which may still be returned by the locationManager.getBestProvider() method. This should be fixed in the newer OS builds, however.
If you'd like to fix your code immediately, all you would need to do would be to set:
provider = LocationManager.GPS_PROVIDER
as in the early stages, GPS_PROVIDER was the only provider that was implemented in the player.