Arcgis : how to get device location

烈酒焚心 提交于 2019-12-05 10:36:14

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;

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

    }

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

You need to use your own location manager or the location client to get the device's current location and then you will have to add that point on the map.

Your map should be in a MapFragment. Get the googleMap object from the fragment and then add your custom blue dot on it.

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

    }
};

The above code gets you the location in onLocationChanged method.

Note: i have used GPS_PROVIDER to get the location. There are other ways to get the current location too.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!