So after spending WAY too much time looking for a simple tutorial to show how to add a device’s location to an Android App using the ArcGIS Android SDK, I decided to write one. This search caused me a lot of frustration and I imagine other new Android programmers have encountered the same thing. Follow these simple steps to add a device’s location to an Android App.
I added the project files to a repository on GitHub for your viewing pleasure. Be warned, the app is only written to work on Android 4.2 devices- you’ll need to change the minimum version in the manifest to run it on older devices.
I apologize for the code formatting. To see the code in its entire formatted beauty, check out the .java file on github
PS If a tutorial/resource exists that shows how to simply add the location to an app, please share!
To show the devices current location on a map, you need to utilize the [<p style="text-align: center;"> </p>
So after spending WAY too much time looking for a simple tutorial to show how to add a device’s location to an Android App using the ArcGIS Android SDK, I decided to write one. This search caused me a lot of frustration and I imagine other new Android programmers have encountered the same thing. Follow these simple steps to add a device’s location to an Android App.
I added the project files to a repository on GitHub for your viewing pleasure. Be warned, the app is only written to work on Android 4.2 devices- you’ll need to change the minimum version in the manifest to run it on older devices.
I apologize for the code formatting. To see the code in its entire formatted beauty, check out the .java file on github
PS If a tutorial/resource exists that shows how to simply add the location to an app, please share!
To show the devices current location on a map, you need to utilize the](https://developers.arcgis.com/en/android/api-reference/reference/com/esri/android/map/LocationService.html) class.
First of all, define the MapView in the standard onCreate method, and add the basemap (and any other layers):
public void onCreate(Bundle savedInstanceState) {</code> super.onCreate(savedInstanceState); setContentView(R.layout.main); mMapView = (MapView) findViewById(R.id.map); mMapView.addLayer(new ArcGISTiledMapServiceLayer(
"http://services.arcgisonline.com/ArcGIS/rest/services/
World_Street_Map/MapServer"));
After the MapView has been created, add a setOnStatusChangedListener to it. Inside this method is where we will establish the LocationService via a new OnStatusChangedListener and a method called onStatusChanged.
mMapView.setOnStatusChangedListener(new OnStatusChangedListener() {</code>
public void onStatusChanged(Object source, STATUS status) {
if (source == mMapView && status == STATUS.INITIALIZED) {
LocationService ls = mMapView.getLocationService();
ls.setAutoPan(false);
ls.start();
}
}
});
Here is the full java (main) activity code to add your device’s location to your android application:
package com.fuscoe.locationOnMapPractice; import android.app.Activity; import android.location.Location; import android.location.LocationListener; import android.os.Bundle; import com.esri.android.map.LocationService; import com.esri.android.map.MapView; import com.esri.android.map.ags.ArcGISTiledMapServiceLayer; import com.esri.android.map.event.OnStatusChangedListener; public class LocationOnMapPracticeActivity extends Activity { MapView mMapView; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mMapView = (MapView) findViewById(R.id.map); mMapView.addLayer(new ArcGISTiledMapServiceLayer( "http://services.arcgisonline.com/ArcGIS/rest/services/
World_Street_Map/MapServer")); mMapView.setOnStatusChangedListener(
new OnStatusChangedListener() { public void onStatusChanged(Object source, STATUS status) { if (source == mMapView && status == STATUS.INITIALIZED) { LocationService ls = mMapView.getLocationService(); ls.setAutoPan(false); ls.start(); } } }); } @Override protected void onDestroy() { super.onDestroy(); } @Override protected void onPause() { super.onPause(); mMapView.pause(); } @Override protected void onResume() { super.onResume(); mMapView.unpause(); } }