我正在尝试在地图上设置界限,仅显示某个城市的某个部分。 我目前的代码是:
var strictBounds = new google.maps.LatLngBounds(
new google.maps.LatLng(42.340, -71.20),
new google.maps.LatLng(42.370, -71.0)
);
var mapOptions =
{
maxZoom:19,
minZoom:15,
};
handler = Gmaps.build('Google', {markers: { maxRandomDistance: null} });
handler.buildMap({ provider: mapOptions, internal: {id: 'map'}}, function(){
markers = handler.addMarkers(<%=raw @hash.to_json %>);
handler.bounds.extendWith(markers);
handler.fitMapToBounds();
handler.getMap().setCenter(new google.maps.LatLng(42.359, -71.090413));
});
// 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));
});
然而,界限根本不起作用,用户可以去地图上的任何地方。我有什么想法吗?
答案 0 :(得分:1)
脚本中的东西顺序似乎很奇怪,请执行:
var southWest = new google.maps.LatLng( 42.340, -71.20 );
var northEast = new google.maps.LatLng( 42.370, -71.0 );
var hyderabadBounds = new google.maps.LatLngBounds( southWest, northEast );
var mapOptions =
{
maxZoom:19,
minZoom:15,
bounds: hyderabadBounds
};
handler = Gmaps.build('Google', {markers: { maxRandomDistance: null} });
handler.buildMap({ provider: mapOptions, internal: {id: 'map'}}, function(){
markers = handler.addMarkers(<%=raw @hash.to_json %>);
handler.bounds.extendWith(markers);
handler.fitMapToBounds();
handler.getMap().setCenter(new google.maps.LatLng(42.359, -71.090413));
});
顺便说一下,谷歌地图中没有bounds
选项,see doc