0

这里我有一个函数 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] 到达这里,因为我不想再次使用地理编码请求,因为这里是标记和他的坐标位置已经创建......

我想获取两个标记的位置,我该怎么做?

4

1 回答 1

2

从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 ...

这样做的工作示例(并渲染折线并创建标记)

于 2013-09-19T13:22:05.087 回答