Arcgis地图:如何获取和显示当前位置(Android)

时间:2013-11-19 02:52:22

标签: java android map arcgis esri

有谁知道如何在一张地图上获取当前位置。获取当前位置后,它会在地图上显示一个点或任何一个点,告诉用户他或她位于gps可以检测到的位置并将其显示在地图上

是否有获取当前位置或附近的示例代码?

我在arcgis地图中相当新,并且我已经花了超过1周的时间来坚持如何获得当前位置。 :(

我只想提前感谢那些愿意分享的人。 C:

2 个答案:

答案 0 :(得分:1)

Esri ArcGIS SDK v100.x.x Android 简单代码段,可通过实时导航(使用Java / Kotlin)启用当前位置。

这已使用构建工具29.0.2,kotlin_version 1.3.70,Java 8和Gradle 3.6.1在Android Studio 3.6.1上成功运行。

科特林代码,使ArcGIS MapView能够自动显示实时当前位置:

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

等效的Java代码:

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

添加清单权限:

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

获取当前位置坐标(纬度)并放大:

    // 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}")
                }
    }

重要说明:

  1. 在这里,我们在Android Studio上通过Java / Kotlin使用Android Esri ArcGIS v100.7.x

  2. 该应用应该已授予上述两个权限。

  3. 该设备应启用GPS_PROVIDER。除了NETWORK_PROVIDER之外,您还可以使用GPS_PROVIDER

  4. 而不是LocationDisplay.AutoPanMode.NAVIGATION尝试使用其他选项来最好地满足您的使用情况。

希望有帮助。

答案 1 :(得分:-1)

使用类似的东西:

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

MyLocation类本身如下所示:

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);
    }
}