How to Integrate Virtual Earth Maps with a SharePoint List

How to Integrate Virtual Earth Maps with a SharePoint List

Rate This
  • Comments 99

Greg Hi everyone,

My name is Greg Chan and I’m a Program Manager in the SharePoint Designer team. I’ve also been involved in the development and testing of the Application Templates for Windows SharePoint Services 3.0. Just a couple weeks ago, I attended Tech Ed in Orlando and presented a breakout session called "Designing and Building Sophisticated Composite Applications with Microsoft Office SharePoint Designer 2007".

Today, I’ll show you how to integrate the Virtual Earth Map control with your SharePoint list. This is actually part of the demo I gave in Tech Ed, and it has generated a lot of interest. Many of you have requested more information on this, so as promised, I’m dedicating this post to it. Some of you may be wondering… "Isn’t this the same as the Plotting Your Data Using Virtual Earth blog that Jon Campbell posted awhile back?" While there are overlaps, the key difference is that this post will talk about plotting a single address as opposed to multiple addresses.  A big benefit for doing single address is that Virtual Earth is good at plotting a single address without the need for longitudes and latitudes.  We simply need to pass in the address value and take advantage of functions provided by Virtual Earth to geocode it. (I don’t know about you, but when I search for driving directions, I’m not typing in longs and lats...)

Let's get started!

Here’s the scenario -- you own a bike shop and use a SharePoint site to manage the manufacturing process for building bicycles. In your site, you have a Suppliers list that stores information about each of your supplier like phone number, address, e-mail, etc. The list is based off the Contacts list. You want to insert a map control inside the display form for each supplier to show the location of the supplier.

Step 1 - Modifying the Display Form

Open in SharePoint Designer the display form (DispForm.aspx) of the SharePoint list containing the address information. Then, we want to use the data view to generate the map. (Yet another reason why we call data view the swiss army knife of web parts...) Below are the detailed steps to do this:

  1. In SharePoint Designer, open up DispForm.aspx under the Suppliers folder.
  2. Bring up the Data Source Library task pane.
  3. Select the Suppliers list and drag it onto the design surface, below the default list form controls.
  4. Bring up the Common Data View Tasks menu for the data view.
  5. Pick the Filter option.
  6. In the Filter Criteria dialog:
    1. Field Name = ID
    2. Comparison = Equals
    3. Value = Create a new parameter
  7. In the Data View Parameters dialog:
    1. Name your parameter (i.e. SupplierID)
    2. Parameter Source = Query String
    3. Query String Variable = ID
    4. Default Value = 36 (optional, I prefer to enter a # that maps to an item that I know already exists in my list)
  8. Hit OK and OK.

Note - All standard SharePoint forms contain an ID parameter in the query section of the URL:

http://<site_name>/Lists/Suppliers/DispForm.aspx?ID=36

Each item in a SharePoint list is mapped to a unique ID value. This ID value is what we're using to filter the data view.

As a result, we’ve just inserted a data view of the suppliers list that is filtered down to show ONLY the current supplier and not all other suppliers. Right now, the data view is still rendered as a table by the default XSLT. Next step, we’re going to write custom XSLT and JavaScript to format the data view as a map!

Step 2 - Use custom XSLT + JavaScript to create Virtual Earth Map control

In order to create the Virtual Earth Map control, we’ll need to utilize some JavaScript functions provided by the Virtual Earth SDK. Below are JavaScript snippets that we need.

Load the map

var map;

 

// Loads the Virtual Earth map control

function loadMyMap() {

            map = new VEMap(&&apos;myMap&apos;);

            map.LoadMap();

            map.FindLocation &apos;<xsl:value-of select="@WorkAddress" />,     <xsl:value-of select="@WorkCity" />, <xsl:value-of select="@WorkState" />,  <xsl:value-of select="@WorkZip" />&apos;, displayPushPinCallBack);

}

FindLocation() will take the address values passed in and center the map to that location. This is a great function! For the function parameters, we’re passing in the XSL values of the Address, City, State and Zip Code field of the supplier.

