How to get markers inside an area selected by mouse drag?
Asked Answered
R

1

6

In Brief
I want to find all the markers which lie within a rectangular area created by dragging the mouse over the map. Is this feature supported by any jQuery plugins or something else? If not, I would like to implement this in my project. I think it would be cool.

Details
I am using Google Maps v2 because I want to support IE 6 in my project.

I am a beginner in Maps and went through the Developer's guide section and some basic demos and other SO questions.

Here is my idea so far -

Summary
Here are my questions again -

  • How do I display a rectangular selection area over the map ? (generally slight grey and transparent in appearance)

  • How to get the lat/lang(latitude/longitude) of the clicked point and then extend it to get those of the selection start and end points.

I am experienced with jQuery and if there are some nice solutions with jQuery plugins them please let me know. I saw this list of 10 jQuery Plugins for Easier Google Map Installation but not sure if any of those would help me meet my requirement.

When I have the list of markers, I would like to populate some related info inside a separate display section, so that every time the user selects some markers, corresponding information is displayed in the display section. That part should easier.

Thanks

Roark answered 8/5, 2011 at 12:30 Comment(0)
U
4

You can check KeyDragZoom example under Google Maps API Demo Gallery to achieve rectangular selection feature. You can find the documentation here.

For the second problem you can use GLatLngBounds class and its containsLatLng(latlng:GLatLng) function. By setting your GLatLngBounds object to the bounds of your rectangle you can test your markers against it with containsLatLng function.

If you decide to use KeyDragZoom, adding a dragend listener to the getDragZoomObject will give you a GLatLngBounds object which you can use to test your markers against with the containsLatLng function.

map.enableKeyDragZoom();
var dz = map.getDragZoomObject();
GEvent.addListener(dz, 'dragend', function (bnds) {
  // test your markers against bnds here
});
Udder answered 9/5, 2011 at 6:51 Comment(1)
Yes that solves the problem. For the checking part inside the dragend event above, this solution #2949986 can be followedRoark

© 2022 - 2024 — McMap. All rights reserved.