Moving Polygons

214 views
Skip to first unread message

kilkul

unread,
Dec 19, 2009, 9:12:52 PM12/19/09
to Google Maps JavaScript API v3
Hi,

I've "translated" this great sample:

http://maps.examples.googlepages.com/highlightmarker.html

for API V3, here:

http://facut.ro/highlightmarker.html

The issue, as you might see, is that, unlike V2 of GMaps API, in V3,
moving the polygon(s) in this manner leaves a "dirty" trace behind the
one currently being dragged, while dragging. If the dragging occurs
over an existing (bigger) polygon, the trace is even more visible (and
lasting).

Is there any way of improving this, or will there be (soon) any API
upgrade that might fix this issue?

Thanks!

Michał Biniek

unread,
Dec 20, 2009, 5:27:31 PM12/20/09
to Google Maps JavaScript API v3
Hi,

You can move polygon rather than erasing it and create new. For
example you can make function moveCurrentMarker (which is almost the
same as highlightCurrentMarker) and running it on "drag" event:

google.maps.event.addListener(marker, "drag", function(){
currentMarker = marker;
moveCurrectMarker();
});

Code of moveCurrentMarker:

function moveCurrectMarker() {
var markerPoint = currentMarker.getPosition();
var mapNormalProj = overlayView.getProjection();
var mapZoom = map.getZoom();
var polyPoints = [];
var clickedPixel = mapNormalProj.fromLatLngToDivPixel
(markerPoint);

var polySmallRadius = 20;

var polyNumSides = 20;
var polySideLength = 18;

for(var a=0; a<(polyNumSides+1); a++){
var aRad = polySideLength * a * (Math.PI/180);
var polyRadius = polySmallRadius;

var pixelX = clickedPixel.x + polyRadius * Math.cos(aRad);
var pixelY = clickedPixel.y + polyRadius * Math.sin(aRad);

var polyPixel = new google.maps.Point(pixelX, pixelY);
var polyPoint = mapNormalProj.fromDivPixelToLatLng
(polyPixel);
polyPoints.push(polyPoint);
}


highlightCircle.setPaths(polyPoints);
}

There are still some problems with event 'mouseover' and 'click' -
when I dropped 'click' event the example still works. 'Mouseover'
event sometimes causes, that there are two polygons in the same place
in the same time - so opacity is higher (but only for the second :)

Cheers,
Michal Biniek

kilkul

unread,
Dec 20, 2009, 8:28:44 PM12/20/09
to Google Maps JavaScript API v3

Your idea of using the setPaths method for updating the Polygon's
coordinates was great, I don't need anything else!

I'll just check, when calling highlightCurrentMarker(), if
highlightCircle exists, and if so, I'll just update its paths, instead
of re-drawing it. Thanks!

Reply all
Reply to author
Forward
0 new messages