重置Google Map v3 API上的界限

时间:2011-11-21 21:41:20

标签: javascript google-maps-api-3

这绝对是与Reset bounds on Google Maps API v3无关的问题。

我想让用户有机会更改他们搜索的区域。我在邮政编码中放置了位置标记(和“触摸”拉链)。如果他们想要搜索不同的zip,他们可以输入新的邮政编码,我们会重复这个过程。

我遵循谷歌建议的删除标记等方法,但尚未弄清楚如何清除地图边界并从一组新边界开始。下面是每个新邮政编码条目调用的函数。

    MYMAP.placeMarkers = function(zipcode) {
            if(markers.length > 0) {
                for(var i=0;i<markers.length;i++) {
                    markers[i].setMap(null);
                }                   
                markers = new Array();
                var bounds = new google.maps.LatLngBounds();
                MYMAP.map.fitBounds(bounds);                      
            }

          $.post("/locations",{"zip": zipcode}, function (data) {
              data = JSON.parse(data);
              $.each(data, function (key, value) {
                var name = value.name;
                var address = value.address;
                // create a new LatLng point for the marker
                var lat = value.lat;
                var lng = value.lon;
                var point = new google.maps.LatLng(parseFloat(lat),parseFloat(lng));
                // extend the bounds to include the new point
                MYMAP.bounds.extend(point);
                // add the marker itself
                var marker = new google.maps.Marker({
                    position: point,
                    map: MYMAP.map
                });
                // create the tooltip and its text
                infoBubble = new InfoBubble({
                  shadowStyle: 1,
                  padding: 10,      
                  backgroundColor: 'white',
                  borderRadius: 5,
                  arrowSize: 10,
                  borderWidth: 1,
                  maxWidth: 400,
                  borderColor: '#A85E45',
                  disableAutoPan: false,
                  hideCloseButton: true,
                  arrowPosition: 50,
                  backgroundClassName: 'phoney',
                  disableAutoPan: true,
                  hideCloseButton: false,
                  arrowStyle: 0
                }); 
                var html='<b>'+name+'</b><br />'+address;
                // add a listener to open the tooltip when a user clicks on one of the markers
                google.maps.event.addListener(marker, 'click', function() {
                    infoBubble.setContent(html);
                    infoBubble.open(MYMAP.map, marker);
                });
                markers.push(marker);
              });
                // Fit the map around the markers we added:
              MYMAP.map.fitBounds(MYMAP.bounds);
              google.maps.event.trigger(map, "resize");
          });
        }

0 个答案:

没有答案