【问题标题】:Using Longitude/Latitude as Waypoint使用经度/纬度作为航点
【发布时间】:2012-10-03 15:42:21
【问题描述】:

我正在尝试在我的谷歌地图中使用经度纬度作为航点,但似乎无法正常工作。

这是我推动价值观的方式

waypts_mtlsheloc.push({
    location: (45.658197,-73.636333), //I don't think I'm supposed to write this like that. But can't find the right way.
    stopover: true
}) 

然后尝试像这样修改我的行

service.route({
    origin: latlng_mtlsheloc[0],
    destination: latlng_mtlsheloc[latlng_mtlsheloc.length - 1],
    waypoints:waypts_mtlsheloc,
    travelMode: google.maps.DirectionsTravelMode.DRIVING
}, function(result, status) {
        console.log(status)
        if (status == google.maps.DirectionsStatus.OK) {
        path = path.concat(result.routes[0].overview_path);
        line_mtlsheloc.setPath(result.routes[0].overview_path);
    }
});

但它给了我一个错误“属性 [航点] 错误”,我尝试了不同的方法来记下位置,但似乎找不到正确的方法。

【问题讨论】:

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


    【解决方案1】:

    要将纬度和经度用作航路点,它必须是 google.maps.LatLng 对象 (documentation 表示字符串或 LatLng;字符串是地址,LatLng 是地理坐标)

    waypts_mtlsheloc.push({
        location: new google.maps.LatLng(45.658197,-73.636333),
        stopover: true
    }) 
    

    Working Example

    jsfiddle

    代码 sn-p:

    var directionDisplay;
    var directionsService = new google.maps.DirectionsService();
    var map;
    
    function initialize() {
      directionsDisplay = new google.maps.DirectionsRenderer();
      var chicago = new google.maps.LatLng(41.850033, -87.6500523);
      var myOptions = {
        zoom: 6,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        center: chicago
      }
      map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
      directionsDisplay.setMap(map);
      calcRoute();
    }
    
    function calcRoute() {
    
      var request = {
        origin: "1521 NW 54th St, Seattle, WA 98107 ",
        destination: "San Diego, CA",
        waypoints: [{
          location: new google.maps.LatLng(42.496403, -124.413128),
          stopover: false
        }],
        optimizeWaypoints: true,
        travelMode: google.maps.DirectionsTravelMode.WALKING
      };
      directionsService.route(request, function(response, status) {
        if (status == google.maps.DirectionsStatus.OK) {
          directionsDisplay.setDirections(response);
          var route = response.routes[0];
          var summaryPanel = document.getElementById("directions_panel");
          summaryPanel.innerHTML = "";
          // For each route, display summary information.
          for (var i = 0; i < route.legs.length; i++) {
            var routeSegment = i + 1;
            summaryPanel.innerHTML += "<b>Route Segment: " + routeSegment + "</b><br />";
            summaryPanel.innerHTML += route.legs[i].start_address + " to ";
            summaryPanel.innerHTML += route.legs[i].end_address + "<br />";
            summaryPanel.innerHTML += route.legs[i].distance.text + "<br /><br />";
          }
        } else {
          alert("directions response " + status);
        }
      });
    }
    
    google.maps.event.addDomListener(window, "load", initialize);
    html {
      height: 100%
    }
    
    body {
      height: 100%;
      margin: 0px;
      padding: 0px
    }
    <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
    <div id="map_canvas" style="float:left;width:70%;height:100%;"></div>
    <div id="control_panel" style="float:right;width:30%;text-align:left;padding-top:20px">
      <div id="directions_panel" style="margin:20px;background-color:#FFEE77;"></div>
    </div>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-10
      • 2018-07-27
      • 1970-01-01
      • 2013-09-09
      相关资源
      最近更新 更多