so I have been developing this app for a few weeks now using my own handheld device and today I was trying to get Google map to center in on the current user location in my app
map.setMyLocationEnabled(true); //Locate the current position of the user
The setLocationEnabled(true) above should normally show the location of the user on the map and when the GPS button is pressed it will zoom into it. However after I added the following code this does not work anymore it doesn't show the location of the user anymore and I also I notice "Google Settings" was installed onto my device which after looking around was an update by Google so it might not be the code itself. But I do not know if Google Settings is the cause for 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());
map.moveCamera(CameraUpdateFactory.newLatLngZoom(userLocation, 15));
Even after commenting out this block of code the problem still persist. I do not know how to fix this and I hope someone could help me. I am not sure if this has to do with the code itself or just my device, it literally does not get the location of the user at all even though it was working for the past few weeks and it doesn't give any errors or warnings even pressing the GPS button on my map gives nothing no errors or warnings.
EDIT: I tried using an older working version of my code for this app and it still does not work in terms of getting the user location to come up on the map.
Thanks.
Why you dont use:
map.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(map.getMyLocation().getLatitude(),map.getMyLocation().getLongitude()), 15));
The map store your actual location, so you dont need LocationManager class.
Related
I would have thought that requesting the "lastKnownLocation" would return a saved coordinate (GeoPoint) that points to the last known location of the device (from when it last had access to the GPS functionalities).
According to some tests (made on OSMDroid's GpsMyLocationProvider), it seems like requesting that location returns a NullPointer.
GpsMyLocationProvider provider = new GpsMyLocationProvider(MainActivity.mainActivity);
provider.addLocationSource(LocationManager.NETWORK_PROVIDER);
locationOverlay = new MyLocationNewOverlay(provider, map);
I've been trying to set up default GeoPoint for centering the map when it is first launched, and then recentering the map where the GPS now has confirmed a position.
The problem is that provider.getLastKnownLocation() returns a NullPointer exception unless it is given time to actually get the current location from the GPS.
What am I getting wrong here? Shouldn't the "last known location" be a GeoPoint that is saved in memory on the phone and always have something there, unless the phone never ever had access to GPS localization ?
Please see https://developer.android.com/training/location/retrieve-current
You must set the permissions correctly too. Note that at the bottom they describe why the location may be null so see these for troubleshooting.
If you're using an emulator see How to emulate GPS location in the Android Emulator?
I’ve implemented Google map & fuse location API to get current location.
But facing issue like unable to get current location (blue dot) after accepting permission at run time .
This is happened only in fresh installed app, if i kill the app and reopen the app then its works fine.
am i missing something?
Thanks
Enable your location
map.setMyLocationEnabled(true);
Add
map.setMyLocationEnabled(true); in
onRequestPermissionsResult() method So it will update the current location after you accept the permission
So, I'm trying to build a weather app for practice and I've been running into the problem of trying to get the location. I've read people suggest to use getLastLocation through the fused location API, problem with that is if they don't already have a location registered on the device it comes up null. I've noticed using the emulator times that this come up is rare, but I'd still like my app to handle it properly. One instance where you might run into this is if they just turned GPS off and back on, or if the phone was just turned on. One thing I did was if getLastLocation does come back null, is to request an update, but then you still have to wait for the device to register an updated location, which with a weather app all of the data is based off of and you're still kind of running into the same problem. I've noticed with other apps this isn't a problem, like sometimes I actually have to load up Google Maps to get it to register a location. How does Google Maps force it to update the location? Here's the example from my getLocation method:
public void getLocation() throws SecurityException {
boolean gps_enabled;
LocationManager lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (gps_enabled) {
Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (location != null) {
startGetForecast(location);
} else {
LocationRequest request = LocationRequest.create();
request.setNumUpdates(1);
request.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, request, this);
}
}
else {
AlertDialogFragment alertDialogFragment = new AlertDialogFragment();
alertDialogFragment.setErrorTexts("Location Unavailable", "Could not retrieve location information.", "Exit");
alertDialogFragment.show(getSupportFragmentManager(), "Location Unavailable");
}
}
Note: This answer could sound like not answering to the question directly but after chatting (comments in question) with OP as his code was ok in general but the condition around the problem, this answer has satisfied OP.
Fortunately, the standard library Is not the only way Google can get
code into your hands. In addition to the standard library, Google
provider Play services. This is a set of common services that are
installed alongside the Google Play store application. To fix this
location mess, Google shipped a new locations services in Play
Services called the Fused Location Provider.
Since these libraries
live in another application, you must actually have that application
installed. This means that only devices with the Play Store app
installed and up to date will be able to use your application.
So the conclusion is:
You need to test the app on your device as mentioned above.
Since you are using Fused Location Provider Api, that means the Api will automatically determine last location from one of the following sources:
GPS radio
Coarse points from cell towers
WiFi connections
So you could easily remove the GPS if condition from your code
Be a ware of that if you must use GPS signal, you need to be out side the building, lab, home or office.
If you want to dig more find more in the mentioned resources and there is a lot of online resources.
Resources: The first part of the answer, the source of it is from:
Android Programming: The Big Nerd Ranch Guide 2nd edition chapter 31 page 552 under Google Play Services
I got the current location latitude and longitude while giving on in a switch button in android and displaying the location in google map. But I need the latitude and longitude changes automatically wherever the device moves and the location on the map should change with respect to the position. How is it possible? Can anyone give me a way?
You could have a look at GoogleApiClient.
You can use requestLocationUpdates(...) to get the users position in a given intervall.
You can find more Info here
But honestly you could have find the answer on your own if you googled a bit.
I'm working on an indoor GPS application and I have my own location provider that will give me lat and long. I have google maps v2 working with my location provider by using
mMap.setMyLocationEnabled(true);
mMap.setLocationSource(myLocationProvider);
My problem is as I set the location source to my custom provider, the little blue icon that displays onscreen location no longer functions. Is this expected? Shouldn't google maps just use the lat/long to display the current location, no matter where it comes from?
The blue dot should still be visible.
If you are sure your LocationSource is implemented properly, and after you call OnLocationChangedListener callback method the blue dot does not appear in the location you send, I suggest creating a simple working example to show the issue and post it on gmaps-api-issues.
I would start with a test call in activate:
Location location = new Location("myProvider");
location.setLatitude(30.0);
location.setLongitude(30.0);
listener.onLocationChanged(location);