使用Google maps API V3,特别是使用绘图工具(仅启用矩形),如下所示:
https://developers.google.com/maps/documentation/javascript/overlays#drawing_tools
但我有以下问题,
答案 0 :(得分:2)
检索矩形
检索用户绘制的矩形的一种方法是使用Drawing Events:
var rectangle;
google.maps.event.addListener(drawingManager, 'rectanglecomplete', function(newRect) {
rectangle = newRect;
});
每当用户绘制一个矩形时,它都会将Rectangle覆盖对象分配给rectangle
全局变量。
提取有关矩形的信息
google.maps.Rectangle
class有getBounds()
方法,返回LatLngBounds
个对象。您可以使用getNorthEast()
和getSouthWest()
方法推导出左上角和右下角坐标。
您可以将onclick
事件绑定到该链接。 click事件处理程序可能如下所示:
function clickEventHandler(event) {
// check if the rectangle has been assigned
if (rectangle == undefined) {
alert('No rectangles have been drawn yet!');
return;
}
// obtain the bounds and corner coordinates of the rectangle
var rectangleBounds = rectangle.getBounds();
var northEast = rectangleBounds.getNorthEast();
var southWest = rectangleBounds.getSouthWest();
// deduce the top-left and bottom-right corner coordinates
var topLeft = new google.maps.LatLng(northEast.lat(), southWest.lng());
var bottomRight = new google.maps.LatLng(southWest.lat(), northEast.lng());
// Now that you have the coordinates, all you have to do is use an AJAX
// technique of your choice to send the HTTP request to script.php.
// ...
}
进一步阅读: