//经纬度转换为墨卡托坐标
private fun wgstmkt(a: Double,b: Double):Point{//指定返回类型
// Log.i("测试","a ==="+a +"b=="+b);
var wgsdian =Point(a, b)
var wgsdian2 = GeometryEngine.project(wgsdian, SpatialReferences.getWgs84()) as Point
var mktdian = GeometryEngine.project(wgsdian2, SpatialReferences.getWebMercator()) as Point
return mktdian
}
//屏幕点击地图的点作为墨卡托坐标
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)
return super.onSingleTapConfirmed(e)
}
}
//计算距离和面积
val graphicsOverlay = GraphicsOverlay()
mapView.graphicsOverlays.add(graphicsOverlay)
val polylinePointsjs = PointCollection(SpatialReferences.getWebMercator()).apply {
add(wgstmkt(121.428731, 37.461226))
add(wgstmkt(121.438493, 37.458680))
}
// 从点集合创建一个折线几何
val polylinejs = Polyline(polylinePointsjs)
//根据路径计算出线的长度
val length = GeometryEngine.length(polylinejs)
val numberFormat = NumberFormat.getInstance()
numberFormat.maximumFractionDigits = 2
val s = numberFormat.format(length)
Log.i("墨卡托距离","长度:$length 米" );
Log.i("墨卡托距离保留两位","长度:$s 米" );
//////////////////////////////////用测地线的方法测距离
val polylinePoints = PointCollection(SpatialReferences.getWgs84()).apply {
// Point(latitude, longitude)
add(Point(121.428731, 37.461226))
add(Point(121.438493, 37.458680))
// add(Point(121.447293, 37.457323))
}
// 从点集合创建一个折线几何
val polyline = Polyline(polylinePoints)
// 为折线创建一个蓝线符号
val polylineSymbol = SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, -0xff9c01, 3f)
val graphic2 = Graphic(polyline, polylineSymbol)
graphicsOverlay.graphics.add(graphic2)
// 用路径图形使路径密集成测地线曲线
val pathGeometry = GeometryEngine.densifyGeodetic(
polyline,
1.0,
unitOfMeasurement,
GeodeticCurveType.GEODESIC
)
// 计算路径的距离 测地线方法
val distancejl =
GeometryEngine.lengthGeodetic(pathGeometry, unitOfMeasurement, GeodeticCurveType.GEODESIC)
Log.i("wgs84距离","distance ==="+distancejl );
//计算面积
val polygonPointsmjjs = PointCollection(SpatialReferences.getWebMercator()).apply {
// 要顺时针,逆时针会出现负的
add(wgstmkt(121.438849, 37.465382))
add(wgstmkt(121.449894, 37.459472))
add(wgstmkt(121.450179, 37.474063))
add(wgstmkt(121.458159, 37.464506))
}
// 从点集合创建一个多边形几何
val polygonmjjs = Polygon(polygonPointsmjjs)
//这里可以根据面的范围 计算面积
val areajs = GeometryEngine.area(polygonmjjs)
Log.i("面积","面积:$areajs 平方米");