单击元素后如何显示和缩放Google Map v3

时间:2016-05-25 11:50:51

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

我最终使用此代码显示谷歌地图:

<script>
function init_map() {

    // Create an array of styles.
    var styles = [
        {
            "featureType": "water",
            "elementType": "geometry",
            "stylers": [
                {
                    "color": "red"
                },
                {
                    "lightness": 25
                }
            ]
        }
    ];

    var myOptions = {
        mapTypeControlOptions: {
            mapTypeIds: ['Map']
        },
        center: new google.maps.LatLng(40.514459, 0.13461),
        zoom:3,
        disableDefaultUI: false,
        mapTypeId: 'Map',
        scrollwheel: false,
        navigationControl: true,
        scaleControl: true,
    };

    var div = document.getElementById('gmap_canvas');
    var map = new google.maps.Map(div, myOptions);
    var styledMapType = new google.maps.StyledMapType(styles, { name: 'Map' });
    map.mapTypes.set('Map', styledMapType);
    var bounds = new google.maps.LatLngBounds();

    marker = new google.maps.Marker({
        map: map,
        //position: new google.maps.LatLng(56.6789471,-0.34535),
        icon: "images/marker.png"
    });
    var markers = [
        ['One', 11.232214,122.155547],
        ['Two', 39.555535,-87.5462367],
        ['Three', 48.3563671,-1.34554825],

    ];

    for( i = 0; i < markers.length; i++ ) {
        var position = new google.maps.LatLng(markers[i][1], markers[i][2]);
        bounds.extend(position);
        marker = new google.maps.Marker({
            position: position,
            map: map,
            icon: "images/ico.png",
            title: markers[i][0]
        });
    }
}

if (document.getElementById("gmap_canvas")) {
    google.maps.event.addDomListener(window, 'load', init_map);
}

上面的代码创建了具有多个点(一,二,三)的谷歌地图。

我想做的是列出这样的联系点,如:

<div class="point">Contact ONE</div>
<div class="point">Contact TWO</div>
<div class="point">Contact THREE</div>

当我点击此联系人地图时,应放大到这一点。 我找到了这个,但是示例只显示了一个点,并且事件在标记点击时触发,而不是自定义Html元素。

https://developers.google.com/maps/documentation/javascript/examples/event-simple

1 个答案:

答案 0 :(得分:1)

您应该将map变量设置为global并将全局变量定义为标记数组(在示例中:markerArr)。(init_map()函数之外):

 <script>
 var map;
 var markerArr = [];
 function init_map() {...
 ...

然后在for循环中将每个标记添加到此数组:

 for( i = 0; i < markers.length; i++ ) {
    var position = new google.maps.LatLng(markers[i][1], markers[i][2]);
    bounds.extend(position);
    marker = new google.maps.Marker({
        position: position,
        map: map,
        icon: "images/ico.png",
        title: markers[i][0]
    });
    markerArr.push(marker); // Add the marker to the array
 }

然后创建一个带整数参数的简单函数,我们称之为jumpToMarker,我们可以在其中缩放并跳转到所选标记。:

function jumpToMarker(cnt){
    map.panTo(markerArr[cnt].getPosition());
    map.setZoom(4);
}

最后在地图的div:

之后在DOM中设置div及其onclick事件
<div class="point"><a href="javascript:void(0);" onclick="jumpToMarker(0)">Contact ONE</a></div>
<div class="point"><a href="javascript:void(0);" onclick="jumpToMarker(1)">Contact TWO</a></div>
<div class="point"><a href="javascript:void(0);" onclick="jumpToMarker(2)">Contact THREE</a></div>

希望这有帮助!