首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何显示From address into to address的方向

如何显示From address into to address的方向
EN

Stack Overflow用户
提问于 2016-01-04 13:53:14
回答 1查看 126关注 0票数 1

我得到了这个代码的纬度和经度值,在这里我想要显示从地址到地址的方向,我不知道请帮助…

代码语言:javascript
复制
<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>
代码语言:javascript
复制
<div id="dvMap" style="width: 300px; height: 300px">

EN

回答 1

Stack Overflow用户

发布于 2016-01-04 19:13:23

你好,这是Direction Service的一个小例子,我使用了FROM address和TO address的中心点,你点击它就会把TO地址带到哪里。地图画出了驱动的根部。

代码语言:javascript
复制
    <!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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34585406

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档