Solved

Google Maps smartinfowindow position

Posted on 2013-11-27
6
930 Views
Last Modified: 2013-11-28
I am trying to change the infowindow in Google maps to the smartinfowindow but for some reason the position of the infowindow is wrong. I never had this issue with the standard infowindow, only the smartinfowindow.

I have found that if I remove position: relative from the map canvas using Firefox inspector the map moves to the top left of the window and the viewport seems to extend to the width of the window which results in the smartinfowindows then being in the correct position.

So is there something I'm missing?

I create the markers with the following:

for (var i = 0; i < data.locations.length; i++) {

    var dataPhoto = data.locations[i];
    var latLng = new google.maps.LatLng(dataPhoto.latitude,dataPhoto.longitude);

latlngbounds.extend( latLng );

var marker = new google.maps.Marker({
    position: latLng,
    icon: 'http://www.irishcottageholidays.com/images/cottage1.png'
}); 

listenMarker(map,marker,InfoWindow,dataPhoto.mID)

    markers.push(marker);
}

Open in new window


And then to create the smartinfowindow:

function listenMarker (map,marker,InfoWindow,mID){
google.maps.event.addListener(marker, 'click', function() {
    load_content(map,this,InfoWindow,mID);
});

function load_content(map,marker,InfoWindow,mID){
    var $j = jQuery.noConflict();
    $j.ajax({
        type : 'GET',
        url : '/ajax/map_popup.asp',
        data: {
            mID : mID
        },
        success: function(result){
            new SmartInfoWindow({position: marker.getPosition(), map: map, content: result});
        }
    });
}

Open in new window


Originally I created the infowindows using:

InfoWindow.setOptions({
    disableAutoPan: true,
    maxWidth: 280
});
InfoWindow.setContent(result);
InfoWindow.open(map, marker);

Open in new window


And this worked but now I've changed to the smartinfowindow, it loads the infowindow but not in the right position.

Below is the image of the smartinfowindow after clicking on the circled marker:
Map with smartinfowindow out of position
Here is the page when I have removed position: relative from the map canvas:
Map with position: relative removed from map canvas
Thanks for all help in advance.
0
Comment
Question by:Orroland
  • 4
  • 2
6 Comments
 

Author Comment

by:Orroland
ID: 39680357
If it helps I was working from the example here
0
 

Author Comment

by:Orroland
ID: 39680842
I have found a solution where I have just added a value to the top and left position set in the smartinfowindow.js file to position the window on the marker
0
 

Author Comment

by:Orroland
ID: 39681029
I now have an issue with the smartinfowindow not closing when opening another and have been unable to achieve this so far. Everything I've found so far seem to apply to the standard infowindows and don't work on the smartinfowindow.

Thanks again for all help in advance.
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 38

Accepted Solution

by:
Tom Beck earned 500 total points
ID: 39683710
SmartInfoWindow creates a div to contain the content of the infowindow and appends it to the body. It's position is set to absolute relative to the body. It assumes your map is in the top left corner of the document.

The simple solution is to absolute position the SmartInfoWindow relative to the map canvas instead of the body. To do this you must edit the smartinfowindow.js file on line 178.

Instead of:

document.body.appendChild(div);

Replace with:

var mapDiv = document.getElementById("map");
mapDiv.appendChild(div);


With "map" as the id of the div that holds the map.
0
 
LVL 38

Assisted Solution

by:Tom Beck
Tom Beck earned 500 total points
ID: 39683756
This may work in your case for closing any opened SmartInfoWindows when you open another.

function listenMarker (map,marker,InfoWindow,mID){
    google.maps.event.addListener(marker, 'click', function() {
        if ($('#map > div').length > 1) { $('#map >div:last-child').remove(); }
        load_content(map,this,InfoWindow,mID);
    });
}

It assumes you have jquery on the page already and no other google map add-ons are creating divs inside the map div.
0
 

Author Closing Comment

by:Orroland
ID: 39683797
Thanks for the help, both solutions worked perfectly :-)
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Developer portfolios can be a bit of an enigma—how do you present yourself to employers without burying them in lines of code?  A modern portfolio is more than just work samples, it’s also a statement of how you work.
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question