Arcgis:如何获取设备位置 [英] Arcgis : how to get device location

查看:62
本文介绍了Arcgis:如何获取设备位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的应用程序中实现这些代码,但它不起作用,我不知道我哪里出错了.

Hie i tried to implement this codes in my application but it doesnt work , i dont know where i went wrong.

基本上,当我启动设备位置的示例时.它没有显示我当前的位置在哪里,也没有看到任何与我当前所在位置相似的蓝点.

basically, when i launch the sample of the device location. it doesnt show me where is my current location and i dont see any blue dots that resembles the current location i am at.

我唯一看到的是地图.只是一张普通的缩小地图.

the only thing that i see is the map . just a plain zoom out map.

如果有人可以帮助我了解如何使用地图上显示的蓝点获取当前位置,我将不胜感激.

I would be really thankful if someone who could help me out on how to get the current location with the blue dots that is displayed on the map..

这是我的 MainActivity.class

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

}

推荐答案

这是一个代码,它通过 provider 和 GPS 每 1 秒绘制一次我的位置.让我们首先声明变量:

this is a code that draws my location every 1 second via provider and GPS . let's first declare variables :

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

在 onCreate 函数中,我们调用 LocationListener:

in onCreate function WE CALL 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();

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

then a function to draw my location :

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 的函数:

make internal class that implements MyLocationListener to get you instant location, and let it call the function named SetMyLocationPoint like this way :

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

这篇关于Arcgis:如何获取设备位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