我试图在谷歌地图上绘制一个可编辑且可拖动的矩形。
我唯一的问题是我找不到给矩形一个固定的高度:只有宽度可以编辑。
我认为这个问题也是一样的: Google Maps Rectangle editable: how to lock (fixed) height from editing
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<title>Rectangle Events</title>
<style>
html, body, #map-canvas {
height: 100%;
margin: 0px;
padding: 0px
}
</style>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script>
<script>
// This example adds a user-editable rectangle to the map.
// When the user changes the bounds of the rectangle,
// an info window pops up displaying the new bounds.
var rectangle;
var map;
var infoWindow;
function initialize() {
var mapOptions = {
center: new google.maps.LatLng(52.0753735,4.3064351),
zoom: 13
};
map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);
var bounds = new google.maps.LatLngBounds(
new google.maps.LatLng(52.0619767211195, 4.295717407226562),
new google.maps.LatLng(52.07294407400174, 4.313404876709001)
);
// Define the rectangle and set its editable property to true.
rectangle = new google.maps.Rectangle({
bounds: bounds,
editable: true,
draggable: true
});
rectangle.setMap(map);
// Add an event listener on the rectangle.
google.maps.event.addListener(rectangle, 'bounds_changed', showNewRect);
// Define an info window on the map.
infoWindow = new google.maps.InfoWindow();
}
// Show the new coordinates for the rectangle in an info window.
/** @this {google.maps.Rectangle} */
function showNewRect(event) {
var ne = rectangle.getBounds().getNorthEast();
var sw = rectangle.getBounds().getSouthWest();
var contentString = '<b>Rectangle moved.</b><br>' +
'New north-east corner: ' + ne.lat() + ', ' + ne.lng() + '<br>' +
'New south-west corner: ' + sw.lat() + ', ' + sw.lng();
// Set the info window's content and position.
infoWindow.setContent(contentString);
infoWindow.setPosition(ne);
infoWindow.open(map);
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<div id="map-canvas"></div>
</body>
</html>