【问题标题】:Arcgis : how to get device locationArcgis:如何获取设备位置
【发布时间】:2013-12-02 11:23:43
【问题描述】:

您好,我尝试在我的应用程序中实现此代码,但它不起作用,我不知道我哪里出错了。

基本上,当我启动设备位置的示例时。它没有显示我当前的位置在哪里,并且我没有看到任何类似于我当前位置的蓝点。

我唯一看到的是地图。只是一个简单的缩小地图。

如果有人能帮助我了解如何使用地图上显示的蓝点获取当前位置,我将非常感激。

这是我的 MainActivity.class

public class HelloWorld extends Activity {
MapView mMapView = null;
ArcGISTiledMapServiceLayer tileLayer;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    // Retrieve the map and initial extent from XML layout


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

           }

         });



    }


protected void onPause() {
    super.onPause();
    mMapView.pause();
   }

@Override
protected void onResume() {
    super.onResume(); 
    mMapView.unpause();
}   

}

【问题讨论】:

    标签: android map arcgis esri


    【解决方案1】:

    您需要使用自己的位置管理器或位置客户端来获取设备的当前位置,然后您必须在地图上添加该点。

    您的地图应位于MapFragment 中。 从片段中获取googleMap 对象,然后在其上添加您的自定义蓝点。

    LocationManager locationManager = (LocationManager) getApplicationContext()
                .getSystemService(Context.LOCATION_SERVICE);
    
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                5000, 5, listener);
    
    }
    
    private LocationListener listener = new LocationListener() {
    
        @Override
        public void onLocationChanged(Location location) {
            // TODO Auto-generated method stub
    
            Log.e("Google", "Location Changed");
    
            if (location == null)
                return;
            Log.e("latitude", location.getLatitude() + "");
            Log.e("longitude", location.getLongitude() + "");
    
            }
    
        }
    
        @Override
        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub
    
        }
    
        @Override
        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub
    
        }
    
        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub
    
        }
    };
    

    上面的代码在onLocationChanged方法中为你获取了位置。

    注意:我使用 GPS_PROVIDER 来获取位置。 还有其他方法可以获取当前位置。

    【讨论】:

    • 我正在使用 arcgis 地图,我尝试了各种代码,但我仍然无法在 arcgis 地图上获取当前位置..
    • 对不起,我的信息有限。我会投票赞成你的问题,以便引起人们的注意
    • @FaridAvesko,您找到解决方案了吗? .
    【解决方案2】:

    这是一个通过提供商和 GPS 每 1 秒绘制一次我的位置的代码。 让我们首先声明变量:

    private GraphicsLayer myGraphicalLayer;
    MapView mMapView;
        ArcGISLocalTiledLayer baseLayer;
    private LocationManager mlocManager;
        private LocationListener mlocListener;
    

    在 onCreate 函数中我们调用 LocationListener:

    mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            mlocListener = new MyLocationListener();
            mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, mlocListener);
            mlocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, mlocListener);
    // loading the map
            mMapView = (MapView) findViewById(R.id.localMap);
            baseLayer = new ArcGISLocalTiledLayer(basemapurl);
            mMapView.addLayer(baseLayer);
    // defining my position layer
            myGraphicalLayer = new GraphicsLayer();
    

    然后是绘制我的位置的函数:

    private void SetMyLocationPoint(final double x, final double y) {
            PictureMarkerSymbol myPin = new PictureMarkerSymbol(getResources().getDrawable(
                    R.drawable.mylocation_icon));
    
            Point wgspoint = new Point(x, y);
            Point mapPoint = (Point) GeometryEngine.project(wgspoint, SpatialReference.create(4326),
                    mMapView.getSpatialReference());
    
            Graphic myPinGraphic = new Graphic(mapPoint, myPin);
    
            try {
                myGraphicalLayer.removeAll();
            } catch (Exception e) {
                e.printStackTrace();
            }
    
            myGraphicalLayer.addGraphic(myPinGraphic);
            myGraphicalLayer.setVisible(true);
            mMapView.addLayer(myGraphicalLayer);
    
        }
    

    制作实现 MyLocationListener 的内部类来获取即时位置,并让它像这样调用名为 SetMyLocationPoint 的函数:

    public class MyLocationListener implements LocationListener {
    
            @Override
            public void onLocationChanged(Location loc) {
                SetMyLocationPoint(loc.getLongitude(), loc.getLatitude());
            }
    
            @Override
            public void onProviderDisabled(String provider) {
                Toast.makeText(getApplicationContext(), "provider enabled", Toast.LENGTH_SHORT)
                        .show();
            }
    
            @Override
            public void onProviderEnabled(String provider) {
                Toast.makeText(getApplicationContext(), "provider disabled", Toast.LENGTH_SHORT)
                        .show();
            }
    
            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-11
      • 1970-01-01
      • 2015-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多