I have a problem with setting current location with GPS.
It works like it should(notification about finding location, then set it properly) on Android 5.0.1(Xiaomi Redmi Note 3)
But for example on Android 6.0 (Sony Xperia Z5 Compact) it doesn't work at all. No notification, GPS position set never. On Xperia I even don't have button on the right of map which is showed by line in code(mMap.setMyLocationEnabled(true);)
On Android 4.4.2(Gigabyte GsMart Roma R2plus) also no notification comes up, but I can see button of current location on right on top of screen. But still doesn't work like it should.
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
mMap.setMyLocationEnabled(true);
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(provider);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
} else {
}
final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
emailicek = user.getEmail();
emailicek = emailicek.replace(".", "");
mDatabase = FirebaseDatabase.getInstance().getReference();
if (latitude != 0) {
if (longitude != 0) {
mDatabase.child("userdata/" + email + "/latitude").setValue(latitude);
mDatabase.child("userdata/" + email + "/longitude").setValue(longitude);
}
}
LatLng latLng = new LatLng(latitude, longitude);
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
//mMap.animateCamera(CameraUpdateFactory.zoomTo(14));
if (latitude != 0) {
mMap.animateCamera(CameraUpdateFactory.zoomTo(14));
} else {
mMap.animateCamera(CameraUpdateFactory.zoomTo(1));
}
}
Did you make sure you requested the right permissions in the Manifest?
to get the last known location you'll need
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
for network based location, or:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
for GPS based location.
By the way there are a lot of good reasons why you should consider moving to Play Services to get your location, I strongly suggest you to read this:
Getting the Last Known Location - Android Developers
Related
I get totally wrong location when using the LocationManager for a location. I get a Location about 500km faraway, the accuracy is around 588902. My Google Maps is showing the accurate position.
Does anyone have a idea about what the problem can be? It is the same when I use the getLastKnownLocation(). below is my code.
LocationManager locationManager = (LocationManager) getApplicationContext().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
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
Location myLocation = locationManager.getLastKnownLocation(provider);
//set map type
//mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
// Get latitude of the current location
double latitude = myLocation.getLatitude();
// Get longitude of the current location
double longitude = myLocation.getLongitude();
System.out.println("Lati >>> "+latitude + " Longi>>>>>>> "+longitude + " accuracy + = " + myLocation.getAccuracy());
// Create a LatLng object for the current location
LatLng latLng = new LatLng(latitude, longitude);
// Show the current location in Google Map
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
// Zoom in the Google Map
mMap.animateCamera(CameraUpdateFactory.zoomTo(14));
mMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).title("You are here!"));
What I am trying to do is to get location from user. First it tries to get location through GPS and checks whether location is available. If not then it it tries to get through Network tower. When GPS is turned on it is working fine. But when I turn off GPS it's not getting location using network provider
manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
provider = manager.getBestProvider(new Criteria(), false);
Log.i("ITEMSET best", provider);
manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
Log.i("ITEMSET provider", provider);
location = manager.getLastKnownLocation(provider);
Log.i("ITEMSET netLoc", location + "");
if(location==null) {
provider = LocationManager.NETWORK_PROVIDER;
location = manager.getLastKnownLocation(provider);
Log.i("ITEMSET netLoc", location + "qqq");
}
Log.i("ITEMSET current", provider);
Log.i("ITEMSET location", location + "");
Location was initially null because GPS wasn't turned on. But then changed provider to network provider. But location still null
Check Whether Network is Enabled or not
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
then:
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 5000,10, this);
Log.d("Network", "Network");
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
You should use GoogleApiClient to get your location. Please visit this post
I am trying to get a device's location information using GPS, but for some reason one of the two coordinates is missing sometimes.
This is the code:
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double longitude = location.getLongitude();
double latitude = location.getLatitude();
Log.d("LOCATION1", "Longitude: " + longitude);
Log.d("LOCATION2", "Latitude: " + latitude);
Sometimes I do get both coordinates, but not always, which makes me think about some kind of delay somewhere. Is there a way to find out why a GPS coordinate is missing when this happens?
Because the GPS isn't always on. getLastKnownLocation will return a location if it knows one and if it isn't too stale. Since nothing else was using the GPS, it doesn't know one. If you need a location, either requestLocationUpdates or requestSingleUpdate, which will turn on the GPS and get a new location.
Criteria criteria = new Criteria();
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
// mMap.addMarker(new MarkerOptions().position(sydney2).title("fi"));
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
locationManager.requestLocationUpdates(locationManager.getBestProvider(criteria, true), 2000, 0, new android.location.LocationListener() {
#Override
public void onLocationChanged(Location location) {
//
}
in onLocationChanged method you can use location.getLatitude & location.getLongitude
I am trying to get a device's location information using GPS, but for some reason one of the two coordinates is missing sometimes.
This is the code:
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double longitude = location.getLongitude();
double latitude = location.getLatitude();
Log.d("LOCATION1", "Longitude: " + longitude);
Log.d("LOCATION2", "Latitude: " + latitude);
Sometimes I do get both coordinates, but not always, which makes me think about some kind of delay somewhere. Is there a way to find out why a GPS coordinate is missing when this happens?
Because the GPS isn't always on. getLastKnownLocation will return a location if it knows one and if it isn't too stale. Since nothing else was using the GPS, it doesn't know one. If you need a location, either requestLocationUpdates or requestSingleUpdate, which will turn on the GPS and get a new location.
Criteria criteria = new Criteria();
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
// mMap.addMarker(new MarkerOptions().position(sydney2).title("fi"));
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
locationManager.requestLocationUpdates(locationManager.getBestProvider(criteria, true), 2000, 0, new android.location.LocationListener() {
#Override
public void onLocationChanged(Location location) {
//
}
in onLocationChanged method you can use location.getLatitude & location.getLongitude
I have a program that gets latitude and longitude of the user.
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
if (ActivityCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
}
Location myLocation = locationManager.getLastKnownLocation(provider);
Location myLocation = locationManager.getLastKnownLocation(provider);
boolean netEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
boolean isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (netEnabled) {
if (isGPSEnabled) {
latitude = myLocation.getLatitude();
longitude = myLocation.getLongitude()
} else if (!isGPSEnabled) {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
} else {
// displayPromptForEnablingGPS(MapsActivity.this);
}
}
} else if (!netEnabled) {
if (isGPSEnabled) {
latitude = myLocation.getLatitude();
// Get longitude of the current location
longitude = myLocation.getLongitude();
} else {
// displayPromptForEnablingGPS(MapsActivity.this);
}
}
(displayPromptForEnablingGPS is for ACTION_LOCATION_SOURCE_SETTINGS)
In the program, if GPS and network providers are off, a window is opened for switching on them. When i switch GPS on in the first time, it gives error in first if(GPSEnabled) method(error on latitude=myLocation.getLatitude()). I can't get latitude and longitude. It is also same when i switch on only network provider. It gives "location" variable null. But if i open Google Maps, it starts to get latitude and longitude.
What can the problem be? It is problem of phone or i must make GPS run by writing any code?
Thanks for your help.
The method will return null value in case there’s no location information previously.
It’s better to get last known location iterate over all of the enabled providers, because this is the case that
there’s no GPS and Network provider. >> link on SO
It’s just better to add handling that case
if(!netEnabled && isGPSEnabled ){
//no network provider is enabled
Above sample code : here