Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to draw a rectangular area on a large google map and query database to find locations/points that exist inside the rectangle?

I've got an application where I would like to present the end user with a google map and allow them to select an area of the map with a simple rectangular drawing tool and then have all of the locations stored in the client's database that fall in that rectangular selection area show up as points on the map...

I have a simple understanding of google maps and can get google maps to plot all the locations on the database w/o a problem... my problem comes in allowing the end user to draw the rectangle. Not sure how to implement this.

Can someone explain or link me to an example of how it's done?

like image 793
Ryan Avatar asked Mar 06 '11 20:03

Ryan


Video Answer


2 Answers

Looks like this question was asked (and answered) a few months before Google posted about the new drawing tools in v3 API ...

I have to say that using the API's drawing tools gives a better user experience than @rcravens solution. Here's a very specific implementation that lets you "select" an area on the map by drawing a rectangle or polygon, and then checking to see if a marker is inside the shape: http://jsfiddle.net/JsAJA/306/.

like image 160
CrazyTim Avatar answered Sep 17 '22 14:09

CrazyTim


Interesting question. I love the google maps api. Here is a jsFiddle with your solution:

http://jsfiddle.net/JsAJA/2/

You will have to query your database for points between the minimum/maximum lat and lngs. Hope this helps.

Bob

P.S. Note that this breaks the natural user experience of google maps. The map is no longer dragged when you mouse down. It needs a better user experience.

like image 28
rcravens Avatar answered Sep 20 '22 14:09

rcravens