Android GIS开发系列-- 入门季(10) MapView快速定位到Geometry

我们知道某个Geometry的坐标,但不知道具体的位置,该如何使地图快速定位呢?这时需要用到MapView.setExtent方法,来看下这个方法的介绍:Zooms the map to the given geometry so that geometry fits within the bounds of the map.大体的意思即缩小地图定位到Geometry。上代码:

public class MainActivity extends Activity {

    private MapView mapView;
    private GraphicsLayer graphicsLayer;
    private static final String TILED_WORLD_STREETS_URL = "http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mapView = (MapView) findViewById(R.id.map_view);
        mapView.addLayer(new ArcGISTiledMapServiceLayer(TILED_WORLD_STREETS_URL));

        findViewById(R.id.location).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                //快速定位到线
                Polyline polyline = new Polyline();
                polyline.startPath(new Point(110, 23));
                polyline.lineTo(new Point(115, 25));
                //由于加载的地图是墨卡托坐标,这里需要转坐标
                Polyline polyline2 = (Polyline) GeometryEngine.project(polyline,
                        SpatialReference.create(SpatialReference.WKID_WGS84),
                        SpatialReference
                                .create(SpatialReference.WKID_WGS84_WEB_MERCATOR));
                mapView.setExtent(polyline2);
            }
        });
        initLayer();
    }


    /**
     * 添加一条线
     */
    private void initLayer() {
        graphicsLayer = new GraphicsLayer();
        mapView.addLayer(graphicsLayer);
        Polyline polyline = new Polyline();
        polyline.startPath(new Point(110, 23));
        polyline.lineTo(new Point(115, 25));
        //由于加载的地图是墨卡托坐标,这里需要转坐标
        Polyline polyline2 = (Polyline) GeometryEngine.project(polyline,
                SpatialReference.create(SpatialReference.WKID_WGS84),
                SpatialReference
                        .create(SpatialReference.WKID_WGS84_WEB_MERCATOR));
        graphicsLayer.addGraphic(new Graphic(polyline2, new SimpleLineSymbol(
                Color.RED, 3, SimpleLineSymbol.STYLE.SOLID)));
    }



}

定位效果图

 

posted @ 2017-10-29 23:42  wenglabs  阅读(1283)  评论(0编辑  收藏  举报