1

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,

  1. Can I (if so how), by action of the user clicking a link on the same page, retrieve the lat,lng of the top left corner of the polygon and the bottom right, and send to a url, ie;

http://foo.bar/script.php?latoftopleft=17.4479216&longoftopleft=78.37720100000001&latofbotright=17.443172404867163&longofbotright=78.39395944192506

user1724365
  • 87
  • 2
  • 15

1 Answers1

2

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:

Community
  • 1
  • 1
Andrew
  • 2,770
  • 1
  • 22
  • 29
  • not getting the code to fire using.. call the script – user1724365 Oct 08 '12 at 08:22
  • Are there any JavaScript errors? It might also help if you posted your code using something like [jsFiddle](http://jsfiddle.net). – Andrew Oct 08 '12 at 21:19
  • Ah, you are referring to the rectangle class, I was using the drawing tools, I can use the rectangle class but need the code to: figure out the center of the currently displayed map and place a rectangle in the middle of it upon the user clicking an outside link. – user1724365 Oct 08 '12 at 22:14
  • Even if you're using Drawing Tools, you can still obtain the Rectangle instance of what was drawn, by listening to the `rectanglecomplete` event provided by the API. I've updated my answer to include this. – Andrew Oct 09 '12 at 02:06
  • That's the ticket! Took a little sorting to get around my ignorance, lol. That works perfectly. Thanks !! – user1724365 Oct 09 '12 at 02:48