简体   繁体   中英

Changing zoom for marker location on google map?

The marker for the address is zoomed in. How can I get the zoom to be farther out for a single marker? If anyone can help that would be awesome! Here is my JavaScript. thanks for any help

$('#map_here').prepend('<div id="map"></div>');
        $(".static_map").remove();

    var map;
    var bounds;
    var geocoder;
    var center;

    function initialize() {
        var mapOptions = {
            center: new google.maps.LatLng(-34.397, 150.644),
            zoom: 5
        };
        map = new google.maps.Map(document.getElementById("map"),
        mapOptions);
        geocoder = new google.maps.Geocoder();
        bounds = new google.maps.LatLngBounds();
    }
    function addMarkerToMap(location){
        var marker = new google.maps.Marker({map: map, position: location});
        bounds.extend(location);
        map.fitBounds(bounds);
    }
    initialize();

    $("address").each(function(){
        var $address = $(this);
        geocoder.geocode({address: $address.text()}, function(results, status){
            if(status == google.maps.GeocoderStatus.OK) addMarkerToMap(results[0].geometry.location);
        });
    });

    google.maps.event.addDomListener(map, "idle", function(){
        center = map.getCenter();
    });

     $(window).resize(function(){
            map.setCenter(center);
        });            

Found out what worked. I added this code before map.fitBounds(bounds); and it worked:

google.maps.event.addListenerOnce(map, 'bounds_changed', function(event) {
    if (this.getZoom() > 15) {
          this.setZoom(15);
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM