需要在infowindow中添加“到这里的路线”链接

时间:2012-03-20 15:16:37

标签: javascript google-maps-api-3

我是谷歌地图和Javascript的新手,但必须解决这个问题!我有一个.html文件,其中包含我们客户位置的Google地图,该地图已加载到map_canvas div中。通过教程,我已经能够弄清楚如何编写足够的Javascript来使地图运行,有一个自定义标记和当你点击它时弹出的信息窗口。剩下的唯一部分是在infowindow内部有一个链接,上面写着“到这里的方向”,并且有一个文本字段,用户可以在其中输入起始地址。我看到很多文档可以帮助我在Google Developer的网站上编写我自己的代码,但是我还不够先进。任何人都可以帮我弄清楚如何做到这一点?这是我的Javascript:

<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
function initialize()
{
    var latlng = new google.maps.LatLng(33.929011, -84.361);
    var myOptions = {
        zoom: 15,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

    var marker = new google.maps.Marker({
        position: new google.maps.LatLng(33.929011, -84.361),
        map: map,
        title: 'Atlanta/Sandy Springs',
        clickable: true,
        icon: 'images/mapmarker.png'
    });

    var infowindow = new google.maps.InfoWindow({
      content: 'client address'
    });

    google.maps.event.addListener(marker, 'click', function() {
      infowindow.open(map, marker);
    });

}
</script>

1 个答案:

答案 0 :(得分:4)

我添加了几件。首先,你需要在infowindow中有一个更好的HTML表单,因为我只输入基本部分,当你按Enter键时它不会响应。

接下来,我添加了一个地理编码器,因为路线服务不会采用“人类可读”的地址,只有latLng坐标。地理编码器会转换两者。最后,添加了服务和显示方向。方向文本进入div(directionsPanel)。

请参阅working fiddle或使用此代码:

<!DOCTYPE html>
<html>
  <head>
    <style type="text/css">
  html, body {
      margin: 0;
      padding: 0;
      height: 100%
  }
  #map_canvas {
      margin: 0;
      padding: 0;
      width: 50%;
      height: 100%
  }
  #directionsPanel {
      position: absolute;
      top: 0px;
      right: 0px;
      width: 50%
  }
    </style>
    <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
    <script type="text/javascript">
var geocoder;
var directionsService;
var directionsDisplay;

function initialize() {
    var latlng = new google.maps.LatLng(33.929011, -84.361);
    var myOptions = {
        zoom: 15,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    var marker = new google.maps.Marker({
        position: new google.maps.LatLng(33.929011, -84.361),
        map: map,
        title: 'Atlanta/Sandy Springs',
        clickable: true
    });

    var infowindow = new google.maps.InfoWindow({
        content: "Your address: <input id='clientAddress' type='text'>"+
                "<input type='button' onClick=getDir() value='Go!'>"
    });

    google.maps.event.addListener(marker, 'click', function () {
        infowindow.open(map, marker);
    });
    geocoder = new google.maps.Geocoder();
    directionsService = new google.maps.DirectionsService();
    directionsDisplay = new google.maps.DirectionsRenderer({
        suppressMarkers: false
    });
    directionsDisplay.setMap(map);
    directionsDisplay.setPanel(document.getElementById("directionsPanel"));
}

function getDir() {
    geocoder.geocode({
        'address': document.getElementById('clientAddress').value
    },

    function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            var origin = results[0].geometry.location;
            var destination = new google.maps.LatLng(33.929011, -84.361);

            var request = {
                origin: origin,
                destination: destination,
                travelMode: google.maps.DirectionsTravelMode.DRIVING
            };

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

        } else {
            document.getElementById('clientAddress').value =
                "Directions cannot be computed at this time.";
        }
    });
}
    </script>
  </head>
  <body onload="initialize()">
    <div id="map_canvas"></div>
    <div id="directionsPanel"></div>
  </body>
</html>