当点击标记时,获取从地理位置到标记的路线

时间:2014-04-26 20:47:53

标签: javascript php google-maps geolocation

我生成了一个地图,其中包含从生成的XML文件加载的标记。我确定了我的位置。现在,当点击标记时,我正试图从我的位置获取指示标记。这是我的代码完成工作;其他一切正常,除非我尝试点击标记并生成方向,也许你可以帮我弄清楚我做错了什么。谢谢。

 function initialize() {


    var directionsDisplay;
    var directionsService = new google.maps.DirectionsService();


    navigator.geolocation.getCurrentPosition(locate);

    var im = 'http://www.robotwoods.com/dev/misc/bluecircle.png';

    function locate(position) {
        var myLatLng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
        var mapOptions = {
          zoom: 12,
          center: myLatLng,
          mapTypeId: google.maps.MapTypeId.ROADMAP,
          zoomControl: true,
          streetViewControl: false,
        }
        var map = new google.maps.Map(document.getElementById('map-canvas'),
                                      mapOptions);
        var userMarker = new google.maps.Marker({
            position: myLatLng,
            map: map,
            icon: im
        });

      var infoWindow = new google.maps.InfoWindow;

      // Change this depending on the name of your PHP file
      downloadUrl("xmltest.php", function(data) {
        var xml = data.responseXML;
        var markers = xml.documentElement.getElementsByTagName("marker");
        for (var i = 0; i < markers.length; i++) {
          var name = markers[i].getAttribute("name");
          var address = markers[i].getAttribute("address");
          var point = new google.maps.LatLng(
              parseFloat(markers[i].getAttribute("lat")),
              parseFloat(markers[i].getAttribute("lng")));
          var html = "<b>" + name + "</b> <br/>" + address;
          var marker = new google.maps.Marker({
            map: map,
            position: point,
          });
          bindInfoWindow(marker, map, infoWindow, html);
        }
      });

    function bindInfoWindow(marker, map, infoWindow, html) {
      google.maps.event.addListener(marker, 'click', function() {
       var start = myLatLng;
       var end = position;
       var request = {
        origin:start,
        destination:end,
        travelMode: google.maps.TravelMode.Driving      
       }

      });
      directionsServices.route(request, function(response, status) { 
      if (status  == google.maps.DirectionsStatus.OK) {
          directionsDisplay.setDirections(response);
      }
      });

    }

    function downloadUrl(url, callback) {
      var request = window.ActiveXObject ?
          new ActiveXObject('Microsoft.XMLHTTP') :
          new XMLHttpRequest;

      request.onreadystatechange = function() {
        if (request.readyState == 4) {
          request.onreadystatechange = doNothing;
          callback(request, request.status);
        }
      };

      request.open('GET', url, true);
      request.send(null); 
}
}
}

1 个答案:

答案 0 :(得分:0)

此代码存在一些问题。

directionsDisplay变量未定义。这应该在locate()函数中完成:

    var directionsDisplay = new google.maps.DirectionsRenderer();
    directionsDisplay.setMap(map);
应在标记点击事件处理程序中调用

directionsService.route()

变量request应定义为全局变量end应具有不同的值...

请参阅example at jsbin