Google Maps API会在点击数组中推送Marker Lat / Lng以获取行路径

时间:2014-09-09 18:07:14

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

我想要完成的是当用户点击一个标记时,该标记的latlng被视为一条线的第一个latlng,它可能有多个目的地。这是我的标记语法,用于将单击的标记推送到数组:

var originPoint = []
for (var i in points) {
    var p = points[i];
    var latlng = new google.maps.LatLng(p[1], p[2]);

    var marker = new google.maps.Marker({
        position: latlng,
        icon: points[i][3],
        zIndex: p[5],
        title: p[0]
    });

    overviewMarkers.push(marker)

    google.maps.event.addListener(marker, 'click', (function(marker, i) {
        return function() {

            infowindow.setContent(points[i][6] + '<div id="infopanel">' +
                '<input onclick="addLine();" type=button value="Show Routes">' +
                '<input onclick="removeLine();" type=button value="Remove Routes"></div>');
            infowindow.open(map, marker)
            originPoint.push(marker, i);
        }
    })(marker, i));
}

然后是我绘制线条的语法。我不确定我是否正确地从originPoint数组调用标记latlng:

var arrayLine = []
var destPoint = [new google.maps.LatLng(51.9279723, 4.4904063),
    new google.maps.LatLng(40.136482, -73.831299),
    new google.maps.LatLng(34.0204989, -118.4117325)
];

var lineSymbol = {
    path: google.maps.SymbolPath.FORWARD_OPEN_ARROW
};

for (var d in destPoint) {
    var dt = destPoint[d];
    var ot = new google.maps.LatLng(originPoint[1], originPoint[2])

    var linePath = new google.maps.Polyline({
        path: [ot, [dt][0]],
        strokeColor: '#4A484D',
        strokeOpacity: 1.0,
        strokeWeight: 2,
        geodesic: true,
        icons: [{
            icon: lineSymbol,
            offset: '100%',
            repeat: '60px'
        }]

    });
    arrayLine.push(linePath)

    function setLines(map) {
        for (var i = 0; i < arrayLine.length; i++) {
            arrayLine[i].setMap(map);
        }
    }
}

这是我当前的小提琴:http://jsfiddle.net/8dcgLja9/

0 个答案:

没有答案