Arcgis runtime for Android 100.5
2020-07-16 本文已影响0人
_执_念__
想要在地图上绘制,需要一个GraphicsOverlay临时绘制图层
100版本单独把绘制图层拿出来的一个好处就是,它将永远置于地图内容之上。在以前我们如果要在MapView里加载一个新的图层,必须先remove带有图形的FeatureLayer,加载这个图层之后再加载一次带有图形的FeatureLayer,否则我们这个FeatureLayer将会被新加载的图层所覆盖。现在这个问题就不复存在了。
//创建GraphicsOverlay
val graphicsOverlay = GraphicsOverlay()
val graphicsOverlay2 = GraphicsOverlay(GraphicsOverlay.RenderingMode.DYNAMIC)
//添加到地图上
mapView.graphicsOverlays.add(graphicsOverlay)
GraphicsOverlay默认的构造模式一共有两种:DYNAMIC和STATIC,默认的是动态渲染模式DYNAMIC。
当为DYNAMIC模式时候,每当图形有变化时候GraphicsOverlay会立刻更新图形;
而为STATIC模式时候,图形变化了不会马上更新,而是在进行地图的缩放、旋转和平移时候才更新图层,适合于绘制图层里含有大量图形时候使用,以防图形更新太慢影响体验。
符号Symbol
1. 点符号(MarkerSymbol)
PictureMarkerSymbol(图片点符号)
SimpleMarkerSymbol(简单点符号)
TextSymbol(文本符号)
2. 线符号(LineSymbol)
SimpleLineSymbol(简单线符号)
3. 面符号(FillSymbol)
PictureFillSymbol(图片面符号)
SimpleFillSymbol(简单面符号)
渲染Renderer
渲染也是设置图形样式,相比起符号来,渲染就相当于是批量的图形样式设置。但是当渲染和符号同时存在时候,会优先使用符号的样式
- ClassBreaksRenderer
- HeatmapRenderer
- SimpleRenderer
- UniqueValueRenderer
- UnsupportedRenderer
举例 SimpleRenderer
//创建一个点样式
val pointSymbol = SimpleMarkerSymbol(
SimpleMarkerSymbol.Style.CIRCLE,
Color.RED,
10F
)
// 创建一个渲染器
val pointRenderer = SimpleRenderer(pointSymbol)
//给GraphicOverlay设置渲染器
pointGraphicOverlay.setRenderer(pointRenderer)
val pointGeometry = Point(40e5, 40e5, SpatialReferences.getWebMercator())
val pointGraphic = Graphic(pointGeometry)
pointGraphicOverlay.graphic.add(pointGraphic )
1. 绘制点
举例 在地图上触摸某处,就在此处绘制一个点
mapView.onTouchListener = object : DefaultMapViewOnTouchListener(this, mapView) {
override fun onSingleTapConfirmed(e: MotionEvent): Boolean {
//获取到触摸的屏幕上的点
val point = android.graphics.Point(e.x.toInt(), e.y.toInt())
//转换为地图上的点
val mapPoint: Point = mapView.screenToLocation(point)
// 生成一个红色圆形标识 (点样式,颜色,大小)
val pointSymbol = SimpleMarkerSymbol(
SimpleMarkerSymbol.Style.CIRCLE,
Color.RED,
10F
)
//创建Graphic
val graphic = Graphic(point1, pointSymbol)
//可以给graphic添加一些属性
graphic.attributes["value"] = "Graphic0"
//添加graphic到 GraphicsOverlay上
graphicsOverlay.graphics.add(lineGraphic)
return super.onSingleTapConfirmed(e)
}
}
2. 绘制线
举例 在地图上触摸一个位置,就连接一条线到这个位置
//创建一个list存放触摸的点集合
private val linePoints by lazy { ArrayList<Point>() }
mapView.onTouchListener = object : DefaultMapViewOnTouchListener(this, mapView) {
override fun onSingleTapConfirmed(e: MotionEvent): Boolean {
//获取到触摸的屏幕上的点
val point = android.graphics.Point(e.x.toInt(), e.y.toInt())
//转换为地图上的点
val mapPoint: Point = mapView.screenToLocation(point)
linePoints.add(mapPoint)
if (linePoints.size == 1) {//如果集合中只有一个点,就先画一个点
val pointSymbol = SimpleMarkerSymbol(
SimpleMarkerSymbol.Style.SQUARE,
Color.BLUE,
5F
)
val graphic = Graphic(mapPoint, pointSymbol)
graphicsOverlay.graphics.add(graphic)
} else {//如果多于1个点,就开始画线,把之前画的一个点清除
graphicsOverlay.graphics.clear()
}
//创建线的路径
val lineGeometry = PolylineBuilder(mapView.spatialReference)
//添加触摸的点作为路径
lineGeometry.addPoints(linePoints)
//线的样式
val lineSymbol = SimpleLineSymbol(
SimpleLineSymbol.Style.SOLID,
Color.BLUE,
5F
)
//根据路径计算出线的长度
val length = GeometryEngine.length(lineGeometry.toGeometry())
val numberFormat = NumberFormat.getInstance()
numberFormat.maximumFractionDigits = 2
val s = numberFormat.format(length)
val lineGraphic = Graphic(lineGeometry.toGeometry(), lineSymbol)
//长度属性设置到graphic上
lineGraphic.attributes["value"] = "长度:$s 米"
graphicsOverlay.graphics.add(lineGraphic)
return super.onSingleTapConfirmed(e)
}
}
3. 绘制面
//创建一个list存放触摸的点集合
private val areaPoints by lazy { ArrayList<Point>() }
mapView.onTouchListener = object : DefaultMapViewOnTouchListener(this, mapView) {
override fun onSingleTapConfirmed(e: MotionEvent): Boolean {
val point =android.graphics.Point(e.x.toInt(), e.y.toInt())
val mapPoint: Point = mapView.screenToLocation(point)
areaPoints.add(point1)
if (areaPoints.size == 1) {//只有一个点,绘制一个点
val pointSymbol = SimpleMarkerSymbol(
SimpleMarkerSymbol.Style.SQUARE,
Color.argb(128, 255, 255, 0),
5F
)
val graphic = Graphic(point1, pointSymbol)
graphicsOverlay.graphics.add(graphic)
} else if (areaPoints.size == 2) {//有两个点,绘制一条线
val lineGeometry = PolylineBuilder(mapView.spatialReference)
lineGeometry.addPoints(areaPoints)
val lineSymbol = SimpleLineSymbol(
SimpleLineSymbol.Style.SOLID,
Color.argb(128, 255, 255, 0),
5F
)
val lineGraphic = Graphic(lineGeometry.toGeometry(), lineSymbol)
graphicsOverlay.graphics.add(lineGraphic)
} else {//两个点以上,开始绘制面,清除之前的线
graphicsOverlay.graphics.clear()
}
//创建面路径
val polygonGeometry = PolygonBuilder(mapView.spatialReference)
polygonGeometry.addPoints(areaPoints)
val polygonSymbol = SimpleFillSymbol(
SimpleFillSymbol.Style.SOLID,
Color.argb(128, 255, 255, 0),
null
)
//这里可以根据面的范围 计算面积
val area = GeometryEngine.area(polygonGeometry.toGeometry())
val numberFormat = NumberFormat.getInstance()
numberFormat.maximumFractionDigits = 2
val s = numberFormat.format(area)
val graphic = Graphic(polygonGeometry.toGeometry(), polygonSymbol)
//面积属性添加到graphic上
graphic.attributes["value"] = "面积:$s 平方米"
graphicsOverlay.graphics.add(graphic)
return super.onSingleTapConfirmed(e)
}
}
4. 绘制文字
mapView.onTouchListener = object : DefaultMapViewOnTouchListener(this, mapView) {
override fun onSingleTapConfirmed(e: MotionEvent): Boolean {
//获取到触摸的屏幕上的点
val point = android.graphics.Point(e.x.toInt(), e.y.toInt())
//转换为地图上的点
val mapPoint: Point = mapView.screenToLocation(point)
val textSymbol = TextSymbol(20, "绘制文字", Color.RED,
TextSymbol.HorizontalAlignment.CENTER, TextSymbol.VerticalAlignment.MIDDLE)
val graphic = Graphic(mapPoint, textSymbol)
graphicsOverlay.graphics.add(graphic)
}
}
5. 绘制图片
mapView.onTouchListener = object : DefaultMapViewOnTouchListener(this, mapView) {
override fun onSingleTapConfirmed(e: MotionEvent): Boolean {
//获取到触摸的屏幕上的点
val point = android.graphics.Point(e.x.toInt(), e.y.toInt())
//转换为地图上的点
val mapPoint: Point = mapView.screenToLocation(point)
val bitmap = BitmapFactory.decodeResource(resources, R.mipmap.ic_compass)
val bitmapDrawable = BitmapDrawable(resources, bitmap)
val pictureMarkerSymbol = PictureMarkerSymbol(bitmapDrawable)
val graphic = Graphic(mapPoint, pictureMarkerSymbol)
//涉及到加载图片到符号里,所以需要一个异步监听操作
pictureMarkerSymbol.loadAsync()
pictureMarkerSymbol.addDoneLoadingListener {
graphicsOverlay.graphics.add(graphic)
}
return super.onSingleTapConfirmed(e)
}
}
6. 绘制矩形
val envelope = Envelope(
leftTopPoint.x,
leftTopPoint.y,
rightBottomPoint.x,
rightBottomPoint.y,
mapView.spatialReference
)
//填充样式
val simpleFillSymbol = SimpleFillSymbol(
SimpleFillSymbol.Style.SOLID,
Color.argb(128, 255, 255, 0),
null
)
//边框样式
simpleFillSymbol.outline = SimpleLineSymbol(
SimpleLineSymbol.Style.DOT,
Color.BLUE,
1F
)
val graphic = Graphic(envelope, simpleFillSymbol)
graphicsOverlay.graphics.add(graphic)
清除临时图层上的Graphic
graphicsOverlay.graphics.clear()