How to display only exact region on any open-sourc

2019-09-21 06:39发布

问题:

This question already has an answer here:

  • Google Maps v3 - limit viewable area and zoom level 12 answers

I need to show only one region on the map. For example Alaska or New-York city.

回答1:

This solution is relevant to many js libraries. 1. Get boundaries via nominatim.openstreetmap.org API in a geojson format. 2. Set polygon/multipolygon mask on a [base]layer



回答2:

You could find you'r area boundary and set map to limit panning. look at sample below:

var allowedBounds = new google.maps.LatLngBounds(
 new google.maps.LatLng(70.33956792419954, 178.01171875), 
 new google.maps.LatLng(83.86483689701898, -88.033203125)
);
var lastValidCenter = map.getCenter();

google.maps.event.addListener(map, 'center_changed', function() {
   if (allowedBounds.contains(map.getCenter())) {
       // still within valid bounds, so save the last valid position
       lastValidCenter = map.getCenter();
       return; 
   }

   // not valid anymore => return to last valid position
   map.panTo(lastValidCenter);
});

Also you can use this link: https://jsfiddle.net/cse_tushar/9d4jy4ye/