谷歌地图v3航点

时间:2014-06-27 09:13:48

标签: javascript google-maps google-maps-api-3

我使用以下代码来显示路线和航路点,但是路标没有显示,如果我使用waypoints: [{location: "(39.424504, -76.596594)", stopover: true}]显示航路点,但我无法使其与数组配合使用。

我使用了alert(waypts[0].location);,数组得到了值。

var start = document.getElementById('start_cord').value;
var end = document.getElementById('end_cord').value;
var waypts = [];
makeRequest('get_waypoint.php', function(data) {

    var data = JSON.parse(data.responseText);
    var latit;
    var longi;

    for (var i = 0; i < data.length; i++) {

        latit = parseFloat(data[i].lat);
        longi = parseFloat(data[i].lon);

        waypts.push({location: new google.maps.LatLng(latit, longi),
                     stopover: true});
    }
});

var request = {
  origin: start,
  destination: end,
  waypoints: waypts,
  optimizeWaypoints: true,
  travelMode: google.maps.TravelMode.DRIVING
};

directionsService.route(request, function(response, status) {
  if (status == google.maps.DirectionsStatus.OK) {
    directionsDisplay.setDirections(response);
    var route = response.routes[0];
    var summaryPanel = document.getElementById('directions_panel');
    summaryPanel.innerHTML = '';
    // For each route, display summary information.
    for (var i = 0; i < route.legs.length; i++) {
      var routeSegment = i + 1;
      summaryPanel.innerHTML += '<b>Route Segment: ' + routeSegment + '</b><br>';
      summaryPanel.innerHTML += route.legs[i].start_address + ' --- ';
      summaryPanel.innerHTML += route.legs[i].end_address + '<br>';
      summaryPanel.innerHTML += route.legs[i].distance.text + '<br><br>';
    }
summaryPanel.style.display = 'block';
  }

});

makeRequest的:

function makeRequest(url, callback) {
  var request;
  if (window.XMLHttpRequest) {
    request = new XMLHttpRequest(); // IE7+, Firefox, Chrome, Opera, Safari
  } else {
    request = new ActiveXObject("Microsoft.XMLHTTP"); // IE6, IE5
  }
  request.onreadystatechange = function() {
    if (request.readyState == 4 && request.status == 200) {
        callback(request);
    }
  }
  request.open("GET", url, true);
  request.send();
}

0 个答案:

没有答案