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.
Related
i tried this but its showing my current location first time i get information that i am inside or not . but when i moved outside of radius of point 1 its doesn't showing anything.
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
//drawing marker
LatLng pointOne = new LatLng(23.10,72.50847684);
mMap.addMarker(new MarkerOptions().position(pointOne).title("Point 1"));
mMap.addMarker(new MarkerOptions().position(new LatLng(0,0)).title("Point 2").snippet("N"));
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
mMap.setMyLocationEnabled(true);
// Get LocationManager object from System Service LOCATION_SERVICE
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// Create a criteria object to retrieve provider
Criteria criteria = new Criteria();
// Get the name of the best provider
String provider = locationManager.getBestProvider(criteria, true);
// Get Current Location
Location myLocation = locationManager.getLastKnownLocation(provider);
//myLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double latitude = myLocation.getLatitude();
Log.e("latitude", String.valueOf(latitude));
// Get longitude of the current location
double longitude = myLocation.getLongitude();
// Create a LatLng object for the current location
LatLng pointTwo = new LatLng(latitude, longitude);
// Show the current location in Google Map
mMap.moveCamera(CameraUpdateFactory.newLatLng(pointTwo)); Log.e("location", String.valueOf(pointTwo));
//drawing overlay circle on marker
Circle circle = mMap.addCircle(new CircleOptions()
.center(pointOne)
.radius(5000)
.strokeColor(0x40FF0000)
.fillColor(0x35FF0000));
//for Checking marker is inside or not
float[] distance = new float[2];
Location.distanceBetween( pointTwo.latitude, pointTwo.longitude,
circle.getCenter().latitude, circle.getCenter().longitude, distance);
if( distance[0] > circle.getRadius() ){
Toast.makeText(getBaseContext(), "Outside", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getBaseContext(), "Inside", Toast.LENGTH_LONG).show();
}
pointToPosition(pointOne);
}
Thanks for Help In advance
You can Use Google GeoFencing API to draw a virtual circle of some radius around the location.On user's entry/exit an event will be triggered which will help your cause.
Now getLastKnownLocation() which you are using May or May Not provide a location value. LocationManager maintains GPS location data until that data becomes outdate in like 8mins odd. After this, there will again be a cold startup and the GPS in your phone will try to get a fix and return results.
In case this is a cold startup, you can also get a NullPointerException because of the fact that the your last know location is outdated.
You can try a requestLocationUpdates() .... if lastKnownLocation returns a Null result...
This will cause the LocationManager to explicitly call the GPS in your phone to search for the current location... this will provide results in a callback function onLocationChanged... here you can get the current location....
i want to make a program to calculate the distance between some places to my current location, but my googleMap.getMyLocation(); doesnt work properly.
googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
googleMap.setMyLocationEnabled(true);
googleMap.getUiSettings().setCompassEnabled(false);
mylocation = googleMap.getMyLocation();
direction = new GMapV2Direction();
LatLng from, to;
from = new LatLng(-7.26071409, 112.80674726);
for(int i=0; i<lat.length; i++){
to = new LatLng(lat[i], lon[i]);
doc = direksi.getDocument(from, to, GMapV2Direction.MODE_DRIVING);
distance[i] = (double)direction.getDistanceValue(doc) / 1000;
}
i saved the latitude and longitude of some places in lat[] and lon[].
LatLng 'from' is mylocation and 'to' is my destination places.
the problem appear when i change
from = new LatLng(-7.26071409, 112.80674726);
to
from = new LatLng(mylocation.getLatitude(), mylocation.getLongitude());
i want to do the calculation without opening the googlemaps. the google maps will appear when i touch the map button as pop up window. so the calculation will happen without opening googlemap
please help me
Try this...I hope this will be help to you
LocationManager locationManager = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
Location location = locationManager.getLastKnownLocation(locationManager
.getBestProvider(criteria, false));
double latitude = location.getLatitude();
double longitude = location.getLongitude();
This is my code for you, I hope this help you... edit it if you think, it isn't with correct sintaxis...
This work for android 4.x and 5.x and 6.x, I not test this in android 7
//I use this like global var to edit or get informacion about google map
GoogleMap gMap;
#Override
public void onMapReady(GoogleMap googleMap) {
gMap = googleMap;
if (ActivityCompat.checkSelfPermission(Activity_Map.this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(Activity_Map.this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(Activity_Map.this,new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, 1);
}else{
if(!gMap.isMyLocationEnabled())
gMap.setMyLocationEnabled(true);
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);
}
if(myLocation!=null){
LatLng userLocation = new LatLng(myLocation.getLatitude(), myLocation.getLongitude());
gMap.animateCamera(CameraUpdateFactory.newLatLngZoom(userLocation, 14), 1500, null);
}
}
}
getMyLocation() => this method is deprecated. You need to use LocationClient instead.
You can find informations about LocationClient and how to get current location here
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 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!
In my application I have a button, to show the user his way to go, on a Google Maps view.
Is there a way to get the Home-/Work location of the user from Google Now?
You can use Google Maps API in your application and find out your location through existing methods like this
LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = service.getBestProvider(criteria, false);
Location location = service.getLastKnownLocation(provider);
LatLng userLocation = new LatLng(location.getLatitude(),location.getLongitude());
credits : https://stackoverflow.com/a/14493351/2071742