3

I am using a google maps api v3 to load some data from a kml file. I wish to style the description data when it is shown in an info window to suit my web page.

Now I am trying to set the style:

style="margin-left:-20px;border:2px dotted #897823; et-a;" 

...inside the description tag of a Kml Placemark but it is not rendering it properly.

I can see that firebug just shows up the positives values of margin and padding. It entirely ignores the negative margin values. So, I was wondering, are any limitation in using css style attributes for kml files?

<placemark>
 <name><![CDATA[First Office Address]]></name>
    <description>
      <![CDATA[
        First Line Information<br> 
        California addresss<br>
        Peak valley<br> 
        <div class="cInfo">Telephone<br>
        Office 9089YUHJT General: (2457TYGFR</div>
      ]]>
    </description>
    <Point>
      <coordinates>-420.2300,137.5332200,0</coordinates>    
    </Point>
</placemark>
Fraser
  • 15,275
  • 8
  • 53
  • 104
Mike
  • 3,348
  • 11
  • 45
  • 80
  • Can you give more context... esp. show the div inside the description element and also the description element's parent. – LarsH Dec 07 '11 at 20:44
  • I have something like this: <![CDATA[ First Line Information
    California addresss
    Peak valley
    Telephone
    Office 9089YUHJT General: (2457TYGFR
    ]]>
    – Mike Dec 08 '11 at 05:42
  • please edit your question to include the KML in your comment (formatted); and make sure it includes the description element's parent element. – LarsH Dec 08 '11 at 20:25
  • you have the privileges you can also edit. But i think i have been able to put forward my point. – Mike Dec 09 '11 at 18:55
  • It was a suggestion for how you can make your situation clearer to people you're asking for help from. So far you appear to have received no help. However I won't take ownership from you. – LarsH Dec 09 '11 at 19:48
  • @Larsh: Edited as you wanted me to?? – Mike Dec 09 '11 at 20:14
  • The editing looks good. According to http://code.google.com/apis/kml/documentation/kmlreference.html#feature, "HTML is mostly rendered as it would be in any WebKit browser." Have you tried your embedded HTML (extracted out of KML) in a WebKit browser for comparison? (E.g. Chrome, not Firefox) – LarsH Dec 09 '11 at 20:59
  • @LarsH - the key words being 'mostly' and 'HTML' - not javascript, not css, and not all even all html... – Fraser Dec 12 '11 at 02:45
  • @Fraser, those words are important, but I think to answer the OP's question we have to determine in detail *what* is supported. "As it would be in any WebKit browser" suggests that CSS *is* supported (again, "mostly"), and the OP's experience (and your code) shows that some of it is. – LarsH Dec 12 '11 at 18:11
  • @LarsH Sorry, didn't mean to sound off. My point is that is talking about HTML not CSS - it makes no mention of CSS and I don't think that is what it suggests at all. "HTML is mostly rendered as it would be in any WebKit browser." - means most HTML elements are rendered as they would be in any webkit browser - nothing more... – Fraser Dec 12 '11 at 22:44
  • @Fraser: I disagree on the interpretation. The statement implies that a `

    ` element with long text content will be wrapped...even though it doesn't say anything explicit about text content. Similarly, a `

    ` element with `style="background: red"` will be rendered with a red background, even though it doesn't say anything explicit about CSS. Again, all this is limited by "mostly".

    – LarsH Dec 12 '11 at 22:59

1 Answers1

11

The issue you are having is due to Content scrubbing in the Api. Scrubbing the contents is a security measure to prevent malicious code from executing.

When a feature balloon's description contents are scrubbed, the following code is removed:

  • JavaScript
  • CSS
  • <iframe> tags
  • <embed> tags
  • <object> tags

If you take a look at the markup in a debugger you will see that you are actually getting something like the following:

<div style="font-family: Arial,sans-serif; font-size: small;">
    <div id="iw_kml">
      First Line Information<br> 
      California addresss<br>
      Peak valley<br> 
      <div>Telephone<br>Office 9089YUHJT General: (2457TYGFR</div> 
    </div>
</div>

You don't say how you are opening the info windows, but something like the following should work for you. Basically you suppress the default info window and then build your own custom one.

function initialize() {
    var myLatlng = new google.maps.LatLng(0, 0);
    var myOptions = {
        zoom: 12,
        center: myLatlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    var map = new google.maps.Map(
        document.getElementById("map_canvas"),
        myOptions
    );

    var layer = new google.maps.KmlLayer(
        'http://www.yourserver.com/some.kml', {
            // prevent default behaviour
            suppressInfoWindows: true, 
            map: map
        }
    );

    // bind the event handler
    google.maps.event.addListener(layer, 'click', function(kmlEvent) {
        showInfoWindow(kmlEvent.featureData.description);
    });

    // show a custom info window
    function showInfoWindow(text) {
        // build your window using whatever, styles, embeds or scripts
        // you like. Anything included here will bypass content scrubbing
        var content = "<div style='margin-left:-20px;border:2px dotted #897823;'>" + text + "</div>";
        var infowindow = new google.maps.InfoWindow({
            content: content
        })
    }
}

Obviously you can replace style='...' with class='whatever' which would then allow you to define the CSS style in an external file.

Fraser
  • 15,275
  • 8
  • 53
  • 104
  • I am not displaying this info in infoWindow, rather I am putting this info in a div below my map, which will be shown onclick of a marker, I need this info to go into 2 cols, one having addresses and other phone no's. But I am not able to style the div which has phone numbers and that should go on the right side. – Mike Dec 12 '11 at 06:32
  • 1
    post your code or a link to an example then - chances are this is nothing to do with the Maps API - more likely something in your HTML or CSS. – Fraser Dec 12 '11 at 17:53
  • 1
    Thanks a ton for your help. I wish I could vote up 10 times here. – Mike Dec 14 '11 at 10:17
  • do you have any clues about this: http://stackoverflow.com/questions/8581392/how-to-get-some-information-in-google-maps-without-click-event-on-marker-and-sho/8581445#8581445 – Mike Dec 21 '11 at 11:45
  • @Fraser, how would this work when using Google Earth API instead of Maps? – RoastBeast Oct 23 '13 at 15:54
  • @RoastBeast Pretty much the same principal but just with different objects. You should as your own question about it if you are having problems. You could also check this page which explains quite well https://developers.google.com/earth/documentation/balloons?hl=it#scrubbing – Fraser Oct 24 '13 at 01:26