Get User Location (Android) Follow
This article describes how to get the user’s location using the Android SDK.
Get a User’s Location
A user’s most recent location can be retrieved with the LocationRequest class. This request is asynchronous.
private LocationRequest request = null;
private static final EditorKey appKey = new EditorKey("5809862863224832");
@Override
public void onResume() {
super.onResume();
request = LocationRequest.requestCurrentLocation(getActivity(), appKey, new LocationRequest.LocationRequestListener() {
@Override
public void onResult(MeridianLocation location) {
// Update UI with new Location
}
@Override
public void onError(LocationRequest.ErrorType location) {
// Notify user that there was an error retrieving the location
}
});
}
@Override
public void onPause() {
if (request != null && request.isRunning()) {
request.cancel();
request = null;
}
super.onPause();
}
Get Continuous Updates About a User’s Location
MeridianLocationManager determines the user’s location by gathering all available location data for your Meridian-powered app. Implement the MeridianLocationManager.LocationUpdateListener interface to get callbacks as new locations are gathered.
// In an Activity that implements the LocationUpdateListener interface
private static final EditorKey appKey = new EditorKey("5809862863224832");
private MeridianLocationManager locationHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
locationHelper = new MeridianLocationManager(this, appKey, this);
// Build out the UI ...
}
@Override
protected void onStart() {
locationHelper.startListeningForLocation();
super.onStart();
}
@Override
protected void onStop() {
locationHelper.stopListeningForLocation();
super.onStop();
}
@Override
public void onLocationUpdate(MeridianLocation location) {
// Update the UI with the new location
}
@Override
public void onLocationError(Throwable tr) {
// Update the UI to inform the user that their location could not be determined.
}
Comments
0 comments
Please sign in to leave a comment.