I am working on Google Maps and want to implement a feature where a user can draw a box/rectangle using his/her mouse to select a region on map (like selecting multiple files in windows). Upon selection, I want to get all the markers that fall in the region. I have been looking around both Google Maps api and search but I am unable to find a solution. I tried using jQuery Selectable for selection but all it returns is a bunch of divs from which I am unable to determine if any marker is selected or not.
Asked
Active
Viewed 1.6k times
13
-
that would require some tool for google maps specifically... I tried to briefly search for "google maps rectangle selection" but without success. Have you found anything? – Tomas Nov 25 '11 at 13:31
-
I did find a library that allows you to draw a rectangle (while holding shift key). It then zooms in to that region. I have changed it so that it doesn't zoom in but rather return the geo-coordinates of the selected region. I then loop through all the markers on the Map and select those which are in that region. The name of the library is "keydragzoom" – U.P Nov 28 '11 at 10:39
-
Is it this one? http://google-maps-utility-library-v3.googlecode.com/svn/tags/keydragzoom/1.0/docs/reference.html – Tomas Nov 28 '11 at 11:13
-
If it's a solution to your question you should post it as answer and accept. – Tomas Nov 28 '11 at 11:13
-
Yes this is the library. Ok, I'll post the answer my self – U.P Nov 29 '11 at 06:50
-
great, thanks for solution to this very interesting question! Upvoted your answer. – Tomas Nov 29 '11 at 08:53
1 Answers
9
I found a Library keydragzoom (http://google-maps-utility-library-v3.googlecode.com/svn/tags/keydragzoom/1.0/docs/reference.html) and used it to draw a rectangle on the page.
Later, I edit the library and stopped it from zooming the selected area and instead made it return the correct co-ordinates in 'dragend' event. Then I manually looped through all the marker on the map to find the markers that are within that particular region. The library was not giving me the proper co-ordinates to I made the following changes.
Changed the DragZoom function to
var prj = null;
function DragZoom(map, opt_zoomOpts) {
var ov = new google.maps.OverlayView();
var me = this;
ov.onAdd = function () {
me.init_(map, opt_zoomOpts);
};
ov.draw = function () {
};
ov.onRemove = function () {
};
ov.setMap(map);
this.prjov_ = ov;
google.maps.event.addListener(map, 'idle', function () {
prj = ov.getProjection();
});
}
and DragZoom.prototype.onMouseUp_ function to
DragZoom.prototype.onMouseUp_ = function (e) {
this.mouseDown_ = false;
if (this.dragging_) {
var left = Math.min(this.startPt_.x, this.endPt_.x);
var top = Math.min(this.startPt_.y, this.endPt_.y);
var width = Math.abs(this.startPt_.x - this.endPt_.x);
var height = Math.abs(this.startPt_.y - this.endPt_.y);
var points={
top: top,
left: left,
bottom: top + height,
right: left + width
};
var prj = this.prjov_.getProjection();
// 2009-05-29: since V3 does not have fromContainerPixel,
//needs find offset here
var containerPos = getElementPosition(this.map_.getDiv());
var mapPanePos = getElementPosition(this.prjov_.getPanes().mapPane);
left = left + (containerPos.left - mapPanePos.left);
top = top + (containerPos.top - mapPanePos.top);
var sw = prj.fromDivPixelToLatLng(new google.maps.Point(left, top + height));
var ne = prj.fromDivPixelToLatLng(new google.maps.Point(left + width, top));
var bnds = new google.maps.LatLngBounds(sw, ne);
//this.map_.fitBounds(bnds);
this.dragging_ = false;
this.boxDiv_.style.display = 'none';
/**
* This event is fired when the drag operation ends.
* Note that the event is not fired if the hot key is released before the drag operation ends.
* @name DragZoom#dragend
* @param {GLatLngBounds} newBounds
* @event
*/
google.maps.event.trigger(this, 'dragend', points);
}
};

Daniel Gelling
- 892
- 1
- 9
- 22

U.P
- 7,357
- 7
- 39
- 61
-
How do I intercept the 'points' in my code? I tried using google.maps.event.addListener(map, 'dragend', function(){...}) but it intercepts when I drag to move the map rather than shift-dragging to make the box. – Lev Stefanovich Jul 29 '15 at 18:08