高德地图 Web JS API示例学习笔记(15)——几何计算(距离/面积计算、关系判断)

距离/面积计算

两点间距离

使用LngLat.distance计算两个经纬度之间的直线距离。

var p1 = m1.getPosition();
var p2 = m2.getPosition();
var distance = Math.round(p1.distance(p2));

路径长度

使用 AMap.GeometryUtil.distanceOfLine 计算某段路径的实际长度,返回数据以米为单位。

var arr=new Array();//经纬度坐标数组 
arr.push(new AMap.LngLat("116.368904","39.913423")); 
arr.push(new AMap.LngLat("116.382122","39.901176")); 
arr.push(new AMap.LngLat("116.387271","39.912501")); 
arr.push(new AMap.LngLat("116.398258","39.904600")); 
arr.push(new AMap.LngLat("116.427097","39.912500"));
var distance = Math.round(AMap.GeometryUtil.distanceOfLine(arr));

区域面积

使用 AMap.GeometryUtil.ringArea 计算区域,返回数据以平方米为单位。

var area = Math.round(AMap.GeometryUtil.ringArea(path));

关系判断

点是否在线上

使用 AMap.GeometryUtil.isPointOnSegment 判断点是否在线上。

// 判断 marker 是否在线段上,最后一个参数为 m米 的误差
// pos为marker的坐标
// path为线段的两个端点坐标
var inLine = AMap.GeometryUtil.isPointOnSegment(pos, path[0], path[1],m);

点是否在多边形内

使用 AMap.GeometryUtil.isPointInRing ,判断点是否在多边形内。

// point为点的坐标
// path为多边形的坐标数组
var isPointInRing = AMap.GeometryUtil.isPointInRing(point, path);

路径是否顺时针

使用 AMap.GeometryUtil.isClockwise ,判断路径是否为顺时针。

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
可以使用以下代码进行高德经纬度距离计算和驾驶距离计算: ```php //高德地图api获取两地经纬度 $origin = "北京市朝阳区阜通东大街6号"; $destination = "北京市海淀区中关村南大街甲2号"; $origin_url = "https://restapi.amap.com/v3/geocode/geo?key=YOUR_KEY&address=".urlencode($origin); $destination_url = "https://restapi.amap.com/v3/geocode/geo?key=YOUR_KEY&address=".urlencode($destination); $origin_json = file_get_contents($origin_url); $origin_data = json_decode($origin_json,true); $origin_location = explode(",",$origin_data['geocodes'][0]['location']); $destination_json = file_get_contents($destination_url); $destination_data = json_decode($destination_json,true); $destination_location = explode(",",$destination_data['geocodes'][0]['location']); $origin_lng = $origin_location[0]; $origin_lat = $origin_location[1]; $destination_lng = $destination_location[0]; $destination_lat = $destination_location[1]; //高德经纬度距离计算 $distance_url = "https://restapi.amap.com/v3/distance?key=YOUR_KEY&origins={$origin_lng},{$origin_lat}&destination={$destination_lng},{$destination_lat}&type=1"; $distance_json = file_get_contents($distance_url); $distance_data = json_decode($distance_json,true); $distance = $distance_data['results'][0]['distance']; //高德地图api计算驾驶距离 $driving_url = "https://restapi.amap.com/v3/direction/driving?key=YOUR_KEY&origin={$origin_lng},{$origin_lat}&destination={$destination_lng},{$destination_lat}"; $driving_json = file_get_contents($driving_url); $driving_data = json_decode($driving_json,true); $duration = $driving_data['route']['paths'][0]['duration']; $distance_driving = $driving_data['route']['paths'][0]['distance']; ``` 请将上述代码中的 `YOUR_KEY` 替换为你的高德地图 API 密钥。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值