6

是否可以绘制多个标记以及绘制两点之间的路线?

我已经成功地分开了。但是如何在同一张地图中完成呢?

当我将两者结合起来时,路线图没有完成,但是标记正在被绘制。

到目前为止我所做的如下:

    var directionsDisplay;
    var directionsService = new google.maps.DirectionsService();
    var map;

    var locations = [
        ['Bondi Beach', -33.890542, 151.274856, 4],
        ['Coogee Beach', -33.923036, 151.259052, 5],
        ['Cronulla Beach', -34.028249, 151.157507, 3],
        ['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
        ['Maroubra Beach', -33.950198, 151.259302, 1]
    ];


    var infowindow = new google.maps.InfoWindow();

    var marker, i;

    for (i = 0; i < locations.length; i++) {
        marker = new google.maps.Marker({
            position: new google.maps.LatLng(locations[i][1], locations[i][2]),
            map: map
        });

        google.maps.event.addListener(marker, 'click', (function (marker, i) {
            return function () {
                infowindow.setContent(locations[i][0]);
                infowindow.open(map, marker);
            }
        })(marker, i));
    }

    directionsDisplay = new google.maps.DirectionsRenderer();

    directionsDisplay.setMap(map);

    var start = new google.maps.LatLng(-33.890542, 151.274856);
    var end = new google.maps.LatLng(-34.028249, 151.157507);
    var request = {
        origin: start,
        destination: end,
        travelMode: google.maps.DirectionsTravelMode.DRIVING
    };
    directionsService.route(request, function (response, status) {
        if (status == google.maps.DirectionsStatus.OK) {
            directionsDisplay.setDirections(response);
        }
    });

    map = new google.maps.Map(document.getElementById('map'), {
        zoom: 10,
        center: new google.maps.LatLng(-33.92, 151.25),
        mapTypeId: google.maps.MapTypeId.ROADMAP
    });

HTML:

 <div id="map-canvas"></div>

脚本:

 <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
4

1 回答 1

10

您需要先初始化地图变量,然后再将其用于标记和方向。如果您希望地图出现的 div 名为“map-canvas”,则应在 google.maps.Map 构造函数中使用该名称。

var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
var map;

var locations = [
    ['Bondi Beach', -33.890542, 151.274856, 4],
    ['Coogee Beach', -33.923036, 151.259052, 5],
    ['Cronulla Beach', -34.028249, 151.157507, 3],
    ['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
    ['Maroubra Beach', -33.950198, 151.259302, 1]
];


var infowindow = new google.maps.InfoWindow();

var marker, i;

map = new google.maps.Map(document.getElementById('map-canvas'), {
    zoom: 10,
    center: new google.maps.LatLng(-33.92, 151.25),
    mapTypeId: google.maps.MapTypeId.ROADMAP
});

for (i = 0; i < locations.length; i++) {
    marker = new google.maps.Marker({
        position: new google.maps.LatLng(locations[i][1], locations[i][2]),
        map: map
    });

    google.maps.event.addListener(marker, 'click', (function (marker, i) {
        return function () {
            infowindow.setContent(locations[i][0]);
            infowindow.open(map, marker);
        }
    })(marker, i));
}

directionsDisplay = new google.maps.DirectionsRenderer();

directionsDisplay.setMap(map);

var start = new google.maps.LatLng(-33.890542, 151.274856);
var end = new google.maps.LatLng(-34.028249, 151.157507);
var request = {
    origin: start,
    destination: end,
    travelMode: google.maps.DirectionsTravelMode.DRIVING
};
directionsService.route(request, function (response, status) {
    if (status == google.maps.DirectionsStatus.OK) {
        directionsDisplay.setDirections(response);
    }
});

您可能还应该在页面完成渲染后运行地图代码。

工作示例

于 2013-10-29T09:49:14.097 回答