简体   繁体   中英

Crop part of map using Google Maps API v3

How can I set bounds for Google Maps widget? For example, I need only [SW corner: 40.5, -25.5] - [NE corner 79.5, 178.5] rectangle. Now I'm using such code from another question:

var strictBounds = new google.maps.LatLngBounds(
    new google.maps.LatLng(40.5, -25.5), 
    new google.maps.LatLng(79.5, 178.5)
);

// Listen for the dragend event
google.maps.event.addListener(map, 'dragend', function() {
    if (strictBounds.contains(map.getCenter())) return;

    // We're out of bounds - Move the map back within the bounds

    var c = map.getCenter(),
        x = c.lng(),
        y = c.lat(),
        maxX = strictBounds.getNorthEast().lng(),
        maxY = strictBounds.getNorthEast().lat(),
        minX = strictBounds.getSouthWest().lng(),
        minY = strictBounds.getSouthWest().lat();

        if (x < minX) x = minX;
        if (x > maxX) x = maxX;
        if (y < minY) y = minY;
        if (y > maxY) y = maxY;

        map.setCenter(new google.maps.LatLng(y, x));
  });

But it doesn't give needed effect - anyway, this code allows to zoom map and user sees fragments across the strict bounds.

So, I would like to know, is there any method in API v3 to crop nesseasary part from full world map?

I think in your case you can just additionally disallow user to zoom out of your bounds by setting corresponding minZoom property

You can do it when initializing map, or after its already initialized like this:

 map.setOptions({minZoom:5});

Finally, I decided to solve this problem as geocodezip advised. For details see source code of this page .

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