gpt4 book ai didi

javascript - 获取谷歌地图中已创建标记的位置(纬度,经度)

转载 作者:行者123 更新时间:2023-12-02 18:20:13 25 4
gpt4 key购买 nike

这里我有一个函数route();该函数在两个地点之间创建一个方向,并从 startPoint 和 endPoint 创建两个标记。一切都很好,但是我在这里如何编写一个函数,该函数将从起点返回 lat,lng ,从终点返回 lat,lng ,而无需在此处使用地理编码,因为在此函数中已经创建了标记

函数路线();代码:

function route() {
// Clear any previous route boxes from the map
clearBoxes();

// Convert the distance to box around the route from miles to km
distance = parseFloat(document.getElementById("distance").value) * 1.609344;

var request = {
origin: document.getElementById("from").value,
destination: document.getElementById("to").value,
travelMode: google.maps.DirectionsTravelMode.DRIVING
}

// Make the directions request
directionService.route(request, function(result, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsRenderer.setDirections(result);

// Box around the overview path of the first route
var path = result.routes[0].overview_path;
var boxes = routeBoxer.box(path, distance);
// alert(boxes.length);
drawBoxes(boxes);
findPlaces(boxes,0);
} else {
alert("Directions query failed: " + status);
}
});
}

所以我需要一些变量等。 var startPoint = [lat,lng]; var endPoint = [lat,lng] 到达这里,因为我不想再次使用地理编码请求,因为这里是标记及其坐标位置已经创建...

我想获取两个标记的位置以及如何做到这一点?

最佳答案

DirectionsResult 中解析出位置

if (status == google.maps.DirectionsStatus.OK) {
directionsRenderer.setDirections(result);
var route = response.routes[0];
startLocation = new Object();
endLocation = new Object();

var path = response.routes[0].overview_path;
var legs = response.routes[0].legs;
for (i=0;i<legs.length;i++) {
if (i == 0) {
startLocation.latlng = legs[i].start_location;
startLocation.address = legs[i].start_address;
}
endLocation.latlng = legs[i].end_location;
endLocation.address = legs[i].end_address;
}
// ... rest of your code to display the route ...

working example that does that (and renders the polyline and creates markers)

关于javascript - 获取谷歌地图中已创建标记的位置(纬度,经度),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18895658/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com