admin管理员组文章数量:1313743
I'm looking for a way to set boundaries around a specific city/town within the Google maps API V3 using JavaScript
Is this type of thing even supported within the API?
Basically i don't want my users to be able to pan across the map further then the city, including country area's not just city limits.
I'm looking for a way to set boundaries around a specific city/town within the Google maps API V3 using JavaScript
Is this type of thing even supported within the API?
Basically i don't want my users to be able to pan across the map further then the city, including country area's not just city limits.
Share Improve this question edited Jul 7, 2014 at 19:27 geocodezip 161k14 gold badges226 silver badges255 bronze badges asked May 29, 2012 at 6:24 NodeDadNodeDad 1,5372 gold badges20 silver badges48 bronze badges3 Answers
Reset to default 2well the boundry that you want to provide to the city/ town can be done. you just need to draw the polylines or polygons specifying the co ordinates of the boundries of the city.
visit demos and documentation for overlays in Gmap V3
try the following code to restrict user to pan across the region.
// Bounds for North America
var strictBounds = new google.maps.LatLngBounds(
new google.maps.LatLng(28.70, -127.50),
new google.maps.LatLng(48.85, -55.90)
);
// 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));
});
this will restrict user to pan across region of north america
You need to get the points for boundries of the city, then you can create a polyline. For example, one idea could be similar to this:
limitCoord = [
new google.maps.LatLng(42.49956716,-7.019005501),
new google.maps.LatLng(42.49947126,-7.029286373),
new google.maps.LatLng(42.50904062,-7.049299123),
new google.maps.LatLng(42.50722622,-7.069103626),
new google.maps.LatLng(42.50452387,-7.000150672),
new google.maps.LatLng(42.49348015,-6.983058917),
new google.maps.LatLng(42.49843269,-6.971666546),
new google.maps.LatLng(42.51765791,-6.956909023),
new google.maps.LatLng(42.52010069,-6.927429186),
new google.maps.LatLng(42.50992238,-6.914231493),
new google.maps.LatLng(42.50096695,-6.879679821),
new google.maps.LatLng(42.48775868,-6.857775832),
new google.maps.LatLng(43.23907504,-3.293216584)], "#000000", 5);
var boundries = new google.maps.Polyline({
path: limitCoord,
strokeColor: "#000000",
strokeOpacity: 1.0,
strokeWeight: 2
});
- Use
map.fitBounds()
ormap.setCenter()
to center the city/country/region on the map. - Use
draggable: false
option to disable panning.
本文标签: javascriptSetting Boundaries around city in google mapsStack Overflow
版权声明:本文标题:javascript - Setting Boundaries around city in google maps - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741957962a2407098.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论