简体   繁体   English

Google Maps Marker:“获取路线”的“需要”选项

[英]Google Maps Marker: Need option for 'Get Directions'

Rather straight forward question. 更直接的问题。

I have currently implemented the google map on my site. 我目前已经在自己的网站上实现了Google地图。 I have so far been able to edit what data is displayed on the marker and exactly how that data is styled. 到目前为止,我已经能够编辑标记上显示的数据以及该数据的样式设置。

I am stuck on one last point. 我被困在最后一点。 I need to be able to give visitors the option to click 'Get Directions'. 我需要使访问者可以选择单击“获取路线”。 I have a link on the marker called "Directions". 我在标记上有一个称为“方向”的链接。 I would like for visitors to be able to click this it 'somehow' and provide them with directions. 我希望访问者能够“以某种方式”单击它并向他们提供指示。 I'm not sure of the means or how this is accomplished. 我不确定这种方法或实现方式。 I've looked everywhere. 我到处都看过。 Any help would be greatly appreciated. 任何帮助将不胜感激。 I have included a screen shot of my marker. 我包括了我的标记的屏幕截图。

Screenshot of marker/map http://postimg.org/image/t5v5hy7yj/ 标记/地图的屏幕快照http://postimg.org/image/t5v5hy7yj/

Also, when answering, treat me as someone who knows only the basics of what was required to do this so far. 另外,在回答时,请把我当作只了解到目前为止所需的基本知识的人。 Thanks! 谢谢!

EDIT * 编辑*

I have edited it so that when they hit the 'directions' link it takes the Google map search command and places inside it what I command it to from the mysql database. 我已经对其进行了编辑,以便当他们单击“方向”链接时,它将使用Google Map搜索命令并将其放置在我从mysql数据库发出的命令中。 They would still need to click directions one more time at the now google result page to get directions. 他们仍然需要在现在的Google搜索结果页面上再点击一次路线以获取路线。 I need a more direct way of doing this! 我需要一种更直接的方法! =) =)

  downloadUrl("genxml.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 type = markers[i].getAttribute("type");
      var address = markers[i].getAttribute("address");
      var city = markers[i].getAttribute("city");
      var state = markers[i].getAttribute("state");
      var zip = markers[i].getAttribute("zip");
      var phone = markers[i].getAttribute("phone");
      var url = markers[i].getAttribute("url");
      var point = new google.maps.LatLng(
          parseFloat(markers[i].getAttribute("lat")),
          parseFloat(markers[i].getAttribute("lng")));
      var html = 
      '<a href="' + url + '" target="blank">' + name + '</a>' +
      '</a><br/><br/>' + address +
      '<br>' + city + ' ' + state + ' ' + zip +
      '<br/>' + phone + 
      '<hr style="border: none; height: 1px; color: blue; background: grey;"/>' +
      '<a href="https://maps.google.com/maps?q='+ address + ' ' + city + ' ' + state + ' ' + zip + '" target="blank"><u>Directions</u></a> ' + ' <a href="#"><u>Write a Review</u></a> ' + ' <a href="#"><u>more</u></a>' +
      '<hr style="border: none; height: 1px; color: blue; background: grey;"/>' +
      '<?php echo $ads ?>';
      var icon = customIcons[type] || {};
      var marker = new google.maps.Marker({
        map: map,
        position: point,
        icon: icon.icon,
        shadow: icon.shadow
      });
      bindInfoWindow(marker, map, infoWindow, html);
    }
  });
}

function bindInfoWindow(marker, map, infoWindow, html) {
  google.maps.event.addListener(marker, 'click', function() {
    infoWindow.setContent(html);
    infoWindow.open(map, marker);
  });
}

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);
}

function doNothing() {}

In the HTML string for the info window, add an onclick parameter that calls a javascript function like this: showDirections(" + address + "," + city + "," + state + "," + ") 在信息窗口的HTML字符串中,添加一个onclick参数,该参数调用如下所示的javascript函数: showDirections(" + address + "," + city + "," + state + "," + ")

This link should help you create the function to show directions: https://developers.google.com/maps/documentation/javascript/examples/directions-simple 此链接应帮助您创建显示路线的功能: https : //developers.google.com/maps/documentation/javascript/examples/directions-simple

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM