简体   繁体   English

Google Maps API-计算点击区域和const位置之间的距离

[英]Google maps API - compute distance between clicked area and const location

I would like to compute distance between a clicked area and const lat lng location. 我想计算点击区域和常量位置之间的距离。

I would like to combine these two pieces of code, but somehow I am not able to do it. 我想将这两段代码结合起来,但是以某种方式我无法做到。

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false&libraries=geometry"></script>

<script>
var p1 = new google.maps.LatLng(45.463688, 9.18814);
var p2 = HERE I WOULD LIKE TO HAVE A POSITION OF THE PLACED MARKER;

alert(calcDistance(p1, p2));

//calculates distance between two points in km's
function calcDistance(p1, p2) {
return (google.maps.geometry.spherical.computeDistanceBetween(p1, p2) / 1000).toFixed(2);
}
</script>

and

google.maps.event.addListener(map, 'click', function(event){
placeMarker(event.latLng);
});

function placeMarker(location) {
var marker = new google.maps.Marker({
    position: location, 
    map: map,
    draggable: true
});
}

It looks like you just need to move the code that alerts the distance into the correct place... 看来您只需要将提醒距离的代码移到正确的位置即可。

google.maps.event.addListener(map, 'click', function(event){
    placeMarker(event.latLng);

    var p1 = new google.maps.LatLng(45.463688, 9.18814);

    setTimeout(function() {
        alert(calcDistance(p1, event.latLng));
    }, 500);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Google Maps API V3计算当前位置与不同位置之间的距离 - Calculate distance between current location and and a different location using Google Maps API V3 使用Google Maps API DistanceMatrixService计算API中当前位置和用户位置之间的距离 - Calculate the distance between my current location and user locations from API with Google Maps API DistanceMatrixService 计算2个位置谷歌地图Api之间的距离 - Calculate the distance between 2 position Google maps Api 使用Google Maps API之间的距离 - Distance between several latlng with Google maps API 如何使用谷歌地图测量位置距离 api at javascript - how to measure distance of location using google maps api at javascript 两点之间的距离谷歌地图a是未定义的api v3 - distance between two points google maps a is undefined api v3 点和线之间的最短距离(Google Maps API问题?) - Shortest distance between a point and a line (Google Maps API issue?) Google Maps API - 检索 2 个坐标之间的道路距离 - Google Maps API - Retrieve the road distance between 2 coordinates 标记Google地图之间的距离 - Distance between markers Google maps 使用Google Map API计算两个位置之间的距离 - Calculate the distance between two location using google map api
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM