Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Maps: how to position OverlayView accurately over a marker?

I'm using google maps API 3 and a custom overlay (OverlayView) I have this code:

http://jsfiddle.net/8X6cY/1/

please hover the maker with the mouse in order to see the tooltip overlay.

How can I precisely position the tooltip-popup (yellow window) nearby the marker?. My X,Y idea is not working because it is related to the map, therefore if the page's layout is liquid my solution (X,Y) is worthless.

Any idea please?

  google.maps.event.addListener(marker, 'mouseover', function(event) {

    var pixel = latLngToPixel.getProjection().fromLatLngToContainerPixel(event.latLng);

    // Grab marker position
    var pos = [ pixel.x, pixel.y ];

    // Create the tooltip on a dummy div and store it on the marker
    marker.tooltip = $('<div />').qtip({
        content: {
            text: 'I\'m a replacement tooltip for the regular google one<br />New line<br /> new line<br />Newer line',
            title: {
                text: 'aaa',
                button: true
            }
        },
        position: {
            at: "right center",
            my: "left center",
            target: pos,
            container: $('#map_canvas')
        },
        show: {
            ready: true,
            event: false,
            solo: true
        },
        hide: {
            event: 'mouseleave unfocus'
        }
    });

  });
}
like image 301
lito Avatar asked Jan 30 '26 13:01

lito


2 Answers

Just in case someone is looking for an answer, I came up doing this:

// getting marker x,y offset from left-top map
var pixel = latLngToPixel.getProjection().fromLatLngToContainerPixel(event.latLng);
// then get the map_canvas offset with jquery
var left = $('#map_canvas').offset().left;
var top = $('#map_canvas').offset().top;
// x,y position tooltip
var pos = [ pixel.x + left, pixel.y + top];

I hope it helps someone, it took me fair amount of time

like image 95
lito Avatar answered Feb 01 '26 07:02

lito


I've updated it and working, removed at: "right center", my: "left center", hope you want that http://jsfiddle.net/8X6cY/2/

var overlay = new google.maps.OverlayView();
overlay.draw = function() {};
overlay.setMap(map);

var proj = overlay.getProjection();
var pos = marker.getPosition();
var p = proj.fromLatLngToContainerPixel(pos);

Now access your pixel coordinates through p.x and p.y. Found on SO. Hope it will help you.

like image 44
The Alpha Avatar answered Feb 01 '26 06:02

The Alpha



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!