高德地图获取点到线的直线距离

最近写高德地图的时候遇到一个功能,地图初始化时存在一条轨迹线,用户在地图上可以自己重新编辑一条新轨迹,判断初始化时的轨迹有无偏移。原理就是用户在点击每个点时获取点到线的最短距离,判断距离有无超出限制。

下面就简单说明一下实现过程

①定义一个marker点和line线路数据

export default {
  data() {
    return {
        marker: [118.78518, 32.041471],
        line: [
	        [118.806423, 32.047364],
	        [118.805221, 32.044272],
	        [118.804706, 32.040889],
	        [118.804406, 32.039361],
	        [118.803977, 32.037797],
	        [118.803376, 32.036087],
	        [118.802689, 32.033504],
	        [118.802217, 32.031358],
	        [118.801874, 32.02892],
        ],
      },
    };
  },

②获取点到线距离

methods: {
	newmap() {
	      map = new AMap.Map("map", {
	        zoom: 12,
	        center: [120.97527, 31.39308],
	        mapStyle: "amap://styles/darkblue",
	      });
	      	var pos = this.marker
			var distance = Math.round(AMap.GeometryUtil.distanceToLine(pos, this.line));
			// 得到距离
			console.log(distance);
      }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Android 高德地图中,计算点到线线段的最短距离可以通过以下步骤实现: 1. 获取地图上的线段。 ```java LatLng point = new LatLng(39.915168, 116.403875); LatLng start = new LatLng(39.993015, 116.474172); LatLng end = new LatLng(39.902981, 116.447901); Polyline polyline = new PolylineOptions().add(start, end).color(Color.RED).width(10f); aMap.addPolyline(polyline); ``` 2. 计算点到线段的距离。 ```java double distance = AMapUtils.calculateLineDistance(point, AMapUtils.nearestPointOnLine(point, start, end)); ``` 其中,nearestPointOnLine 方法可以计算点到线段的垂足,然后使用 calculateLineDistance 方法计算点到垂足的距离。 3. 显示距离信息。 ```java Toast.makeText(this, "点到线段的最短距离为:" + distance + "米", Toast.LENGTH_SHORT).show(); ``` 完整代码如下: ```java public class MainActivity extends AppCompatActivity implements OnMapReadyCallback { private AMap aMap; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); MapView mapView = findViewById(R.id.map_view); mapView.onCreate(savedInstanceState); mapView.getMapAsync(this); } @Override public void onMapReady(AMap aMap) { this.aMap = aMap; LatLng point = new LatLng(39.915168, 116.403875); LatLng start = new LatLng(39.993015, 116.474172); LatLng end = new LatLng(39.902981, 116.447901); Polyline polyline = new PolylineOptions().add(start, end).color(Color.RED).width(10f); aMap.addPolyline(polyline); double distance = AMapUtils.calculateLineDistance(point, AMapUtils.nearestPointOnLine(point, start, end)); Toast.makeText(this, "点到线段的最短距离为:" + distance + "米", Toast.LENGTH_SHORT).show(); } @Override protected void onResume() { super.onResume(); MapView mapView = findViewById(R.id.map_view); mapView.onResume(); } @Override protected void onPause() { super.onPause(); MapView mapView = findViewById(R.id.map_view); mapView.onPause(); } @Override protected void onDestroy() { super.onDestroy(); MapView mapView = findViewById(R.id.map_view); mapView.onDestroy(); } @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); MapView mapView = findViewById(R.id.map_view); mapView.onSaveInstanceState(outState); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值