4

Hello: I'm making progress on my Google Map (see my previous post: KML markers missing on Google Maps API v3: What's wrong?), but I'm now stuck, and hoping for help.

My custom-styled map pulls from a KML file with about 20 Placemarks.

For design reasons, I want my Placemarks to open on the RIGHT side of the anchor, rather than the default top/center. I've tried searching in vain for a simple way to do this; closest I've come is: Issue with infowindows remaining active when another KML layer is selected - Google Maps API V3, which I can't make work for me.

Here is an example of what I'm looking for: http://nationaltreasures.aircanada.com/ (its InfoWindows open to right).

I think I need to supress the default InfoWindow, create my own that pulls the KML data, and then specify a pixelOffset to my custom InfoWindow, but I can't figure out how to do it.

Thank you in advance!

function initialize() {

var styles = [   ]; // Styles removed to simplify code

var styledMap = new google.maps.StyledMapType(styles,
    {name: "HEPAC"});

var mapOptions = { 
    zoom: 7,
    center: new google.maps.LatLng(46.69504, -67.69751),
    panControl: false,
        mapTypeControl: false,
        streetViewControl: false,
    noClear: true,
    zoomControlOptions: {
        position: google.maps.ControlPosition.TOP_RIGHT
    },
    mapTypeControlOptions: {
        mapTypeIds: ['map_style', google.maps.MapTypeId.ROADMAP]
    }
    };                

google.maps.visualRefresh = true;  

var map = new google.maps.Map(document.getElementById("map-canvas"),mapOptions);

map.mapTypes.set('map_style', styledMap);
map.setMapTypeId('map_style');

var opt = { minZoom: 7, maxZoom: 9 }; // Sets minimum & maximum zoom level
map.setOptions(opt);

var ctaLayer = new google.maps.KmlLayer({
    url: 'http://hepac.ca/wp-content/mapping/wellnessnetworksl.kml?f=3',
    preserveViewport: true,
        });

ctaLayer.setMap(map);        

} 

google.maps.event.addDomListener(window, 'load', initialize);
Community
  • 1
  • 1
SPS
  • 304
  • 1
  • 2
  • 16

2 Answers2

11

Thanks to @SeanKendle for pointing me in the right direction. Found more or less what I wanted by adding this into my original code.

  google.maps.event.addListener(ctaLayer, 'click', function(kmlEvent) {
    showInContentWindow(kmlEvent.latLng, kmlEvent.featureData.description);
     });

 function showInContentWindow(position, text) {
    var content = "<div>" + text +  "</div>";
    var infowindow = new google.maps.InfoWindow({
    content: content, 
    position: position,
    pixelOffset: new google.maps.Size(300, 0),
     })
 infowindow.open(map);
}
SPS
  • 304
  • 1
  • 2
  • 16
5

antyrat posted about this with an infoWindow to the right of the marker here:

Googlemap custom infowindow

See the link in the accepted answer.


EDIT: Here's an example. Obviously you will want to include InfoBox.js on your page to get access to that plugin. I hope this works, I didn't test it, but it might point you in the right direction:

function initialize() {

    var styles = [   ]; // Styles removed to simplify code

    var styledMap = new google.maps.StyledMapType(styles,
        {name: "HEPAC"});

    var mapOptions = { 
        zoom: 7,
        center: new google.maps.LatLng(46.69504, -67.69751),
        panControl: false,
        mapTypeControl: false,
        streetViewControl: false,
        noClear: true,
        zoomControlOptions: {
            position: google.maps.ControlPosition.TOP_RIGHT
        },
        mapTypeControlOptions: {
            mapTypeIds: ['map_style', google.maps.MapTypeId.ROADMAP]
        }
    };                

    google.maps.visualRefresh = true;  

    var map = new google.maps.Map(document.getElementById("map-canvas"),mapOptions);

    map.mapTypes.set('map_style', styledMap);
    map.setMapTypeId('map_style');

    var opt = { minZoom: 7, maxZoom: 9 }; // Sets minimum & maximum zoom level
    map.setOptions(opt);

    var ctaLayer = new google.maps.KmlLayer({
        url: 'http://hepac.ca/wp-content/mapping/wellnessnetworksl.kml?f=3',
        preserveViewport: true,
    });

    ctaLayer.setMap(map);   

    google.maps.event.addListener(ctaLayer, 'click', function(kmlEvent) {
        var text = kmlEvent.featureData.description; // ALTER THIS TO POINT TO THE DATA YOU WANT IN THE INFOBOX
        var infoBox = new InfoBox({content: text, latlng: kmlEvent.position, map: map});
    });     

}

Google Maps API says:

Additionally, a click on a KML feature generates a KmlMouseEvent, which passes the following information: position indicates the latitude/longitude coordinates at which to anchor the InfoWindow for this KML feature. This position is generally the clicked location for polygons, polylines, and GroundOverlays, but the true origin for markers. pixelOffset indicates the offset from the above position to anchor the InfoWindow "tail." For polygonal objects, this offset is typically 0,0 but for markers includes the height of the marker. featureData contains a JSON structure of KmlFeatureData.

See this page for more info: KML Feature Details

Community
  • 1
  • 1
Sean Kendle
  • 3,538
  • 1
  • 27
  • 34
  • Thanks for the response. I see he/she has used the InfoBox script, but is manually created a marker -- not pulling from a KML -- so I don't this applies. – SPS Jul 18 '13 at 18:13
  • Why would it matter where the data comes from? Just use the InfoBox script with your KML data. See edit in my answer above. – Sean Kendle Jul 18 '13 at 18:26
  • Thanks @SeanKendle. Can you further explain this part --- var text = kmlEvent.featureData.description; // ALTER THIS TO POINT TO THE DATA YOU WANT IN THE INFOBOX ---- The data is coming from the KML file, but how do I specify that? – SPS Jul 18 '13 at 19:26
  • Thanks @SeanKendle. I understand it now, though something is off with the code because it's crashing the map. Trying now to see how to call the Infobox. – SPS Jul 18 '13 at 19:39
  • Code doesn't appear to work, but I bet it's just something small missing. Anyone? – SPS Jul 18 '13 at 20:12
  • Did you include the InfoBox.js file before your map code? Include it right after the main Google maps js file. http://google-maps-utility-library-v3.googlecode.com/svn-history/r49/trunk/infobox/src/infobox.js – Sean Kendle Jul 19 '13 at 13:42