Im using GoogleMap.OnMyLocationChangeListener to get the current location (below)
private GoogleMap.OnMyLocationChangeListener myLocationChangeListener = new GoogleMap.OnMyLocationChangeListener() {
#Override
public void onMyLocationChange(Location location) {
start = new LatLng(location.getLatitude(), location.getLongitude());
map.addMarker(new MarkerOptions().position(start));
}
};
However, I need a way to wait for the listener to complete. Reason being that, Start var will obviously be null if I continue to execute other code without location data. I thought that AsyncTask might be useful here, but have no idea what to call inside doInBackground/onPostExecute.
map.setOnMyLocationChangeListener(myLocationChangeListener); // set the listener
// perform wait and initiate start variable
String url = getDirectionsUrl(start, destination);
// a sync task here using the url
Any hints or code snippets would be much appreciated~!
I need the location information after setting the listener
That is not possible. There may not be any "location information after setting the listener". It may take some time to get a location fix. Given your existing code, your first opportunity to have location data will be in onMyLocationChange(), and you cannot use the location data before that point.
Also note that setOnMyLocationChangeListener() has been deprecated for about a year. Quoting the documentation:
This method is deprecated.
use FusedLocationProviderApi instead. FusedLocationProviderApi provides improved location finding and power usage and is used by the "My Location" blue dot. See the MyLocationDemoActivity in the sample applications folder for example example code, or the Location Developer Guide.
Related
I'm building an android app and I want my app to provide location based content. For example if I'm near a museum or in one, shows me details about that museum. The thing is how to identify that place.What is the best method?
Well beacons do the job perfectly, where they emit a signal that has an id, my app reads the id and provide related information.But it would take some time for me to get some beacons so if there are any similar technologies, please list them down here.
An GPS technology or APIs are fine as long as its simple without being overly complicated since I just want my app to verify it has arrived to a location, and based on that, just show the content.
what are the options I have got?
The simplest option might be to use your phone gps location, without beacons.
For this you need to find a museum api to display information about nearest museums.
Then, you can use LocationManager in Android to first check if location permissions are granted:
private Location getLastKnownLocation() {
Location n = null;
LocationManager mLocationManager = (LocationManager)getApplicationContext().getSystemService(LOCATION_SERVICE);
List<String> locationProviders = mLocationManager.getProviders(true);
for (String provider : locationProviders) {
if(ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION)==PackageManager.PERMISSION_GRANTED) {
n = mLocationManager.getLastKnownLocation(provider);
}
}
return n;
}
Then we need to do the following:
Location gps = getLastKnownLocation();
String latitude = Double.toString(gps.getLatitude());
Log.e(“User Location Latitude”, “” + gps.getLatitude());
String longitude = Double.toString(gps.getLongitude());
Log.e(“User Location Longitude”, “” + gps.getLongitude());
Uri baseUri = Uri.parse(“YOUR_API_URL” + latitude + “YOUR_API_URL” + longitude + “YOUR_API_URL”);
Log.e(“Api Query”, “” + baseUri);
Uri.Builder uriBuilder = baseUri.buildUpon();
Another elegant way to get a location would be to implement getLastKnownLocation the Reactive way.
This approach allows keeping code concise using lambdas, chaining observables & using deferred computation. A way to do so would be to combine both the RxPermissions library & RxLocation library for flexibility.
Below code for this post is taken from both libraries’ official documentations provided, with some modifications to suit this discussion:
final RxPermissions rxPermissions = new RxPermissions(this);
rxLocation = new RxLocation(this);
Like in the conventional non-reactive method earlier, we create getLastKnownLocation:
private void getLastKnownLocation(final String apiSearch) {
compositeDisposable.add(rxPermissions
.request(Manifest.permission.ACCESS_FINE_LOCATION)
.subscribe(granted -> {
if (granted) {
The lastLocation() below belongs to a Maybe<T>, which is a lazy implementation that represents a deferred Rx computation.
It allows onSuccess, onComplete & onError to operate as mutually exclusive events, unlike an Observable, such that if location is not available, no value is returned to the subscriber.
Using this library wraps the LocationServices.FusedLocationApi in rxLocation.location():
rxLocation.location().lastLocation()
.doOnSuccess(location -> {
// Your code implementation for Received Last Known Location: If last known location is already known & exists, to pass the existing last known location into your museum api, to fetch results to your adapter
})
.doOnComplete(() -> {
// Your code implementation for handling the Location Request, then doing a Completing action: This sends a new request to request the latest user location, before subscribing to your museum api, to fetch results to your adapter
})
.doOnError(throwable ->
// Your code implementation to handle any errors thrown: Pops a toast message to prompt user to enable GPS location on phone
})
Following the documentation, we send a request for user’s location:
private void latestLocation(final String apiSearch) {
LocationRequest locationRequest = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setInterval(6000); // 6s
We subscribe to location updates, to prevent location returning null, which is possible in conventional non-reactive method. Using this library wraps the LocationServices.FusedLocationApi in rxLocation.location():
rxLocationSubscriber = rxLocation.location().updates(locationRequest)
Because fromLocation() call below belongs to a Maybe<T>, it returns a Maybe. We convert it into an Observable through toObservable, to use it with flatMap, that supports thread concurrency. This library wraps Geocoder API in rxLocation.geocoding():
.flatMap(location ->
rxLocation.geocoding().fromLocation(location).toObservable())
.subscribe(location -> {
// your code implementation for museum api
rxLocationSubscriber.dispose();
});
}
I think the most straightforward approach would be the first method outlined.
Hope this is helpful.
I am developing an android application in which I need to get my current Location. I have successfully wrote the code and I am getting my current location using Google Play Service.
The problem is sometimes it gives me the location after a long time. I have noticed that it was only for first use of the app.
Any way to avoid this problem and get the current location fast? Is it related to the version of google play service in my code? (I am not using the last one in fact I am using version 9.8.0.)
As #tahsinRupam said, avoid using getLastLocation as it has a high tendency to return null. It also does not request a new location, so even if you get a location, it could be very old, and not reflect the current location. You might want to check the sample code in this thread: get the current location fast and once in android.
public void foo(Context context) {
// when you need location
// if inside activity context = this;
SingleShotLocationProvider.requestSingleUpdate(context,
new SingleShotLocationProvider.LocationCallback() {
#Override public void onNewLocationAvailable(GPSCoordinates location) {
Log.d("Location", "my location is " + location.toString());
}
});
}
You might want to verify the lat/long are actual values and not 0 or something. If I remember correctly this shouldn't throw an NPE but you might want to verify that.
Here's another SO post which might help:
What is the simplest and most robust way to get the user's current location on Android?
I am developing a proximity alert related project. For that whenever I opened my app, I need to get exact readings of where I am. Even though I am following the right coding practices prescribed by android documentation, I am not getting the expected result.
Why there is no alternative command in entire android Geolocation coding for getLastKnownLocation which will give us where we are earlier not now.
I have did one javascript coding in the same lines. There My code is working properly. The Descriptive address and coordinates where my device is working nice there. Those commands getCurrentPosition and watchPosition are givinga beautiful response via their event handler callbacks. Why there is no getCurrentLocation in android geolocation parlance?
Even though I have followed relevant coding practices, MyLocationListener myLocationUpdate which implements LocationListener is not updating my new location when I am moving from one place to another place. I gave MINIMUM_DISTANCE_CHANGE_FOR_UPDATES as 1(in meters) and MINIMUM_TIME_BETWEEN_UPDATES as 1000 (in milliseconds).
I am giving important code snippets below to understand the problem
in onCreate handler of the activity
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
boolean enabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (!enabled) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
myLocationUpdate = new MyLocationListener();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,MINIMUM_TIME_BETWEEN_UPDATES,MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, myLocationUpdate);
retrieveLocationButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"Finding Location",Toast.LENGTH_LONG).show();
showCurrentLocation();
}
});
latituteField = (TextView) findViewById(R.id.display_Location);
showCurrentLocation();
in showCurrentLocation function
I am using locationManager.getLastKnownLocation(provider) to retrieving that location.
By using GeoCoder Object and the command geocoder.getFromLocation(latitude, longitude, 1) to get First Address match for the coordinates.
// internal class to handle location cahnge event
private class MyLocationListener implements LocationListener contains all the Overridden functions including public void onLocationChanged(Location location)
But Practically I am getting nothing out of all the application. I have already recorded Time via location.getTime(). It is showing a fixed earlier time, but not the interval i specified.
the problem with getting GPS location is that it isnt available immediately. From my understanding of GPS location provider is that when you request location update, the gpr provider will try to connect to the gps satellites which runs in a separate thread (not entirely sure about it). In the meantime your program is executed normally and there maybe a chance that you wont get any location.
What you can do is use Fused Location Provide which was introduced in this year's IO Event. You can find the tutorial here
I'm trying to get the user's location within Android, the code below works however it always returns the same location no matter what I do. I've tested out in the middle of an empty parking lot to ensure the GPS is locked on, and it is. Google maps also shows my location correctly. Is there something wrong with the code below?
public class LocationTestActivity extends Activity implements LocationListener
{
private Location myLoc;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
output = (TextView) findViewById(R.id.output);
myLong = (TextView) findViewById(R.id.longi);
myLat = (TextView) findViewById(R.id.lat);
myRefreshed = (TextView) findViewById(R.id.counter);
mgr = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
mgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
}
#Override
public void onLocationChanged(Location location)
{
timesChanged++;
myRefreshed.setText("Refreshed: " + timesChanged);
myLong.setText("Longitude: " + location.getLongitude());
myLat.setText("Latitude: " + location.getLatitude());
}
}
I'd like to add that I can pass in a location using the emulator with no problem. I also removed the other needed methods required by LocationListener for clarity. Thanks for the help!
First make sure that onLocationChanged() is actually getting called. If your GPS has locked before then there is a chance that onLocationChanged won't get fired at all because the phone thinks that you haven't really moved. Therefore it is not a good practice to rely only on requestLocationUpdates() to get your current location.
In general here is what you should do to get your position:
You should use getLastKnownLocation() first to try locating your current location based on last known position.
Check if the location retrieved from step #1 is within reasonable time (not too old) by calling getTime() in the location and comparing with the current time
If this last known position is considered old (I normally use 5-10 minutes depending on the context of the app) then you start requestLocationUpdates() with specific distance (the app should make assumption that the user has moved within the specified limit of last known position)
Implement the onLocationChanged() as desired
Another note, I notice you are only using GPS, there is a lot of situation where GPS cannot lock and therefore never call onLocationChange(), your code should take account of that and checking Network based triangulation in the case onLocationChange() is not called within specified time
Try getting last location fix
Location loc = mgr.getLastKnownLocation(LocationManager.GPS_PROVIDER);
and give some time/distance between each location service request
mgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, this);
And I hope you have enough patience to wait until your device retrieves the location and onLocationChanged() is triggered ;) Joking.
please check your manifest file whether have you added those permission or not.
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
One more thing.Where did you call "removeUpdates()"?
So I'm trying to sample the gps coordinates just once in an application. I don't want to create a LocationListener object to constantly get gps updates. I want to wait until receiving the coordinates, and then proceed on to another task.
Here is a code snippet
LocationManager lm = (LocationManager)act.getSystemService(Context.LOCATION_SERVICE);
Criteria crit = new Criteria();
crit.setAccuracy(Criteria.ACCURACY_FINE);
String provider = lm.getBestProvider(crit, true);
Location loc = lm.getLastKnownLocation(provider);
The loc variable is always null in the emulator. I tried using the command "geo fix latitude longitude" to set it, and also I tried using the DDMS way of setting it. Neither method had any effect on the code. Also the snippet isn't causing any exceptions.
Thanks for your help.
The call to request update for a location is not blocking, hence it wont wait there. Also the provider in emulator may not have been started.
A possible check could be to see if the settings in it disable gps provider ? then send geo fix.
However, I would use Location Listener, it would be ideal in your case since you need a geo fix to proceed further.Location Listener is Used for receiving notifications from the LocationManager when the location has changed. You can unregister the listener after first geofix.
Note: It can take some time on device to get current location, and even on device this can return null.
Try using the MyLocationOverlay , create a runnable that does what you need to do with that GPS location, and pass it to
boolean runOnFirstFix(java.lang.Runnable runnable)
Queues a runnable to be executed as soon as we have a location fix.
and then disable the location updates for the MyLocationOverlay.
Edit: The reason the location is null is because at the time that code is run, no geofix has been received.