导致在地图中平移和打开标记的链接

jon*_*man 2 google-maps google-maps-api-3 google-maps-markers

JS小提琴:http: //jsfiddle.net/megatimes/NVDLf/7/

我有一个从数组创建多个标记的地图.在地图下方是一些链接,当点击时,我想使地图平移到其各自的标记,并打开信息窗口.

鉴于我不想生成链接本身作为创建标记的循环的一部分,我该怎么做?

我很确定这是一个范围问题,因为地图下面的链接都打开了位置数组中的最后一项,但我似乎无法弄清楚如何绕过它.

谢谢

var locations = [
    [
    "Location 1",
     "215 West Girard Avenue 19123",
    "39.9695601",
    "-75.1395161"
    ],
    [
    "Location 2",
    "5360 Old York Road 19141",
    "40.034038",
    "-75.145223"
    ],
    [
    "Location 3",
    "1350 W Girard Avenue 19123",
    "39.9713524",
    "-75.1590360"
    ]
    ];


var map = new google.maps.Map(document.getElementById('map'), {
    zoom: 12,
    center: new google.maps.LatLng(39.9995601, -75.1395161),
    mapTypeId: google.maps.MapTypeId.ROADMAP
});

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][2], locations[i][3]),
        map: map
    });

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


$(function() {
    $('#locations h3 a').each(function() {
        $(this).on('click', function() {
            google.maps.event.trigger(marker, 'click');    
        })    
      });    
}); 

<div id="map" style="width: 500px; height: 400px;"></div>
<div id="locations">
    <h3><a href="#">Location 1</a></h3>
    <p>Arbitrary content about 1</p>

    <h3><a href="#">Location 2</a></h3>
    <p>Arbitrary content about 2</p>

    <h3><a href="#">Location 3</a></h3>
    <p>Arbitrary content about 3</p>
</div>
Run Code Online (Sandbox Code Playgroud)

geo*_*zip 5

你可以这样做:

http://jsfiddle.net/6vjwd/2/embedded/result/

使用createMarker函数将infowindow与标记相关联:

function createMarker(latlng, html)
{
var marker = new google.maps.Marker({
    position: latlng,
    map: map
});

google.maps.event.addListener(marker, 'click', function() {
        infowindow.setContent(html);
        infowindow.open(map, marker);
});
return marker;
}
Run Code Online (Sandbox Code Playgroud)

还有一个全局数组,允许从HTML单击侦听器中引用它们.

外部链接取决于知道位置的顺序.或者,如果您想通过"名称"查找它们,您可以使用"关联"数组,并将名称作为索引.

按名称索引标记:

http://jsfiddle.net/6nqj8/1/embedded/result/