我在 Here Map 上绘制了几个标记和一个多边形对象。
我想知道多边形和标记之间的距离。非常接近多边形的标记是我想要弄清楚的。
这里的地图有一个距离函数,但它只能计算两个标记之间的距离。
这是我用来计算距离的函数PaohaIslandPaohaIsland.getGeometry().distance(yosmitePark.getGeometry())
是标记对象,yosmitePark是多边形对象
var map;
function loadMap(){
platform = new H.service.Platform({
'apikey': HEREMAP_KEY
});
// Obtain the default map types from the platform object:
var defaultLayers = platform.createDefaultLayers();
// Instantiate (and display) a map object:
map = new H.Map(
document.getElementById('mapContainer'),
defaultLayers.vector.normal.map,
{
center: { lat: 37.278419, lng: -119.674072,
pixelRatio: window.devicePixelRatio || 1 },
zoom: 8
}
);
// Enable the event system on the map instance:
var mapEvents = new H.mapevents.MapEvents(map);
var behavior = new H.mapevents.Behavior(mapEvents);
behavior.enable(H.mapevents.Behavior.WHEELZOOM);
}
function addPolygonToMap() {
var lineString2 = new H.geo.LineString();
lineString2.pushPoint({lat:37.278419,lng:-119.674072});
lineString2.pushPoint({lat:37.335224,lng:-119.30603});
lineString2.pushPoint({lat:37.529331,lng:-119.198914});
lineString2.pushPoint({lat:37.522789,lng:-118.99292});
lineString2.pushPoint({lat:37.627281,lng:-118.87207});
lineString2.pushPoint({lat:37.80978,lng:-119.053337});
lineString2.pushPoint({lat:38.01347,lng:-119.143982});
lineString2.pushPoint({lat:37.965851,lng:-119.770203});
lineString2.pushPoint({lat:37.898689,lng:-120.18219});
lineString2.pushPoint({lat:37.867802,lng:-120.341492});
lineString2.pushPoint({lat:37.746819,lng:-120.239861});
lineString2.pushPoint({lat:37.51844,lng:-120.07782});
lineString2.pushPoint({lat:37.278419,lng:-119.674072});
var yosmitePark = new H.map.Polygon(lineString2, {
style: {
strokeColor: '#829',
lineWidth: 8
}
});
map.addObject(yosmitePark);
var lakeMcClure = new H.map.Marker({
lat: 37.6373862,
lng: -120.3448606
});
lakeMcClure.setData("lakeMcClure");
var Stockton = new H.map.Marker({
lat: 37.9729404,
lng: -121.4419639
});
Stockton.setData("Stockton");
var Monolake = new H.map.Marker({
lat: 38.0067483,
lng: -119.1013779
});
Monolake.setData("Monolake");
var PaohaIsland = new H.map.Marker({
lat: 38.000514,
lng: -119.0416587
});
PaohaIsland.setData("PaohaIsland");
var WalkerLake = new H.map.Marker({
lat: 38.7010578,
lng: -118.878652
});
WalkerLake.setData("WalkerLake");
var Bakersfield = new H.map.Marker({
lat: 35.3208963,
lng: -119.1587737
});
Bakersfield.setData("Bakersfield");
var SanFrancisco = new H.map.Marker({
lat: 37.7576793,
lng: -122.5076405
});
SanFrancisco.setData("SanFrancisco");
map.addObjects([lakeMcClure,Stockton,Monolake,PaohaIsland,WalkerLake,Bakersfield,SanFrancisco]);
var mapObj = map.getObjects();
console.log(yosmitePark.getGeometry());
console.log(PaohaIsland.getGeometry().distance(yosmitePark.getGeometry()));
}