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

Author Avatar
wshunli 4月 12, 2016
  • 在其它设备中阅读本文章

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、获取/设置比例尺

返回类型方法说明
doublegetScale()获得当前地图的比例尺.
doublegetMaxScale()获得当前地图的最大比例尺.
doublegetMinScale()获得当前地图的最小比例尺.
voidsetScale(double scale)设置当前地图的比例尺.
voidsetMaxScale(double maxScale)设置当前地图的最大比例尺.
voidsetMinScale(double minScale)设置当前地图的最小比例尺.

2、获取/设置分辨率

返回类型方法说明
doublegetResolution()获得当前地图的分辨率.
doublegetMaxResolution()此方法在 10.2.2 以后弃用,使用 getMinScale() 替代.
doublegetMinResolution()此方法在 10.2.2 以后弃用,使用 getMaxScale() 替代.
voidsetResolution(double res)设置当前地图的分辨率.
voidsetMaxResolution(double maxResolution)此方法在 10.2.2 以后弃用,使用 setMinScale(double) 替代.
voidsetMinResolution(double minResolution)此方法在 10.2.2 以后弃用,使用 setMaxScale(double) 替代.

3、获取/设置中心点

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

4、获取/设置范围

返回类型方法说明
PolygongetExtent()返回当前可视区域范围.
EnvelopegetMaxExtent()获取MapView的最大范围.
voidsetExtent(Geometry geometry)设置当前地图显示的空间范围.

地图缩放、平移和旋转

1、地图缩放

返回类型方法说明
voidzoomin()放大地图.
voidzoomout()缩小地图.
voidzoomTo(Point centerPt, float factor)缩放到给定点.
voidzoomToResolution(Point centerPt, double res)设定中心点,缩放到给定分辨率水平.
voidzoomToScale(Point centerPt, double scale)设定中心点,缩放到指定比例尺.

2、地图平移

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

3、地图旋转

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

地图坐标转换

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

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

地图手势操作

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

  • 单指双击和双指手势出来会放大到地图。
  • 双指双击和捏合手势将缩小地图。
  • 单指拖动和单击快速滑动手势会平移地图。
  • 双击后拖动手势将允许进/出地图的缩放持续。
返回类型方法说明
OnLongPressListenergetOnLongPressListener()获取地图长按事件监听.
OnPanListenergetOnPanListener()获取地图平移事件监听.
OnPinchListenergetOnPinchListener()获取地图捏夹事件监听.
OnSingleTapListenergetOnSingleTapListener()获取地图单击事件监听.
OnZoomListenergetOnZoomListener()获取缩放监听.
voidsetOnLongPressListener(OnLongPressListener onLongPressListener)设置地图长按事件监听.
voidsetOnPanListener(OnPanListener onPanListener)设置地图平移事件监听.
voidsetOnPinchListener(OnPinchListener onPinchListener)设置地图捏夹事件监听.
voidsetOnSingleTapListener(OnSingleTapListener onSingleTapListener)设置地图单击事件监听.
voidsetOnZoomListener(OnZoomListener onZoomListener)设置缩放监听.

MapView 的方法

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

