Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Rik Helsen 670 posts 873 karma points
    Dec 08, 2009 @ 11:43
    Rik Helsen
    0

    Adding a Google Map in the content area of a webpage

    I was wondering how you would approach adding a google map to the content area of a page?

    steps involved:

    -Adding a javascript tag to the header section of the page where it is used (with the google maps api key)

    <script type="text/javascript" src="http://www.google.com/jsapi?key=ABCDEFG"></script>

    - creating a div with width and height defining the container for the map:

    <div id="map" style="width: 400px; height: 400px; border: 1px solid #ccc"></div>   

    -loading additional javascript

    <script type="text/javascript">
     
    google.load("maps", "2.x");
       
     
    // Call this function when the page has been loaded
     
    function initialize() {
       
    var map = new google.maps.Map2(document.getElementById("map"));
        map
    .setCenter(new google.maps.LatLng(37.4419, -122.1419), 13);
     
    }
     
    google.setOnLoadCallback(initialize);
    </script>

     

    However when i include all this in an xslt file, i have several validation errors that i need to find a solution for... also i've never succeeded in getting google maps to output anything ...

    All help is very much appreciated!

     

  • Horst Sterr 171 posts 133 karma points
    Dec 08, 2009 @ 12:22
    Horst Sterr
    0

    Hi Rik,

    i did it the easy way.
    In google maps  you have the opportunity to get an embedding code (iframe way) - copy this and paste it into an xslt macro and check "Use in editor".
    Now you can insert this macro into an content page in tinyMCE.

    /horst

  • Petr Snobelt 923 posts 1535 karma points
    Dec 08, 2009 @ 12:43
    Petr Snobelt
    0

    * add script into separate .js file and add link to it

    * put script into cdata section in xslt

  • Rik Helsen 670 posts 873 karma points
    Dec 08, 2009 @ 13:18
    Rik Helsen
    0

    Horst: true but that way you don't have the possibility to set the text inside the balloon, define markers, and all the other stuff the API allows you to customize...

    Petr: how would you add the link to the javascript file in the header section if you want to use the same page templates for all content pages?

     

  • Rik Helsen 670 posts 873 karma points
    Dec 08, 2009 @ 13:29
    Rik Helsen
    0

    This is my temporary solution that embeds it into a page template (but i'd still like to be able to embed it into the content area itself...)

    <%@ Master Language="C#" MasterPageFile="/masterpages/InnerPageMaster.master" AutoEventWireup="true" %>


    <asp:Content ContentPlaceHolderId="additionalPageHead" runat="server">
    <script type="text/javascript" src="http://www.google.com/jsapi?key=ABQIAAAAvtsp6eXg1HIjSwivCNSVFRQVGMMgQ78JOwTIVqc4uOsobDRt_hQM16B8XtzGW9gCZ-nIeesGwCV-NQ"></script>
    </asp:Content>


    <asp:Content ContentPlaceHolderID="ContentPlaceHolderDefault" runat="server">
    <h1><umbraco:Item field="Title" runat="server"></umbraco:Item></h1>
    <umbraco:Item field="PageContent" runat="server"></umbraco:Item>
    <br />
    <div id="map" style="width: 511px; height: 400px; border: 1px solid #ccc"></div>
    <script type="text/javascript">
    google.load("maps", "2.x");

    // Call this function when the page has been loaded
    function initialize() {
    var map = new google.maps.Map2(document.getElementById("map"));
    map.addControl(new GSmallMapControl());
    map.setCenter(new google.maps.LatLng(50.86961044984257, 4.419143199920654),15);

    var info='<div style="background-color: #fff; width: px; font-size: 11px; FONT-FAMILY: Verdana, Tahoma, Arial, Sans-Serif;"><B>Recytyre</B> VZW<BR>Avenue Jules Bordet 164<BR>1140 Evere<BR>Belgium<br></div>';
    var point = new GLatLng(50.86961044984257, 4.419143199920654);
    var marker = new GMarker(point);
    GEvent.addListener(marker, "click", function() {
    marker.openInfoWindowHtml(info);
    });
    map.addOverlay(marker);
    marker.openInfoWindowHtml(info); }
    google.setOnLoadCallback(initialize);
    </script>

    </asp:Content>
  • Petr Snobelt 923 posts 1535 karma points
    Dec 10, 2009 @ 13:10
    Petr Snobelt
    0

    Hi,

    Inserting script with link to external source into header isn't good idea for your page load speed, if google is inaccessible :-) your page try download script and will wait for timeout.

    Why you don't put content of additionalPageHead in front of your second script tag?

    Maybe you should look at http://clientdependency.codeplex.com/ which is for automating including scripts, minifiiing it etc.

    Petr

  • Laurence Gillian 600 posts 1219 karma points
    Dec 10, 2009 @ 13:44
    Laurence Gillian
    1

    Hi,

    When we embed google maps on our websites we use this XSLT and an XML file placed in the Scripts/XML folder.

    We then place the macro at the bottom of our masterpage (to increase load times) and if a GoogleMap is present (set by a document property) the javascript is loaded on the page.

    XML File

    We place the API Key(s) in an XML file so we can support multiple domains. This makes moving from development -> staging -> production quick and painless and it also means you can added keys for localhost or testing IP's if you need to.

    <?xml version="1.0" encoding="UTF-8"?>
    <googleapi name="Google API Code">
      <gapi>
         <domain>www.ourdomain.com</domain>
         <apicode>copy-and-paste-api-code-here</apicode>
      </gapi>
      <gapi>
         <domain>dev-project.companydomain.com</domain>
         <apicode>copy-and-paste-api-code-here</apicode></gapi>
    </googleapi>

    XSLT File

    The XSLT file finds the hostname and then pulls in the relevant API key.

    Detailed

    1. Get the Hostname from the Server Variables and set it to a variable of $hostname
    2. Load the XML file we created above into a variable called $apixml
    3. Load the correct key for the domain ($hostname) into a variable called $apikey, this contains the data within the <apicode> tag.
    4. Then we perform a test on a attribute a document can have, so if googleMaps equals true then load the code.
      This is so the macro can be added on the masterpage, but only spit the Javascript code out when a GoogleMap is present.
      You can of course remove this if test.
    5. If the map is present we then load the correct javascript.
    6. The first script is the GoogleMaps JS file, in this version its using API v2, but you can change this.
    7. The second script is the 'load' script, change this to contain the code you need for your map.
      <xsl:variable name="hostname" select="umbraco.library:RequestServerVariables('HTTP_HOST')"/>
      <xsl:variable name="apixml" select="umbraco.library:GetXmlDocument('/Scripts/XML/googlemapsapi.xml','true')" />
      <xsl:variable name="apikey">
        <xsl:for-each select="$apixml/googleapi/gapi[domain=$hostname]">
          <xsl:value-of select="apicode"/>
        </xsl:for-each>
      </xsl:variable>
    
      <xsl:if test="$currentPage/data [@alias = 'googleMaps'] = 1">
        <script>
          <xsl:attribute name="src">
            <xsl:text>http://maps.google.com/maps?file=api&amp;v=2&amp;key=</xsl:text>;
            <xsl:value-of select="$apikey"/>
          </xsl:attribute>
          <xsl:attribute name="type">
            <xsl:text>text/javascript</xsl:text>
          </xsl:attribute>
        </script>
    
        <xsl:text></xsl:text>
    
        <script>
          <xsl:attribute name="type">
            <xsl:text>text/javascript</xsl:text>
          </xsl:attribute>
          <xsl:attribute name="language">
            <xsl:text>javascript</xsl:text>
          </xsl:attribute>
          <xsl:text>
            GEvent.addDomListener(window, "load", load);
            GEvent.addDomListener(window, "unload", GUnload);
          </xsl:text>
        </script>
    
      </xsl:if>

    Hope this helps! :)

    Laurie

  • Laurence Gillian 600 posts 1219 karma points
    Dec 10, 2009 @ 13:55
    Laurence Gillian
    0

    @Petr - that looks really sweet! (: thanks, i'll have a play with that next week. 

    Maybe you should look at http://clientdependency.codeplex.com/ which is for automating including scripts, minifiiing it etc.
  • bob baty-barr 1180 posts 1294 karma points MVP
    Dec 10, 2009 @ 16:23
    bob baty-barr
    0

    why wouldn't you just use one of the many google maps packages found in the projects section of this website?

  • Olly Berry 47 posts 68 karma points
    Jan 06, 2010 @ 17:33
    Olly Berry
    0

    Horst - I've tried your suggestion as I'm happy to use the iframe method but I have a problem:

    When I visualise my xslt, I can see the map just fine, it's all good, but when I drop the macro into a page in tinyMCE, it doesn't render...

     

    The rendered markup is just

    <?UMBRACO_MACRO macroAlias="GoogleMap" />

    Am I missing something here?

  • Laurence Gillian 600 posts 1219 karma points
    Jan 06, 2010 @ 17:45
    Laurence Gillian
    0

    There is an issue that if the RichText content is rendered using a XSLT Macro, then the Macro contained in the RichText content does not get processed.

    Try loading the content using; (replacing body for whatever is the correct alias for your richtext content).

    <umbraco:Item field="body" runat="server">

    Hope that helps, Laurie

  • Jamie Howarth 306 posts 773 karma points c-trib
    Jan 06, 2010 @ 17:46
    Jamie Howarth
    0

    <?UMBRACO_MACRO /> is the old format for v3, you need to use <umbraco:Macro Alias="GoogleMap" runat="server" /> in v4.

    Hope this helps.

    Benjamin

  • Olly Berry 47 posts 68 karma points
    Jan 06, 2010 @ 18:03
    Olly Berry
    0

    Laurie, you're a genius, thanks very much : )

  • Laurence Gillian 600 posts 1219 karma points
    Jan 06, 2010 @ 18:09
    Laurence Gillian
    0

    No worries! Its always something simple! 

    Hopefully this will be fixed soon ;) /Laurie

Please Sign in or register to post replies

Write your reply to:

Draft