0

Does anyone know how to get current location on one map. after getting your current location , it will display a dot or whatsoever on the map telling the user that he or she is at a location which the gps could detect and display it on the map

Is there any get current location or nearby sample codes?

Im quite new in arcgis map and ive been spending more than 1 week stuck in how to get the current location. :(

I just want to thank you in advance to those who are willing to share. C:

  • refer this: [Arcgis : how to get device location][1] [1]: http://stackoverflow.com/questions/20062552/arcgis-how-to-get-device-location/20063261#20063261 – Atish Agrawal Nov 19 '13 at 04:34

2 Answers2

0

Esri ArcGIS SDK v100.x.x Android simple code snippet to enable the current location with live navigation (using Java/Kotlin).

This is successfully working on Android Studio 3.6.1 using Build-Tool 29.0.2, kotlin_version 1.3.70, Java 8 and Gradle 3.6.1.

Kotlin Code to enable ArcGIS MapView to automatically show live current location:

mapView.locationDisplay.startAsync()
mapView.locationDisplay.autoPanMode = LocationDisplay.AutoPanMode.NAVIGATION

It's equivalent Java Code:

mapView.getLocationDisplay().startAsync();
mapView.getLocationDisplay().setAutoPanMode(LocationDisplay.AutoPanMode.NAVIGATION);

Add Manifest Permissions:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

Get Current Location Coordinates (Lat-Lng) and zoom on it:

    // Check if location provider enabled.
    val locationServiceManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager
    val isEnabled = locationServiceManager.isProviderEnabled(LocationManager.GPS_PROVIDER)
    if (!isEnabled) {
        // Location provide not enabled.
        Toast.makeText(this, "Enable Location Setting", Toast.LENGTH_SHORT).show()
        startActivity(Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS))
    } else {
        // Fetch valid current location provided from provider.
        if (mapView.locationDisplay != null)
            if (mapView.locationDisplay.location != null)
                if (mapView.locationDisplay.location.position != null) {
                    val point = mapView.locationDisplay.location.position
                    // Zoom to current location with magnification 1000.
                    mapView.setViewpointCenterAsync(point, 1000.0)
                    Log.d("Latitude", "${point.x}")
                    Log.d("Longitude", "${point.y}")
                }
    }

Important Notes:

  1. Here we are using Android Esri ArcGIS v100.7.x using Java/Kotlin on Android Studio.

  2. The app should have granted for the above two mentioned permission.

  3. The device should have GPS_PROVIDER enabled. You can also use NETWORK_PROVIDER in addition to GPS_PROVIDER.

  4. Instead of LocationDisplay.AutoPanMode.NAVIGATION try using other options to best meet your use case.

Hope this helped.

Rahul Raina
  • 3,322
  • 25
  • 30
-2

Use something like this:

LocationResult locationResult = new LocationResult(){
    @Override
    public void gotLocation(Location location){
        //Got the location!
    }
};
MyLocation myLocation = new MyLocation();
myLocation.getLocation(this, locationResult);

The MyLocation class itself is shown below:

import java.util.Timer;
import java.util.TimerTask;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

public class MyLocation {
    Timer timer1;
    LocationManager lm;
    LocationResult locationResult;
    boolean gps_enabled=false;
    boolean network_enabled=false;

    public boolean getLocation(Context context, LocationResult result)
    {
        //I use LocationResult callback class to pass location value from MyLocation to user code.
        locationResult=result;
        if(lm==null)
            lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

        //exceptions will be thrown if provider is not permitted.
        try{gps_enabled=lm.isProviderEnabled(LocationManager.GPS_PROVIDER);}catch(Exception ex){}
        try{network_enabled=lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);}catch(Exception ex){}

        //don't start listeners if no provider is enabled
        if(!gps_enabled && !network_enabled)
            return false;

        if(gps_enabled)
            lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListenerGps);
        if(network_enabled)
            lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListenerNetwork);
        timer1=new Timer();
        timer1.schedule(new GetLastLocation(), 20000);
        return true;
    }

    LocationListener locationListenerGps = new LocationListener() {
        public void onLocationChanged(Location location) {
            timer1.cancel();
            locationResult.gotLocation(location);
            lm.removeUpdates(this);
            lm.removeUpdates(locationListenerNetwork);
        }
        public void onProviderDisabled(String provider) {}
        public void onProviderEnabled(String provider) {}
        public void onStatusChanged(String provider, int status, Bundle extras) {}
    };

    LocationListener locationListenerNetwork = new LocationListener() {
        public void onLocationChanged(Location location) {
            timer1.cancel();
            locationResult.gotLocation(location);
            lm.removeUpdates(this);
            lm.removeUpdates(locationListenerGps);
        }
        public void onProviderDisabled(String provider) {}
        public void onProviderEnabled(String provider) {}
        public void onStatusChanged(String provider, int status, Bundle extras) {}
    };

    class GetLastLocation extends TimerTask {
        @Override
        public void run() {
             lm.removeUpdates(locationListenerGps);
             lm.removeUpdates(locationListenerNetwork);

             Location net_loc=null, gps_loc=null;
             if(gps_enabled)
                 gps_loc=lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
             if(network_enabled)
                 net_loc=lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

             //if there are both values use the latest one
             if(gps_loc!=null && net_loc!=null){
                 if(gps_loc.getTime()>net_loc.getTime())
                     locationResult.gotLocation(gps_loc);
                 else
                     locationResult.gotLocation(net_loc);
                 return;
             }

             if(gps_loc!=null){
                 locationResult.gotLocation(gps_loc);
                 return;
             }
             if(net_loc!=null){
                 locationResult.gotLocation(net_loc);
                 return;
             }
             locationResult.gotLocation(null);
        }
    }

    public static abstract class LocationResult{
        public abstract void gotLocation(Location location);
    }
}
RouteMapper
  • 2,484
  • 1
  • 26
  • 45
  • Hie, i dont really understand how to implement the codes inside my file .. For the locationResult, when i put it under my MainAcvity under mMapView.addlayer . it gives me error .. –  Nov 19 '13 at 05:16