主要是利用 数学计算库api AMap.GeometryUtil.isPointInRing
通过判断返回值的true false来判断
lnglat是该点的经纬度:
lnglarArr是该区域的周边经纬度点的集合,
lnglarArr可以通过 AMap.DistrictSearch 来获取
AMap.plugin(‘AMap.DistrictSearch‘, function () {
var districtSearch = new AMap.DistrictSearch({
level: ‘district‘,
subdistrict: 0,
extensions: ‘all‘
})
// 搜索所有省/直辖市信息
districtSearch.search(‘萧山区‘, function (status, result) {
console.log("status, result===", status, result)
var bounds = result.districtList[0].boundaries;
let latlngArr = []
for (var i = 0; i < bounds.length; i += 1) {
let arr = JSON.parse(JSON.stringify(bounds[i]));
arr.map(item => {
latlngArr.push([item.lng, item.lat])
})
}
var isPointInRing = AMap.GeometryUtil.isPointInRing(lnglat, latlngArr)
console.log("isPointInRing", isPointInRing)
})
})
附上api链接:
AMap.DistrictSearch: https://lbs.amap.com/api/javascript-api/reference/search#m_AMap.DistrictSearch
AMap.GeometryUtil: https://lbs.amap.com/api/javascript-api/reference/math