Reputation: 97
Using Google maps API V3, specifically using the drawing tools (only the rectangle is enabled) as seen here:
https://developers.google.com/maps/documentation/javascript/overlays#drawing_tools
But I have the following question,
Upvotes: 1
Views: 2811
Reputation: 2780
Retrieving the Rectangle
One way to retrieve the rectangle drawn by the user is to use Drawing Events:
var rectangle;
google.maps.event.addListener(drawingManager, 'rectanglecomplete', function(newRect) {
rectangle = newRect;
});
Whenever a rectangle is drawn by the user, it will assign the Rectangle overlay object to the rectangle
global variable.
Extracting information about the Rectangle
The google.maps.Rectangle
class has a getBounds()
method, which returns a LatLngBounds
object. You can use the getNorthEast()
and getSouthWest()
methods to deduce the top-left and bottom-right corner coordinates.
You can bind an onclick
event to the link. The click event handler might look something like this:
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.
// ...
}
Further reading:
Upvotes: 2