如何显示From地址到To地址的方向

时间:2016-01-04 05:53:14

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

我获得了此代码的纬度和经度值,此后我想显示FROM地址到TO地址的方向,我不知道请帮忙......



<script type="text/javascript">
        window.onload = function () {
            var mapOptions = {
                center: new google.maps.LatLng(12.9715987, 77.59456269999998),//FROM address Latitude , Longitude value
                zoom: 14,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            };
            var infoWindow = new google.maps.InfoWindow();
            var latlngbounds = new google.maps.LatLngBounds();
            var map = new google.maps.Map(document.getElementById("dvMap"), mapOptions);
            google.maps.event.addListener(map, 'click', function (e) {
                alert("Latitude: " + e.latLng.lat() + "\r\nLongitude: " + e.latLng.lng());// i got TO address Latitude , Longitude value
            });
        }
    </script>
&#13;
<div id="dvMap" style="width: 300px; height: 300px">
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

嗨这个会对你有帮助,  方向服务的小例子,我使用FROM地址的中心点,而TO地址点击它将获取TO地址。地图绘制了驱动根。

    <!DOCTYPE html>
    <html>
      <head>
        <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
        <meta charset="utf-8">
        <title>Google Maps JavaScript API v3 Example: Directions Travel Modes</title>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
        <script src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
        <input type="text" id="from_Latlng" value="12.9301397, 77.58773180000003" />
        <input type="text" id="to_Latlng" value="" />
        <script>
        $(function(){ initialize();  });
          var directionDisplay;
          var directionsService = new google.maps.DirectionsService();
          var map;

         function initialize() {

          var From = new google.maps.LatLng(12.9301397, 77.58773180000003);
          var to_latLng=$("#to_Latlng").val();
          if(to_latLng!='')
          {
                var to_LatLngSplit=to_latLng.split(",");
                var to_lat=to_LatLngSplit[0];
                var to_lng=to_LatLngSplit[1];
                var to = new google.maps.LatLng(to_lat,to_lng);
          }
          else
          {
            var to = new google.maps.LatLng(12.9997841, 77.68394269999999);
          }
            directionsDisplay = new google.maps.DirectionsRenderer();
            var mapOptions = {
              zoom: 14,
              mapTypeId: google.maps.MapTypeId.ROADMAP,
              center: From
            }
            map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
          // If there are any parameters at eh end of the URL, they will be in  location.search
          var lat,lng,zoom,type;
          //looking something like  "?marker=3"
          directionsDisplay.setMap(map);
          calcRoute(From,to);

                google.maps.event.addListener(map, 'click', function (e) {
                     //alert("Latitude: " + e.latLng.lat() + "\r\nLongitude: " + e.latLng.lng());// i got TO address Latitude , Longitude value
                     $('#to_Latlng').val(e.latLng.lat()+","+e.latLng.lng());
                     initialize();
                });

         }

          function calcRoute(From,to) {
            var selectedMode = "DRIVING";
            var request = {
                origin: From,
                destination: to,
                travelMode: google.maps.TravelMode[selectedMode]
            };
            directionsService.route(request, function(response, status) {
              if (status == google.maps.DirectionsStatus.OK) {
                directionsDisplay.setDirections(response);
              } else { 
                alert("Directions Request Failed, "+status);
              }
            });
          }
        </script>
      </head>
      <body onload="initialize()">
        <div>
        <b>Mode of Travel: </b>
        </div>
        <div id="map_canvas" style="height: 450px;"></div>
      </body>
    </html>

Demo