移除标记v3时设置缩放

时间:2013-01-30 18:02:37

标签: google-maps google-maps-api-3 google-maps-markers

从一组标记中删除标记时,我想设置缩放级别以尽可能接近所有剩余标记:

这已经回答了API的v2: set zoom level when marker is removed

但我无法将其翻译为v3 ..

<html>
<head>

<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<style type="text/css">
  html { height: 100% }
  body { height: 100%; margin: 0; padding: 0 }
  #map_canvas { height: 100% }
</style>
<script type="text/javascript"
  src="https://maps.googleapis.com/maps/api/js?key=&sensor=false">
</script>


</head>
<body onload="initialize()">
    <div id="title">:: Removing Markers Example Loading:: </div>    
    <script type="text/javascript">
        var bounds;
        var points = {};


                /* Initialize the map */
                function initialize(){
                    var map = null;
                    var mapOptions={
                      center: new google.maps.LatLng(54.749991,-4.18213),
                      zoom: 3,
                      mapTypeId: google.maps.MapTypeId.ROADMAP
                    }
                    map = new google.maps.Map(document.getElementById("map_canvas"),
                    mapOptions);
                    setMarkers(map, postcodes);
                }
                var postcodes = [
                ['EH1',55.952,-3.188],
                ['EH2',55.954,-3.195],
                ['EH3',55.954,-3.2],
                ['EH5',55.975,-3.216]
                ];
                var markersArray = [];
                function setMarkers(map, locations) {
                bounds = new google.maps.LatLngBounds();
                    var infowindow = new google.maps.InfoWindow({content: "holding..."});

                    for (var i = 0; i < locations.length; i++) {
                    var pcd = locations[i];
                    var myLatLng = new google.maps.LatLng(pcd[1], pcd[2]);
                    points[i]=myLatLng;
                    var marker = new google.maps.Marker({
                        position: myLatLng,
                        map: map,
                        html: "<a onclick=\"removeMarker('" + pcd[0] + "','"+i+"');\" href='javascript:void(0);'>Remove " + pcd[0] + "</a>",
                        title: pcd[0],
                    });
                    bounds.extend(myLatLng);
                    markersArray[i] = marker;
                    google.maps.event.addListener(marker, 'click', function(){
                    infowindow.setContent(this.html);
                    infowindow.open(map,this);
                    });



                }

                    map.fitBounds(bounds);

            }
        function removeMarker(pc,markersKey){ 
        markersArray[markersKey].setMap(null);
        points[markersKey]=null;
        bounds = new google.maps.LatLngBounds();
        for (var point in points)
            {
                if (points[point]!=null)
                {
                    bounds.extend(points[point]);
                }
            }
            map.setCenter(bounds.getCenter());
            map.setZoom(map.getBoundsZoomLevel(bounds)-1);
        }

    </script>
    <div id="map_canvas" style="width:350px;height:450px;"></div>
</body>
</html>

0 个答案:

没有答案