当前位置: 首页 > 工具软件 > OSMDroid > 使用案例 >

OSMDroid(2)—— 地图基本操作

扈沛
2023-12-01

OSMDroid支持当前基本的地图操作

一般地图的基本操作就是

 

·初始化地图

·按键放大缩小地图

·双指放大缩小地图

·双指旋转地图

·初始化地图放缩等级

·显示地图指南针

·显示网格图层

初始化地图中心

/**

* 基础操作

*/

IMapController mapController = map.getController();

//初始化地图中心

mapController.setCenter(new GeoPoint(25.1,121.1));

//设置放缩等级

mapController.setZoom(14.1);

//双指旋转

map.setMultiTouchControls(false);

//双指旋转

RotationGestureOverlay mRotationGestu = new RotationGestureOverlay(map);

map.getOverlays().add(mRotationGestu);

//指南针

CompassOverlay  compassOverlay= new CompassOverlay(getApplicationContext(), new InternalCompassOrientationProvider(getApplicationContext()), map);

compassOverlay.enableCompass();

map.getOverlays().add(compassOverlay);

onResume();

//网格图层

LatLonGridlineOverlay2 latLonGridlineOverlay2 = new LatLonGridlineOverlay2();

map.getOverlays().add(latLonGridlineOverlay2);

/**

* 添加ItemOverlay

*/

ArrayList<OverlayItem> overlayItems = new ArrayList<>();

OverlayItem overlayItem = new OverlayItem("itemizedoverlay","overlayitem",new GeoPoint(21.1111,121.1111));

overlayItem.setMarker(getDrawable(R.drawable.marker_default));

overlayItems.add(overlayItem);

ItemizedOverlayWithFocus itemizedOverlayWithFocus = new ItemizedOverlayWithFocus(overlayItems,

        new ItemizedIconOverlay.OnItemGestureListener<OverlayItem>() {

            @Override

            public boolean onItemSingleTapUp(final int index, final OverlayItem item) {

                return true;

            }

            @Override

            public boolean onItemLongPress(final int index, final OverlayItem item) {

                return false;

            }

        }, getApplicationContext());

map.getOverlays().add(itemizedOverlayWithFocus);



/**

* 添加线

*/

Polyline polyline = new Polyline();

polyline.setPoints(linepoints);

map.getOverlays().add(polyline);

/**

* 添加面

*/

Polygon polygon = new Polygon();

polygon.setPoints(linepoints);

map.getOverlays().add(polygon);

MainAcivity代码


public class MainActivity extends Activity {

    MapView map = null;

    @Override public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

//先加载OSM的配置

        Context ctx = getApplicationContext();

        Configuration.getInstance().load(ctx,PreferenceManager.getDefaultSharedPreferences(ctx));

//首先确保存储权限已经开启,在手机权限管理中打开本软件的存储读写权限

//加载视图

        setContentView(R.layout.activity_main);

        map = (MapView) findViewById(R.id.map);

        map.setTileSource(TileSourceFactory.MAPNIK);

        /**

         * 基础操作

         */

        IMapController mapController = map.getController();

        //初始化地图中心

        mapController.setCenter(new GeoPoint(21.1111,121.1111));

        //设置放缩等级

        mapController.setZoom(14.1);

        //双指旋转

        map.setMultiTouchControls(false);

        //双指旋转

        RotationGestureOverlay mRotationGestu = new RotationGestureOverlay(map);

        map.getOverlays().add(mRotationGestu);

        //指南针

        CompassOverlay  compassOverlay= new CompassOverlay(getApplicationContext(), new InternalCompassOrientationProvider(getApplicationContext()), map);

        compassOverlay.enableCompass();

        map.getOverlays().add(compassOverlay);

        onResume();

        //网格图层

        LatLonGridlineOverlay2 latLonGridlineOverlay2 = new LatLonGridlineOverlay2();

        map.getOverlays().add(latLonGridlineOverlay2);

        /**

         * 添加ItemOverlay

         */

        ArrayList<OverlayItem> overlayItems = new ArrayList<>();

        OverlayItem overlayItem = new OverlayItem("itemizedoverlay","overlayitem",new GeoPoint(21.1111,121.1111));

        overlayItem.setMarker(getDrawable(R.drawable.marker_default));

        overlayItems.add(overlayItem);

        ItemizedOverlayWithFocus itemizedOverlayWithFocus = new ItemizedOverlayWithFocus(overlayItems,

                new ItemizedIconOverlay.OnItemGestureListener<OverlayItem>() {

                    @Override

                    public boolean onItemSingleTapUp(final int index, final OverlayItem item) {

                        return true;

                    }

                    @Override

                    public boolean onItemLongPress(final int index, final OverlayItem item) {

                        return false;

                    }

                }, getApplicationContext());

        map.getOverlays().add(itemizedOverlayWithFocus);

        /**

         * 添加线和面需要的点坐标

         */

        GeoPoint point1 = new GeoPoint(21.1111,121.1111);

        GeoPoint point2 = new GeoPoint(21.2345,121.3456);

        GeoPoint point3 = new GeoPoint(21.9876,121.9876);

        GeoPoint point4 = new GeoPoint(21.4444,121.4444);

        ArrayList<GeoPoint> linepoints = new ArrayList<>();

        linepoints.add(point1);

        linepoints.add(point2);

        linepoints.add(point3);

        linepoints.add(point4);

        /**

         * 添加线

         */

        Polyline polyline = new Polyline();

        polyline.setPoints(linepoints);

        map.getOverlays().add(polyline);

        /**

         * 添加面

         */

        Polygon polygon = new Polygon();

        polygon.setPoints(linepoints);

        map.getOverlays().add(polygon);

    }

    public void onResume(){

        super.onResume();

//Acivity声明周期OnResume

//可以使用sharedpreference用来保存配置,当配置发生改变的时候可以使用如下语句

//SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);

//Configuration.getInstance().load(this, PreferenceManager.getDefaultSharedPreferences(this));

        map.onResume(); //needed for compass, my location overlays, v6.0.0 and up

    }

    public void onPause(){

        super.onPause();

//Acivity声明周期OnPause

        //可以使用sharedpreference用来保存配置,当配置发生改变的时候可以使用如下语句

//SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);

//Configuration.getInstance().save(this, prefs);

        map.onPause(); //needed for compass, my location overlays, v6.0.0 and up

    }

 

 

 类似资料: