《ArcGIS Runtime SDK for Android学习笔记》-6、地图容器MapView

wshunli
2016-04-12 / 0 评论 / 11 阅读 / 正在检测是否收录...

MapView 概述

MapView是ArcGIS Runtime SDK for Android的核心组件,通过MapView可以呈现地图服务的数据,并且在MapView中定义了丰富的属性、方法和事件,用户通过MapView可以操作设备的触摸屏,默认MapView可以响应用户的各类手势的操作,GIS的开发中,什么时候都少不了地图操作。MapView是Android中ViewGroup的子类,也是ArcGIS Runtime SDK for Android中的地图容器,与很多ArcGIS API中的Map、MapControl类作用是一样的。

![](https://img.wshunli.com/ArcGISforAndroid/6/地图的容器MapView1.png-hexo.png)

MapView 功能介绍

MapView 的强大不仅仅是因为它是呈现地图数据的容器,MapView 为提供丰富的功能,下面来分析 MapView 为提供哪些功能。
首先,MapView 具有呈现地图能力,MapView 可以添加一个或多个图层,图层又分很多种,如:切片图层、动态图层、本地图层等等;图层只有添加到 MapView 容器中才能进行显示。
其次,通过 MapView 可以设置地图的显示范围、是否允许被旋转、地图背景、地图的最大/最小分辨率及其指定当前显示的分辨率/比例尺。
最后,MapView 提供了丰富的手势监听接口,通过这些监听器,可以监听各种手势动作,如点击、双击、移动或长按等操作。

MapView 的使用

MapView 的添加方式

有两种方式可以将 MapView 添加到应用当中:一个是 XML 方式,另一个是硬编码方式;一般多采用 XML 方式,方便调整布局及其属性相关设置。

1、XML方式

 <com.esri.android.map.MapView xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent" 
 android:layout_height="match_parent"
 url="http://www.arcgis.com/home/webmap/viewer.html?webmap=e229d715f7ca4fa980308549fb288165"/>

2、JAVA方式

 //ArcGISTiledMapServiceLayer class is used to define the Layer that is added to the 
 //MapView object.
 MapView mMapView = new MapView(this);
 mMapView.addLayer(new ArcGISTiledMapServiceLayer(
     "http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer"));
 setContentView(mMapView);

MapView的配置

XML方式

以上内容使用XML实现

 <!-- MapView with MapOptions settings for Topo basemap,
         zoom level, and centered in Costa Mesa, CA. -->
 <com.esri.android.map.MapView
 android:id="@+id/map"
 android:layout_width="match_parent" 
 android:layout_height="match_parent"
 mapoptions.MapType="topo"
 mapoptions.ZoomLevel="13"
 mapoptions.center="33.666354, -117.903557"/>

在JAVA中以下代码即可使用

// Retrieve the MapView, Basemap, ZoomLevel, and Center from XML layout
 MapView mMapView = (MapView) findViewById(R.id.map);

JAVA方式

1、采用的MapOptions类实例化一个MapView,定义底图,缩放级别,以及中心。

 MapOptions topo = new MapOptions(MapType.TOPO, 23, -110, 9);
 MapView mapView = new MapView(this, topo);

2、使用 MapOptions 切换底图。

 MapOptions topo = new MapOptions(MapType.TOPO, 23, -110, 9);
 MapView mapView = new MapView(this, topo);

MapView 的常见操作

地图常见的操作有缩放、旋转、平移、获取范围、比例尺、分辨率等信息,以及常用的手势操作,其中,经常使用到的功能和常见问题有以下几个:

获取/设置比例尺、分辨率、中心点、范围

ArcGIS forAndroid中,MapView具有很多与地图操作有关的方法,其中,与地图的比例尺、分辨率、中心点、范围有关的方法如下:

1、获取/设置比例尺

返回类型 方法 说明
double getScale() 获得当前地图的比例尺.
double getMaxScale() 获得当前地图的最大比例尺.
double getMinScale() 获得当前地图的最小比例尺.
void setScale(double scale) 设置当前地图的比例尺.
void setMaxScale(double maxScale) 设置当前地图的最大比例尺.
void setMinScale(double minScale) 设置当前地图的最小比例尺.

2、获取/设置分辨率

返回类型 方法 说明
double getResolution() 获得当前地图的分辨率.
double getMaxResolution() 此方法在 10.2.2 以后弃用,使用 getMinScale() 替代.
double getMinResolution() 此方法在 10.2.2 以后弃用,使用 getMaxScale() 替代.
void setResolution(double res) 设置当前地图的分辨率.
void setMaxResolution(double maxResolution) 此方法在 10.2.2 以后弃用,使用 setMinScale(double) 替代.
void setMinResolution(double minResolution) 此方法在 10.2.2 以后弃用,使用 setMaxScale(double) 替代.

3、获取/设置中心点

返回类型 方法 说明
Point getCenter() 获取地图中心点.
void centerAt(Point centerPt, boolean animated) 设置地图中心点.
void centerAt(double lat, double lon, boolean animated) 设置地图中心点.

4、获取/设置范围

返回类型 方法 说明
Polygon getExtent() 返回当前可视区域范围.
Envelope getMaxExtent() 获取MapView的最大范围.
void setExtent(Geometry geometry) 设置当前地图显示的空间范围.

地图缩放、平移和旋转

1、地图缩放

返回类型 方法 说明
void zoomin() 放大地图.
void zoomout() 缩小地图.
void zoomTo(Point centerPt, float factor) 缩放到给定点.
void zoomToResolution(Point centerPt, double res) 设定中心点,缩放到给定分辨率水平.
void zoomToScale(Point centerPt, double scale) 设定中心点,缩放到指定比例尺.

2、地图平移

MapView的方法中,没有专门针对平移操作,主要原因在于,MapView中已经默认支持平移操作,即使用鼠标或手势拖动地图时就会平移地图,所以无需设置;

3、地图旋转

返回类型 方法 说明
double getRotationAngle() 返回当前地图旋转角度(单位degree).
void setRotationAngle(double degree, Point centerPt, boolean animated) 将地图旋转到指定的角度(单位degree),度数为正数则按逆时针方向旋转.中心点、动画可选.
void setRotationAngle(double degree, float pivotX, float pivotY) 以某个点为中心旋转地图.
void setAllowRotationByPinch(boolean allowRotationByPinch) 允许/取消pinch旋转.
boolean isAllowRotationByPinch() 返回是否允许pinch时旋转.

地图坐标转换

地图坐标转换主要使用下列几个方法,其中,主要使用toMapPoint()方法实现获取地图上的点坐标信息:

返回类型 方法 说明
SpatialReference getSpatialReference() 返回地图的坐标系统.
Point toMapPoint(float screenx, float screeny) 将屏幕坐标转换成地图坐标系下的ArcGIS geometry Point坐标.
Point toMapPoint(Point src) 将屏幕坐标转换成地图坐标系下的ArcGIS geometry Point坐标.
Point toScreenPoint(Point src) 将地图坐标系下的ArcGIS geometry Point坐标转换成屏幕坐标.

地图手势操作

默认情况下的MapView将下面的手势作出回应:

  • 单指双击和双指手势出来会放大到地图。
  • 双指双击和捏合手势将缩小地图。
  • 单指拖动和单击快速滑动手势会平移地图。
  • 双击后拖动手势将允许进/出地图的缩放持续。
返回类型 方法 说明
OnLongPressListener getOnLongPressListener() 获取地图长按事件监听.
OnPanListener getOnPanListener() 获取地图平移事件监听.
OnPinchListener getOnPinchListener() 获取地图捏夹事件监听.
OnSingleTapListener getOnSingleTapListener() 获取地图单击事件监听.
OnZoomListener getOnZoomListener() 获取缩放监听.
void setOnLongPressListener(OnLongPressListener onLongPressListener) 设置地图长按事件监听.
void setOnPanListener(OnPanListener onPanListener) 设置地图平移事件监听.
void setOnPinchListener(OnPinchListener onPinchListener) 设置地图捏夹事件监听.
void setOnSingleTapListener(OnSingleTapListener onSingleTapListener) 设置地图单击事件监听.
void setOnZoomListener(OnZoomListener onZoomListener) 设置缩放监听.

MapView 的方法

MapView的所有公有方法及描述。

返回类型 方法 说明
int addLayer(Layer layer, int index) Adds the child Layer at the given index.
int addLayer(Layer layer) Adds the child Layer.
void addLayers(Layer[] layerArray) Adds the Layer array.
void centerAndZoom(double lat, double lon, float levelOrFactor) If the MapView is initialized, centers the map at the given latitude and longitude and zoom the map based on the given factor.
void centerAt(double lat, double lon, boolean animated) If the MapView is initialized, centers the map at the given latitude and longitude; optionally, the change is animated.
void centerAt(Point centerPt, boolean animated) If the MapView is initialized, centers the map at the given point; optionally, the change is animated.
Bitmap createSymbolImage(Symbol symbol, Geometry geometry, int width, int height, int color) This method was deprecated in API level . at 10.2.2 use getLegendImage(Symbol, Geometry, int, int, int) instead.
Bitmap[] createSymbolImages(Symbol[] symbol, Geometry[] geometry, int width, int height, int color) This method was deprecated in API level . at 10.2.2 use getLegendImages(Symbol[], Geometry[], int, int, int) instead.
void enableWrapAround(boolean enable) Enable or disable the map wrap around feature.
ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs)
Callout getCallout() Returns a callout window.
Point getCenter() Returns the center of the MapView as an ArcGIS geometry Point.
Bitmap getDrawingMapCache(float x, float y, int w, int h) Creates a drawing cache of the map in the given screen coordinates.
Polygon getExtent() Returns a polygon comprising of four corners of map in map coordinates.
Grid getGrid() Returns the Grid instance of the map which allows for the type and visibility of the grid to be controlled.
Layer getLayer(int index) Gets a layer at the given index.
Layer getLayerByID(long layerID) Gets a layer by its ID.
Layer getLayerByURL(String url) Gets a layer by its service URL.
Layer[] getLayers() Returns all child Layers that are added to the MapView.
LocationDisplayManager getLocationDisplayManager() Returns the LocationDisplayManager.
Envelope getMapBoundaryExtent() This method was deprecated in API level . in 10.2.2, use getMaxExtent() instead
Envelope getMaxExtent() Returns the max extent of the map.
double getMaxResolution() This method was deprecated in API level . at 10.2.2 please use getMinScale()
double getMaxScale() Returns maximum scale.
double getMinResolution() This method was deprecated in API level . 10.2.2 please use getMaxScale()
double getMinScale() Returns the minimum scale.
OnLongPressListener getOnLongPressListener() Gets the OnLongPressListener of the MapView.
OnPanListener getOnPanListener() Gets the OnPanListener of the MapView.
OnPinchListener getOnPinchListener() Gets the OnPinchListener of the MapView.
OnSingleTapListener getOnSingleTapListener() Gets the onSingleTapListener of the MapView.
OnStatusChangedListener getOnStatusChangedListener() Gets the OnStatusChangedListener of the MapView.
OnZoomListener getOnZoomListener() Gets the OnZoomListener of the MapView.
double getResolution() Returns the resolution of the MapView.
double getRotationAngle() Returns the current rotation angle of the map in degrees counter-clockwise.
double getScale() A convenience method for obtaining the current map scale .
SpatialReference getSpatialReference() Returns the spatial coordinate system being used by the MapView.
boolean isAllowOneFingerZoom() Returns true if one-finger zooming is allowed.
boolean isAllowRotationByPinch() Returns true if the rotation by pinch is allowed.
boolean isLoaded() Returns true if the MapView is initialized.
boolean isRecycled() Returns true if the MapView has been recycled.
boolean isShowMagnifierOnLongPress() This method checks whether the map will show the magnifier when the user performs a tap and hold gesture on the map.
void onChildViewAdded(View parent, View child)
void onChildViewRemoved(View parent, View child)
void pause() Pauses the map.
void recycle() Releases the resources referenced by the MapView so that they can be recycled.
void removeAll() Removes all child layers from the map.
void removeLayer(int index) Removes a child layer at the given index.
void removeLayer(Layer layer) Removes the child Layer from the parent MapView.
void restoreState(String state) Restores the center and resolution, and the LocationDisplayManager state (whether it was active and whether or not to autopan) from the given state String into the MapView.
String retainState() Saves the coordinates of the center of the map, the current map resolution, and the LocationDisplayManager state (whether it was active and whether or not to autopan) as a String.
void setAllowMagnifierToPanMap(boolean allow) Flag to indicate if the map should pan when the magnifier gets near the edge of the map's bounds.
void setAllowOneFingerZoom(boolean allowOneFingerZoom) Allows/disallows one-finger zooming.
void setAllowRotationByPinch(boolean allowRotationByPinch) Allows/disallow pinch to rotate.
void setEsriLogoVisible(boolean visible) Turns on or off the ESRI logo.
void setExtent(Geometry geometry, int padding) Zooms the map to the given geometry, so that geometry fits within the bounds of the map with the specified padding between the geometry and edge of the map.
void setExtent(Geometry geometry) Zooms the map to the given geometry so that geometry fits within the bounds of the map.
void setExtent(Geometry geometry, int padding, boolean animated) Zooms the map to the given geometry, so that geometry fits within the bounds of the map with the specified padding between the geometry and edge of the map; optionally, the change is animated.
void setMapBackground(int bkColor, int gridColor, float gridSize, float gridLineSize) Sets the map background with color and grid.
boolean setMapOptions(MapOptions options) Switches basemap by the given options.
void setMaxExtent(Envelope env) Sets the boundary extent of the map.
void setMaxResolution(double maxResolution) This method was deprecated in API level . at 10.2.2 please use setMinScale(double)
void setMaxScale(double maxScale) Sets the maximum scale to which the map can be zoomed in.
void setMinResolution(double minResolution) This method was deprecated in API level . at 10.2.2, please use setMaxScale(double)
void setMinScale(double minScale) Set the minimum scale to which the map can be zoomed out.
void setOnLongPressListener(OnLongPressListener onLongPressListener) Sets the OnLongPressListener of the MapView.
void setOnPanListener(OnPanListener onPanListener) Sets the OnPanListener of the MapView.
void setOnPinchListener(OnPinchListener onPinchListener) Sets the onPinchListener of the MapView.
void setOnSingleTapListener(OnSingleTapListener onSingleTapListener) Sets the onSingleTapListener of the MapView.
void setOnStatusChangedListener(OnStatusChangedListener onStatusChangedListener) Sets the OnStatusChangedListener of the MapView.
void setOnZoomListener(OnZoomListener onZoomListener) Sets the OnZoomListener of the MapView.
void setResolution(double res) Sets the resolution of the MapView.
void setRotationAngle(double degree, Point centerPt, boolean animated) Rotates the map to the given angle in degrees and re-centers the map at the specified map coordinates; optionally, the change is animated.
void setRotationAngle(double degree, boolean animated) Rotates the map to the given angle in degrees; optionally, the change is animated.
void setRotationAngle(double degree) Rotates the map to the given angle in degrees; if the specified angle is positive, rotation is counter-clockwise, if negative then rotation is clockwise.
void setRotationAngle(double degree, float pivotX, float pivotY) Rotates the map to the given angle in degrees, pivoting the rotation around a given point on the screen.
void setScale(double scale, boolean animated) Sets the map scale; optionally, the change is animated.
void setScale(double scale) Sets the map scale.
void setShowMagnifierOnLongPress(boolean showMagnifier) Sets a flag that defines whether or not the map should show the magnifier when the user performs a long press gesture on the map.
Future switchBaseMap(BaseMap basemap, Portal portal, OnBaseMapSwitchListener listener) Replaces the basemap layers of this MapView if it was created from a WebMap, otherwise just adds the new basemap layers at the bottom or top of the existing MapView layers, depending on whether they are base or reference layers.
Point toMapPoint(float screenx, float screeny) A convenience method that will convert a device's screen coordinates to an ArcGIS geometry Point that has the same spatial coordinate system as the MapView's.
Point toMapPoint(Point src) A convenience method that will convert a device's screen coordinates into an ArcGIS geometry Point that has the same spatial coordinate system as the MapView's.
Point toScreenPoint(Point src) A convenience method that will convert an ArcGIS geometry Point from the MapView's spatial coordinate system into the device's screen coordinates.
void unpause() Unpauses the map.
void zoomTo(Point centerPt, float factor) If the MapView is initialized, zooms the map by a factor to the given center point.
void zoomToResolution(Point centerPt, double res) Centers the map on the given point and zoom into the given resolution level.
void zoomToScale(Point centerPt, double scale) Centers the map on the given point and zoom into the given scale level.
void zoomin() Zooms in one level from current map resolution; the map animates to the new resolution.
void zoomin(boolean animated) Zooms in one level from current map resolution; optionally, the change is animated.
void zoomout() Zooms out one level from current map resolution; the map animates to the new resolution.
void zoomout(boolean animated) Zooms out one level from current map resolution; optionally, the change is animated.

参考资料
1、MapView | ArcGIS Android 10.2.7 API
https://developers.arcgis.com/android/api-reference/reference/com/esri/android/map/MapView.html
2、ArcGIS for Android地图控件的5大常见操作
http://blog.csdn.net/arcgis_mobile/article/details/7801467
3、《ArcGIS Runtime SDK for Android开发笔记》——(9)、空间数据的容器-地图MapView - gis-luq - 博客园
http://www.cnblogs.com/gis-luq/p/4941715.html
4、arcgis for android 学习 - (4) 了解mapView的一些方法和事件 - 张云飞VIR - 博客园
http://www.cnblogs.com/vir56k/archive/2012/06/04/2534418.html

0

评论 (0)

取消