Add the pushpin

function displayPushPinCallBack() {

      var pin = new VEPushpin(

          1,

          map.GetCenter(),

          null,

          &quot;<xsl:value-of select="@Title" />&quot;,

          &apos;&apos;

          );

       map.AddPushpin(pin);

}

GetCenter() will return the longitude and latitude values of the center of the map, which is currently the location of the address you passed in from FindLocation(). We’re also passing in the XSL value of the Title field of the supplier.

The entire XSL stylesheet

Here’s the entire XSL that we will use to format the data view into a Virtual Earth Map control.

<?xml version="1.0" encoding="utf-8" ?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="html" indent="no"/>

  <xsl:template match="/">

    <xsl:call-template name="dvt_1"/>

  </xsl:template>

  <xsl:template name="dvt_1">

    <xsl:variable name="Rows" select="/dsQueryResponse/Rows/Row" />

    <xsl:call-template name="dvt_1.body">

      <xsl:with-param name="Rows" select="$Rows" />

    </xsl:call-template>

  </xsl:template>

  <xsl:template name="dvt_1.body">

    <xsl:param name="Rows" />

   

    <!-- Link to the Virtual Earth Map control JS file -->

    <script src="http://dev.virtualearth.net/mapcontrol/v4/mapcontrol.js">

    </script>

    <xsl:for-each select="$Rows">

      <xsl:call-template name="dvt_1.rowview" />

    </xsl:for-each>

  </xsl:template>

  <xsl:template name="dvt_1.rowview">

    <div id='myMap' style="width:650px; height:400px;"></div>

    <script type="text/javascript">

 

      _spBodyOnLoadFunctionNames.push(&quot; loadMyMap &quot;);

      var map;

      // Loads the Virtual Earth map control

      function loadMyMap() {

          map = new VEMap(&apos;myMap&apos;);

          map.LoadMap();

          map.FindLocation(&apos;<xsl:value-of select="@WorkAddress" />,

<xsl:value-of select="@WorkCity" />,

<xsl:value-of select="@WorkState" />,

<xsl:value-of select="@WorkZip" />&apos;, displayPushPinCallBack);

      }

      function displayPushPinCallBack() {

          var pin = new VEPushpin(

            1,

            map.GetCenter(),

            null,

            &quot;<xsl:value-of select="@Title" />&quot;,

            &apos;&apos;

          );

          map.AddPushpin(pin);

      }

    </script>

  </xsl:template>

</xsl:stylesheet>

  • _spBodyOnLoadFunctionNames() adds a function to the onload event of the body element. This ensures that loadMyMap() is called whenever the display form is loaded. The Using Javascript to Manipulate a List Form Field blog has a more detailed description of this.
  • The actual XSL is pretty basic. We’re simply using XSL to generate the <div> container for the map control and also passing in XSL values for the JavaScript function calls.

Next thing you'll want to do is take that entire XSL stylesheet and save it out as a separate XSL file. This will make things a lot cleaner and easier to manage. We'll save this out into a file called LiveMaps.xsl, and store it in the folder for the Suppliers list, along with the DispForm.aspx.

Step 3 - Format the data view using LiveMaps.xsl

Now we want to use LiveMaps.xsl to render the data view.

  1. Go back to DispForm.aspx in SharePoint Designer.
  2. Right-click on the data view, which is a Data Form Web Part.
  3. Select Web Part Properties.
  4. Expand the Miscellaneous category.
  5. For XSL Link, click on the button to the right of the input field.
  6. Navigate to the LiveMaps.xsl file and select it.

All Done!

Back on the browser, when you go to the display form of any supplier, there’ll be a great map view that will show you the location of the supplier. You’ll be able to interact with the map, just like you would on http://local.live.com.

Thanks,

