【问题标题】:Creating a polyline for Google driving directions为 Google 行车路线创建折线
【发布时间】:2011-12-04 07:40:23
【问题描述】:

我阅读了有关 Google API 的资料,我想实现 Google 地图针对 2 个不同点的常用 Google 行车路线。到目前为止,这是我的代码

(function() {
  window.onload = function() {

// Creating a map
var options = {
  zoom: 5,
  center: new google.maps.LatLng(36.1834, -117.4960),
  mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById('map'), options);

// Creating an array that will contain the points for the polyline
var route = [
  new google.maps.LatLng(37.7671, -122.4206),
  new google.maps.LatLng(34.0485, -118.2568)
];

// Creating the polyline object
var polyline = new google.maps.Polyline({
  path: route,
  strokeColor: "#ff0000",
  strokeOpacity: 0.6,
  strokeWeight: 5
});

// Adding the polyline to the map
polyline.setMap(map);

  };
 })();

两座城市之间有一条直线……

【问题讨论】:

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


    【解决方案1】:

    以下是使用 DirectionsService 的示例:

    var directionsService = new google.maps.DirectionsService();
    var directionsDisplay;
    var homeLatlng;
    
    function initialize() {
        homeLatlng = new google.maps.LatLng(37.7671, -122.4206);
    
        var myOptions = {
            zoom: 10,
            center: homeLatlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    
        var infowindow = new google.maps.InfoWindow({
            content: ''
        });
    
        directionsDisplay = new google.maps.DirectionsRenderer({
            draggable: false,
            map: map, 
            markerOptions: {
                draggable: false
            },
            panel: document.getElementById("directionsPanel"),
            infoWindow: infowindow
        });
    
        var request = {
            origin:homeLatlng,
            destination:new google.maps.LatLng(34.0485, -118.2568),
            travelMode: google.maps.DirectionsTravelMode[DRIVING],
            unitSystem: google.maps.UnitSystem[METRIC]
        };
    
        directionsService.route(request, function(result, status) {
            if (status == google.maps.DirectionsStatus.OK) {
                directionsDisplay.setDirections(result);
            }
        });
    }
    

    【讨论】:

    【解决方案2】:

    如果您想根据行车路线(或步行路线、公交路线或自行车路线)绘制路线,请使用the Google Directions API。我不相信 API 不会为您绘制路线,但它会为您提供您需要在折线中连接以显示路线的所有纬度/经度点。

    【讨论】:

      猜你喜欢
      • 2013-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-22
      • 1970-01-01
      • 2015-11-22
      相关资源
      最近更新 更多