4

I have the following Google Map test app: http://dev.driz.co.uk/googlemap/

As you can see I use geolocation to show your (the user) position on the map, and then some JSON data to populate the map with other markers.

Note: depending where you are in the world you may not see the pins (they are in the UK near Huddersfield) if you zoom out you should seem them.

I am having the following issues:


1.) All the markers have the same titles, so I'm presuming that somewhere in the for loop at the bottom of the page I have made a mistake... Not sure what though?

Fixed in answers below.


2.) The markers have various overlapping issues due to the z-index and also because some of the markers have the same co-ordinates. Is it possible to make it so that markers offset themselves a couple pixels per loop so that they don't overlap, and the z-index automatically increases per loop so they are higher than the previous marker

Need to make it so that when a user hovers the marker it has a higher z-index to make it sit on top... If that makes sense? So in the hover event I need to get the latest offset and then add to that to make it the highest! But how do I alter the zindex of the marker on the hover?


3.) The final thing (and probably the most difficult) is that the tooltips are not equally positioned when moved to the right side of the marker when the map is moved. Any ideas to improve this? They get even worse with the JSON-based markers and slip off the map.


Can anyone help me out with these problems?

Thanks

Cameron
  • 27,963
  • 100
  • 281
  • 483
  • Had to comment as the pins are about half a mile from my house! – Stevo Sep 13 '12 at 19:49
  • Each time you loop you are re-initialising tooltip - it's overwriting the previous value. Hence you always end up with the comment defined last (San Fran Jazz). Maybe instantiate the tooltip inside the addListener event? – Stevo Sep 13 '12 at 20:08
  • Tried that but still getting the same content? – Cameron Sep 13 '12 at 20:13
  • Oh yeah, it's still binding the last value of 'item' to the event. – Stevo Sep 13 '12 at 20:26
  • does this post help? http://stackoverflow.com/questions/8581190/adding-multiple-addlistener-events-to-a-google-map-form-with-geocoding – Stevo Sep 13 '12 at 20:28
  • Actually forget that, you want to make the tooltip adding a callback, I reckon this post should help... http://stackoverflow.com/questions/6939983/creating-a-custom-google-map-with-multiple-markers-and-popup-windows-issues?rq=1 – Stevo Sep 13 '12 at 20:35
  • Could you show how to use that with my loop? As I don't follow. Thanks – Cameron Sep 13 '12 at 20:56
  • I'd suggest breaking up your post into individual more specific questions. No need for bounties. – Marcelo Sep 15 '12 at 06:41

1 Answers1

3

I don't know if this will work, but its following the pattern of that link I shared, perhaps something like this....

    function doToolTip(item) {
        return function () {                        
                        mTooltip = new Tooltip('<span class="name">' + item.User.Profile.firstname + ' asked:</span> <span class="title">' + item.Post.title + '</span>');                  
                        mTooltip.open(this.getMap(), this); 
                    };
        }

...and this is your main code. I think 'item needs' initialising outside the scope of the loop (but I could be wrong)

    //other code etc...

var item;
    for (var i = 0; i < data.length; i++) {
        item = data[i];
        //other code etc....
        google.maps.event.addListener(marker, 'mouseover', doToolTip(item));
        //other code etc...
}

OK. I'm guessing here, as I haven't got a local copy of the code, but, It looks like you need to change the z-index when you do the draw function...

    //code fragment...
    // need to force redraw otherwise it will decide to draw after we show the Tooltip                      
    $(this).css('z-index', 9999);
    this.draw();
// show tooltip

With regard to the position of the tooltip, you're going to have to experiment with the draw function, as it seems to calculate the position from the marker. It might be better to work out the position not from the google map coordinates but from the actual position on the page - I think the culprits are:

    pos = this.getProjection().fromLatLngToDivPixel(this.get('position'));
                // top offset
                top = pos.y - this.getAnchorHeight() / 2 - this.wdiv.outerHeight() / 2;
                // left offset
                if (this.getMap().getCenter().lng() > this.get('position').lng()) {
                    left = pos.x + this.wdiv.outerWidth();
                } else {
                    left = pos.x - this.wdiv.outerWidth();
                }
                // window position
                this.wdiv.css('top', top);
                this.wdiv.css('left', left);

If the positioning is consistently off, you could just apply a correction to the top and left values, if it's more complicated, you'll have to change the algorithm.

Stevo
  • 2,601
  • 3
  • 24
  • 32
  • Okay that fixes question 1 :) thanks. Can you help make it so that the marker that is being hovered gets the highest zIndex as well as help make the positioning of tooltips better? Thanks again. – Cameron Sep 16 '12 at 10:22
  • Oh good! Can you accept the answer so I get my valuable reputation points? ;-) I'll try and have a look at the positioning thing tomorrow. Cheers. – Stevo Sep 16 '12 at 17:56
  • Sure. If you can help fix questions 2 and 3 then I'll accept no problem :) Thanks – Cameron Sep 17 '12 at 09:22
  • @cameron I've added to my answer - does that offer any guidance? I did notice that it only happens when it's trying to redraw the tooltip on the right hand side of the icon. When it redraws on the left it seems OK. – Stevo Sep 19 '12 at 09:07
  • You need to debug it and see what the values are when it renders on the left and renders on the right. The origin is top, left - the height is OK, but the left value is incorrect when rendering on the right hand side. So I reckon it's something to do with a negative value. How about testing for a negative of the left variable and then doing 'this.wdiv.css('right', left);' to change the offset? – Stevo Sep 19 '12 at 09:20
  • Don't seem to be able to get either of those working... Any chance you could throw a jsfiddle together? – Cameron Sep 19 '12 at 12:20
  • @cameron Sorry, without being able to test it, I can't really give you any further assistance. It sounds like you need to understand the fundamentals of what you are trying to achieve and how that is represented in the code, rather than looking for an off the shelf answer. Once you do that, it's probably quite an easy problem to solve. – Stevo Sep 21 '12 at 07:06
  • I know this is old and probably out of date, but I was just reading a bout closures and specifically about loops - so that bit in the loop is suffering from closure issues https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Closures?redirectlocale=en-US&redirectslug=JavaScript%2FGuide%2FClosures – Stevo Oct 05 '13 at 09:38