Greg

  • Please Help

    I've gone through this 3 times.

    when i attach the xsl file, everything loads but no map appears.

    I know its working correctly because my sharepoint site is https:// and I'm using another VE map elsewhere that prompts me with the same message "this page contains secure and unsecure......"

    everything is in the correct spots - if i remove the xsl link i get the dataview where the map should be poping up - I'm lost?

    PS - I updated the code to VE 6 - but either way 4 or 6 the same thing happens even after I've gone thorugh every suggestion (including the fact my default.master is customized - tried that fix also!!!)

  • Great post! Thanks to all who have contributed to a valuable resource.

    http://sharepointuzma.wordpress.com/

  • I've created an updated XSL file you can use with the Virtual Earth 6.2 API. You can grab it off my new blog post:

    http://blogs.microlinkllc.com/tresing/archive/2009/04/02/virtual-earth-6-2-in-sharepoint.aspx

    -Tom

  • Hi guys,

    I am trying to use the following XSL for transform the coreresults web part in MOSS. I add metadata property mappings for lat and long in central admin then use the follwoing XSLT: However I get a blank DIV with no results any ideas:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >

    <xsl:output  method="html"/>

       <!-- This template is called for each result -->

       <xsl:template match="Result">

           <xsl:text>AddShape(</xsl:text>

           <xsl:value-of select="lat"/>

           <xsl:text>,</xsl:text>

           <xsl:value-of select="lon"/>

           <xsl:text>,"</xsl:text>

           <xsl:value-of select="title"/>

           <xsl:text>");</xsl:text>

       </xsl:template>

       <!-- XSL transformation starts here -->

       <xsl:template match="/">

                   <div id="myMap" style="position:relative; width:400px; height:400px;"/>

           <xsl:text>

           </xsl:text>

                   <script type="text/javascript" language="JavaScript">

                       <![CDATA[

               var map = null;

               var shapes = new Array();

               function CreateMap()

               {

                   map = new VEMap('myMap');

                   options = new VEMapOptions();

                   options.EnableBirdseye=false;

                   optionddShape(lat,lon,desc)s.EnableDashboardLabels=false;

                   map.SetDashboardSize(VEDashboardSize.Tiny);

                   map.LoadMap(new VELatLong(47.6, -122.33), 11 ,'h' ,false,VEMapMode.Mode2d,false,0,options);

                   CreateArray();

                   var shapeLayer = new VEShapeLayer();

                   map.AddShapeLayer(shapeLayer);

                   shapeLayer.AddShape(shapes);

               }

              function AddShape(lat,lon,desc)

              {

                    var shape = new VEShape(VEShapeType.Pushpin, new VELatLong(lat, lon));

                    shape.SetDescription(desc);

                    shapes.push(shape);

                    //If you want to set a custom pushpin icon, use the next line

                    shape.SetCustomIcon('http://myimage.jpg');

              }

              _spBodyOnLoadFunctionNames.push("CreateMap");

               ]]>

                  <xsl:text>function CreateArray() {</xsl:text>

                   <xsl:apply-templates select="//Result"/>

                   <xsl:text>}</xsl:text>

    </script>

           <script type="text/javascript" src="http://dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=6.1"/>

       </xsl:template>

       <!-- End of Stylesheet -->

    </xsl:stylesheet>

    Thanks James

  • Did anyone get the Directions To, and the Directions From to work in the Pushpin.  I saw the code in an earlier post but I am to much of a newby to know where to put it.  Thanks for your help.

  • Doesn't the license for Microsoft's maps restrict you from using their maps internally (behind a firewall, non-public) unless you pay a licensing fee? We looked into this for customers and that's what MS came back to us with, and it was rather expensive!

  • Direkter Download: SPPD-074-2007-07-12 [00:00] Intro [00:00] Buchtipps Microsoft Office SharePoint Server

  • I don't understand this part of the process.

    Query String Variable = ID

    Default Value = 36 (optional, I prefer to enter a # that maps to an item that I know already exists in my list)

    When I add the above I loose all my records.  Can someone explain why this would happen?

  • SQL data source use dsQueryResponse/NewDataSet/Row instead of dsQueryResponse/Rows/Row

Page 7 of 7 (99 items) «34567
Leave a Comment
  • Please add 8 and 8 and type the answer here:
  • Post