Dragging map with custom event

528 views
Skip to first unread message

4dllmg

unread,
Nov 18, 2011, 7:41:07 AM11/18/11
to Google Maps JavaScript API v3
Is it possible to simulate map dragging when holding the right click
and moving the mouse?

I have tried to simulate this behavior with the code above, but
dragging is "jumpy":

var drag={};
function rightclickdown(event){
if(!
drag.event)drag.event=google.maps.event.addListener(map,"mousemove",function(e)
{
var lat=e.latLng.lat(),lng=e.latLng.lng();

if(!drag.startLat)drag.startLat=lat;
if(!drag.startLng)drag.startLng=lng;
if(!drag.center)drag.center=map.getCenter();

if(!drag.incLat)drag.incLat=0;
if(!drag.incLng)drag.incLng=0;
drag.incLat=lat-drag.startLat;
drag.incLng=lng-drag.startLng;

var newCenter=new google.maps.LatLng(drag.center.lat()-
drag.incLat,drag.center.lng()-drag.incLng)
map.setCenter(newCenter);
});
}
function rightclickup(event){
google.maps.event.removeListener(drag.event);
delete drag.event;
delete drag.incLat;
delete drag.incLng;
delete drag.center;
delete drag.startLat;
delete drag.startLng;
}

Another way of doing this could be by setting the map to draggable and
triggering the "drag" behavior...but I'm not sure on how to achieve
this.

Thanks in advance.

Pil

unread,
Nov 18, 2011, 8:11:23 AM11/18/11
to Google Maps JavaScript API v3
Why do want to 'simulate' a functionality that is already provided by
the Maps API?
Would be a good idea to describe what you want to achieve - but
without posting code.

4dllmg

unread,
Nov 21, 2011, 3:06:15 AM11/21/11
to Google Maps JavaScript API v3
I want to use the left click by default to select elements in the map
- for example, by creating a rectangular area and selecting elements
within its boundaries -, but want to keep the functionality of map
dragging active. When it comes to selecting elements, users are used
to the left click button. This is basically my reason to relocate the
drag button to the right click or middle mouse button (like in CAD
applications).

On 18 nov, 14:11, Pil <wolf...@gmail.com> wrote:
> Why do want to 'simulate' a functionality that is already provided by
> the Maps API?
> Would be a good idea to describe what you want to achieve - but
> without posting code.
>
> On Nov 18, 1:41 pm, 4dllmg <adelol...@gmail.com> wrote:
>
>
>
>
>
>
>

> > Is itpossibletosimulatemap dragging when holding the right click
> > and moving the mouse?
>
> > I have tried tosimulatethis behavior with the code above, but

Pil

unread,
Nov 21, 2011, 5:11:01 AM11/21/11
to Google Maps JavaScript API v3
What do you mean by "select elements in the map" and
"selecting elements within its boundaries"?

Doesn't seem clear to me what you mean by "selecting", and which
elements should be selected?

4dllmg

unread,
Nov 22, 2011, 3:48:17 AM11/22/11
to Google Maps JavaScript API v3
I am writing several custom functions for selecting markers and
polylines in a map instance (for editing info, moving around,
deleting, etc.). For these I want to use the left click and for map
dragging the mouse middle button.

On 21 nov, 11:11, Pil <wolf...@gmail.com> wrote:
> What do you mean by "select elements in themap" and

> > > > Another way of doing this could be by setting themapto draggable and

4dllmg

unread,
Nov 22, 2011, 4:21:25 AM11/22/11
to Google Maps JavaScript API v3
Solved it. I had to use jQuery to listen to the mousedown and mouseup
events but the effect was achieved correctly.
Reply all
Reply to author
Forward
0 new messages