1
votes

I'm struggling to find a way to style the infowindows on this map. I've tried suppressing the infowindow by setting suppressInfoWindows: true but that didn't seem to work. Any ideas would be massively appreciated. I've read through a lot of Google docs and a lot of other posts on here and can't find a solution.

     <script>

var geocoder;
var map; 
var marker;
var layers = [];

function initialize() {
geocoder = new google.maps.Geocoder ();
var latlng = new google.maps.LatLng (51.505288, -0.191544);
var myOptions = {
  zoom: 15,
  disableDefaultUI: true,
  styles: [
        {
           stylers: [

  ]
},

{
  featureType: "poi.park",
  stylers: [
      { color: "#aecfae" },
    { saturation: 0 },
    { lightness: 0 },
    { visibility: "simplified" }



  ]
  },

  {
  featureType: "landscape",
  stylers: [
      { color: "#ffffff" },
    { saturation: 0 },
    { lightness: 0 },
    { visibility: "simplified" }
  ]
},

{
  featureType: "road.highway",
  elementType: "labels",
  stylers: [
    { color: "transparent" },
    { visibility: "off" },

  ]
},

{
  featureType: "water",
  elementType: "geometry.fill",
  stylers: [
    { color: "#a5bfdd" },
    { visibility: "on" },

  ]
},

{
  featureType: "road",
  elementType: "labels",
  stylers: [
    { color: "transparent" },
    { visibility: "off" },

  ]
},

  {
  featureType: "road",
  elementType: "geometry",
  stylers: [
      { color: "#e0e0e0" },
    { saturation: 0 },
    { lightness: 0 },
    { visibility: "simplified" }
  ]
}],
  center: latlng,
  mapTypeId: google.maps.MapTypeId.ROADMAP
    }
  map = new google.maps.Map(document.getElementById("map_canvas"),
    myOptions);

  marker = new google.maps.Marker({map:map});

     layers[0] = new google.maps.KmlLayer('http://www.cid-dev.co.uk/vicarage-phase-2/kml/VicarageGate.kml', {preserveViewport: true});

    for (var i = 0; i < layers.length; i++) {
    layers[i].setMap(map);


  }

  layers[1] = new google.maps.KmlLayer('http://www.cid-dev.co.uk/vicarage-phase-2/kml/PrimarySchools-1.kml',
         {preserveViewport: true});
  layers[2] = new google.maps.KmlLayer('http://www.cid-dev.co.uk/vicarage-phase-2/kml/SecondarySchools-1.kml', {preserveViewport: true});
  layers[3] = new google.maps.KmlLayer('http://www.cid-dev.co.uk/vicarage-phase-2/kml/Culture-6.kml', {preserveViewport: true});
  layers[4] = new google.maps.KmlLayer('http://www.cid-dev.co.uk/vicarage-phase-2/kml/Hotels-2.kml', {preserveViewport: false});
   layers[5] = new google.maps.KmlLayer('http://www.cid-dev.co.uk/vicarage-phase-2/kml/Shopping.kml', {preserveViewport: false});
   layers[6] = new google.maps.KmlLayer('http://www.cid-dev.co.uk/vicarage-phase-2/kml/Restaurants.kml', {preserveViewport: false});
  for (var i = 1; i < layers.length; i++) {
    layers[i].setMap(null);


  }
    }
function codeAddress () {
    var address = document.getElementById ("address").value;
    geocoder.geocode ( { 'address': address}, function(results, status)  {
    if (status == google.maps.GeocoderStatus.OK)  {
        map.setCenter(results [0].geometry.location);
        marker.setPosition(results [0].geometry.location);
        map.setZoom(15);
        } 
    else {
        alert("Geocode was not successful for the following reason: " + status);
            }
}); 
}

function toggleLayer(i) {
  if(layers[i].getMap() === null) {
    layers[i].setMap(map);
  }
  else {
    layers[i].setMap(null);
  }






}


  google.maps.event.addDomListener(window, 'load', initialize);






    </script>



    <div id="map_canvas" style="position:absolute; width:100%; height:100%; top:0px; left:0px; right:0px; bottom:0px; z-index:100; background-color:#000;"></div>
2
How did you use suppressInfoWindows? I don't see it in your posted code. - geocodezip
Sorry geocodezip, I took it out of this code, I was just using {suppressInfoWindows: true}. The infowindows work when I don't suppress them but I want to suppress them in order to show a styled infowindow instead if this makes sense? Instead of the infowindow provided by Google's API with their styling - Rob B
So your question is not "how do I suppress the existing infoWindows", but "how do I add a custom infowindows"? - geocodezip

2 Answers

0
votes

Suppress the automatic info window generation, and use the click event to manually handle the data.

Here's an example that is removing the target="_blank" attribute from the info window links: http://people.missouristate.edu/chadkillingsworth/mapsexamples/removekmllinktargets.js

0
votes

You can try adding a method like this to all your markers.

// generalized click handler
function addClickHandler(item, content, position) {
  google.maps.event.addListener(item, 'click', function () {
    infoWindow.close();
    infoWindow.setContent(content);
    infoWindow.setPosition(position);
    infoWindow.open(map);
  });
}

When I am styling with KML anything passed through in <[CDATA tag is formatted. So adding within the cdata is allowed.

Also heres an example from GOOGLE

http://google-maps-utility-library-v3.googlecode.com/svn/trunk/infobubble/examples/example.html