单击元素后如何显示和缩放谷歌地图v3



我最终用这段代码来显示谷歌地图:

<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

您应该将map变量设置为全局变量,并将全局变量定义为标记数组(在示例中: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);
}

最后在 DOM 中设置div 及其 onclick 事件,在映射的div 之后:

<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>

希望这有帮助!

最新更新