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
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 use Google Map Android API v2 in my app and I have a little problem as in the picture below but have no ideas how to fix it. I set marker to current location of device but its now in the center of circle. I want to set marker to the center of the circle. Is it possible or not?
Thanks for any help!
Code that I used:
// Showing status
if(status!= ConnectionResult.SUCCESS){ // Google Play Services are not available
int requestCode = 10;
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode);
dialog.show();
}else { // Google Play Services are available
// Getting reference to the SupportMapFragment of activity_location.xml
SupportMapFragment mSupportMapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
// Getting GoogleMap object from the fragment
mGoogleMap = mSupportMapFragment.getMap();
// Enabling MyLocation Layer of Google Map
mGoogleMap.setMyLocationEnabled(true);
// Getting LocationManager object from System Service LOCATION_SERVICE
LocationManager mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
// Creating a criteria object to retrieve provider
Criteria criteria = new Criteria();
// Getting the name of the best provider
String provider = mLocationManager.getBestProvider(criteria, true);
// Getting Current Location
Location location = mLocationManager.getLastKnownLocation(provider);
if(location!=null){
onLocationChanged(location);
}
mLocationManager.requestLocationUpdates(provider, 20000, 0, this);
// Getting latitude of the current location
double latitude = location.getLatitude();
// Getting longitude of the current location
double longitude = location.getLongitude();
// Creating a LatLng object for the current location
LatLng mLatLng = new LatLng(latitude, longitude);
//Add Marker to current location of devise
mGoogleMap.addMarker(new MarkerOptions()
.position(mLatLng)
.title("Geolocation system")
.snippet("Your last current location which was available!"));
// .icon(BitmapDescriptorFactory.fromResource(R.drawable.icon_location)));
// Showing the current location in Google Map
mGoogleMap.moveCamera(CameraUpdateFactory.newLatLng(mLatLng));
// Show Zoom buttons
mGoogleMap.getUiSettings().setZoomControlsEnabled(true);
// Turns on 3D buildings
mGoogleMap.setBuildingsEnabled(true);
// Zoom in the Google Map
mGoogleMap.animateCamera(CameraUpdateFactory.zoomTo(14));
You are getting your location using GPS and network. these are not giving your correct location and you are using:-
mGoogleMap.setMyLocationEnabled(true);
this showing your location using google map api. so the loaction by you and google are different .so circle and marker are showing different location.
so you can use google's Fuesed Location client for this
Visit this for more help.
https://developer.android.com/reference/com/google/android/gms/location/FusedLocationProviderApi.html
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!
My app only needs a coarse location service when started up.
In detail, I need the app's rough location so as to provide the users with the shop info nearby.
The location does NOT need to be updated constantly. In addition, coarse localization will be sufficient in this case.
I wish the app to choose GSM, or wifi, or GPS automatically, whichever is available.
The location service should also be one-time to save phone energy.
How may I do that?
I have tried using GPS separately.
My problem is I don't know how to stop the constantly-refreshing-location feature of GPS. I don't know how to make the phone select one out of the three methods, either.
Some sample codes or ideas are greatly appreciated.
Here's a certain point of view:
private void _getLocation() {
// Get the location manager
LocationManager locationManager = (LocationManager)
getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String bestProvider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(bestProvider);
try {
lat = location.getLatitude();
lon = location.getLongitude();
} catch (NullPointerException e) {
lat = -1.0;
lon = -1.0;
}
}
This might however request a FINE_LOCATION access. So:
Another way is to use this which uses the LocationManager.
The quickest possible way is to use the Last Known location with this, I used it and it's quite fast:
private double[] getGPS() {
LocationManager lm = (LocationManager) getSystemService(
Context.LOCATION_SERVICE);
List<String> providers = lm.getProviders(true);
Location l = null;
for (int i=providers.size()-1; i>=0; i--) {
l = lm.getLastKnownLocation(providers.get(i));
if (l != null) break;
}
double[] gps = new double[2];
if (l != null) {
gps[0] = l.getLatitude();
gps[1] = l.getLongitude();
}
return gps;
}
you can use LocationClient it provides a unified/simplified (Fused) location API, check out this Video for introductory material and background or this developer guide
the main drawback is that you make you app dependent on the existence of Google Play Services on the device.
In order to get info from specific provider you should use: LocationManager.getLastKnownLocation(String provider), if you want your app to choose automatically between providers then you can add choosing of the provider with getBestProvider method. As for stopping refreshing of GPS location, I didn't quite catch. Do you need to get the location info only once or do you need to monitor location changes periodically?
EDIT: Oh by the way, if you want your location info to be up-to-date you should use requestSingleUpdate method of location manager, with specified Criteria. In this case provider should also be chosen automatically