返回类型方法说明
intaddLayer(Layer layer, int index)Adds the child Layer at the given index.
intaddLayer(Layer layer)Adds the child Layer.
voidaddLayers(Layer[] layerArray)Adds the Layer array.
voidcenterAndZoom(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.
voidcenterAt(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.
voidcenterAt(Point centerPt, boolean animated)If the MapView is initialized, centers the map at the given point; optionally, the change is animated.
BitmapcreateSymbolImage(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.
voidenableWrapAround(boolean enable)Enable or disable the map wrap around feature.
ViewGroup.LayoutParamsgenerateLayoutParams(AttributeSet attrs)
CalloutgetCallout()Returns a callout window.
PointgetCenter()Returns the center of the MapView as an ArcGIS geometry Point.
BitmapgetDrawingMapCache(float x, float y, int w, int h)Creates a drawing cache of the map in the given screen coordinates.
PolygongetExtent()Returns a polygon comprising of four corners of map in map coordinates.
GridgetGrid()Returns the Grid instance of the map which allows for the type and visibility of the grid to be controlled.
LayergetLayer(int index)Gets a layer at the given index.
LayergetLayerByID(long layerID)Gets a layer by its ID.
LayergetLayerByURL(String url)Gets a layer by its service URL.
Layer[]getLayers()Returns all child Layers that are added to the MapView.
LocationDisplayManagergetLocationDisplayManager()Returns the LocationDisplayManager.
EnvelopegetMapBoundaryExtent()This method was deprecated in API level . in 10.2.2, use getMaxExtent() instead
EnvelopegetMaxExtent()Returns the max extent of the map.
doublegetMaxResolution()This method was deprecated in API level . at 10.2.2 please use getMinScale()
doublegetMaxScale()Returns maximum scale.
doublegetMinResolution()This method was deprecated in API level . 10.2.2 please use getMaxScale()
doublegetMinScale()Returns the minimum scale.
OnLongPressListenergetOnLongPressListener()Gets the OnLongPressListener of the MapView.
OnPanListenergetOnPanListener()Gets the OnPanListener of the MapView.
OnPinchListenergetOnPinchListener()Gets the OnPinchListener of the MapView.
OnSingleTapListenergetOnSingleTapListener()Gets the onSingleTapListener of the MapView.
OnStatusChangedListenergetOnStatusChangedListener()Gets the OnStatusChangedListener of the MapView.
OnZoomListenergetOnZoomListener()Gets the OnZoomListener of the MapView.
doublegetResolution()Returns the resolution of the MapView.
doublegetRotationAngle()Returns the current rotation angle of the map in degrees counter-clockwise.
doublegetScale()A convenience method for obtaining the current map scale .
SpatialReferencegetSpatialReference()Returns the spatial coordinate system being used by the MapView.
booleanisAllowOneFingerZoom()Returns true if one-finger zooming is allowed.
booleanisAllowRotationByPinch()Returns true if the rotation by pinch is allowed.
booleanisLoaded()Returns true if the MapView is initialized.
booleanisRecycled()Returns true if the MapView has been recycled.
booleanisShowMagnifierOnLongPress()This method checks whether the map will show the magnifier when the user performs a tap and hold gesture on the map.
voidonChildViewAdded(View parent, View child)
voidonChildViewRemoved(View parent, View child)
voidpause()Pauses the map.
voidrecycle()Releases the resources referenced by the MapView so that they can be recycled.
voidremoveAll()Removes all child layers from the map.
voidremoveLayer(int index)Removes a child layer at the given index.
voidremoveLayer(Layer layer)Removes the child Layer from the parent MapView.
voidrestoreState(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.
StringretainState()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.
voidsetAllowMagnifierToPanMap(boolean allow)Flag to indicate if the map should pan when the magnifier gets near the edge of the map’s bounds.
voidsetAllowOneFingerZoom(boolean allowOneFingerZoom)Allows/disallows one-finger zooming.
voidsetAllowRotationByPinch(boolean allowRotationByPinch)Allows/disallow pinch to rotate.
voidsetEsriLogoVisible(boolean visible)Turns on or off the ESRI logo.
voidsetExtent(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.
voidsetExtent(Geometry geometry)Zooms the map to the given geometry so that geometry fits within the bounds of the map.
voidsetExtent(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.
voidsetMapBackground(int bkColor, int gridColor, float gridSize, float gridLineSize)Sets the map background with color and grid.
booleansetMapOptions(MapOptions options)Switches basemap by the given options.
voidsetMaxExtent(Envelope env)Sets the boundary extent of the map.
voidsetMaxResolution(double maxResolution)This method was deprecated in API level . at 10.2.2 please use setMinScale(double)
voidsetMaxScale(double maxScale)Sets the maximum scale to which the map can be zoomed in.
voidsetMinResolution(double minResolution)This method was deprecated in API level . at 10.2.2, please use setMaxScale(double)
voidsetMinScale(double minScale)Set the minimum scale to which the map can be zoomed out.
voidsetOnLongPressListener(OnLongPressListener onLongPressListener)Sets the OnLongPressListener of the MapView.
voidsetOnPanListener(OnPanListener onPanListener)Sets the OnPanListener of the MapView.
voidsetOnPinchListener(OnPinchListener onPinchListener)Sets the onPinchListener of the MapView.
voidsetOnSingleTapListener(OnSingleTapListener onSingleTapListener)Sets the onSingleTapListener of the MapView.
voidsetOnStatusChangedListener(OnStatusChangedListener onStatusChangedListener)Sets the OnStatusChangedListener of the MapView.
voidsetOnZoomListener(OnZoomListener onZoomListener)Sets the OnZoomListener of the MapView.
voidsetResolution(double res)Sets the resolution of the MapView.
voidsetRotationAngle(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.
voidsetRotationAngle(double degree, boolean animated)Rotates the map to the given angle in degrees; optionally, the change is animated.
voidsetRotationAngle(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.
voidsetRotationAngle(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.
voidsetScale(double scale, boolean animated)Sets the map scale; optionally, the change is animated.
voidsetScale(double scale)Sets the map scale.
voidsetShowMagnifierOnLongPress(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.
FutureswitchBaseMap(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.
PointtoMapPoint(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.
PointtoMapPoint(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.
PointtoScreenPoint(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.
voidunpause()Unpauses the map.
voidzoomTo(Point centerPt, float factor)If the MapView is initialized, zooms the map by a factor to the given center point.
voidzoomToResolution(Point centerPt, double res)Centers the map on the given point and zoom into the given resolution level.
voidzoomToScale(Point centerPt, double scale)Centers the map on the given point and zoom into the given scale level.
voidzoomin()Zooms in one level from current map resolution; the map animates to the new resolution.
voidzoomin(boolean animated)Zooms in one level from current map resolution; optionally, the change is animated.
voidzoomout()Zooms out one level from current map resolution; the map animates to the new resolution.
voidzoomout(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

如果本文对您有所帮助,且您手头还很宽裕,欢迎打赏赞助我,以支付网站服务器和域名费用。 https://paypal.me/wshunli 您的鼓励与支持是我更新的最大动力,我会铭记于心,倾于博客。
本文链接:https://www.wshunli.com/posts/7622cba.html