【问题标题】:Arcgis map : How to get and display Current location (Android )Arcgis 地图:如何获取和显示当前位置(Android)
【发布时间】:2023-04-08 08:57:02
【问题描述】:

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

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

我是 arcgis 地图的新手,我花了超过 1 周的时间来解决如何获取当前位置的问题。 :(

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

【问题讨论】:

标签: java android map arcgis esri


【解决方案1】:

Esri ArcGIS SDK v100.x.x Android 简单的代码 sn-p 以使用实时导航启用当前位置(使用 Java/Kotlin)。

这在使用 Build-Tool 29.0.2、kotlin_version 1.3.70、Java 8 和 Gradle 3.6.1 的 Android Studio 3.6.1 上成功运行。

启用 ArcGIS MapView 自动显示实时当前位置的 Kotlin 代码:

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

获取当前位置坐标(Lat-Lng)并放大:

    // 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。除了GPS_PROVIDER,您还可以使用NETWORK_PROVIDER

  4. 请尝试使用其他选项而不是 LocationDisplay.AutoPanMode.NAVIGATION,以最好地满足您的用例。

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    使用这样的东西:

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

    【讨论】:

    • 嗨,我真的不明白如何在我的文件中实现代码。对于 locationResult,当我把它放在我的 MainAcvity 下 mMapView.addlayer 下时。它给了我错误..
    猜你喜欢
    • 2013-12-02
    • 2013-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-09
    • 2021-09-11
    • 1970-01-01
    相关资源
    最近更新 更多