Search Wiki:
Project Description
This sample illustrates how to display interactive Virtual Earth maps on your Search Server 2008 search results page.

Include Virtual Earth maps in your search results via Federated Search:


The Federated Search Web part makes it possible to display more than results from OpenSearch (1.0/1.1) sites on your search results page. The Search Server 2008 SDK already explains how to include results from SQL Server database queries and search sites that do not expose XML feeds (such as Atom or RSS). In both scenarios, you do this by means of a "connector," a light-weight interface that sends queries to a given location or database, places the results into a structured XML document, and sends that XML to a Federated Search Web Part.

See Federated Search Overview for more information: http://msdn2.microsoft.com/en-us/library/bb931080.aspx

This sample demonstrates that you can extend the connector concept to Web service requests other than search queries. It shows how to use a connector to pass a structured address string to Microsoft's MapPoint Web service in order to obtain the latitude and longitude coordinates of that address. Once you have those coordinates, displaying a Microsoft Virtual Earth map requires only the addition of some Javascript to your Federated Location definition file.

Here’s an example of what it looks like:


VESearch.JPG

Here’s a summary of how the sample works:


  1. Set up a new web site on your server that does nothing but display RSS feeds (and only this one, for now). You can also deploy the code that creates this feed to the _layouts directory of your Search Server site. See this page for guidance about how to set up a Web application on Sharepoint. The instructions also apply to Search Server: http://msdn2.microsoft.com/en-us/library/ms368312.aspx. Setting up a new Web site for creating RSS feeds can be a quicker way to implement a connector, because it lessens the risk of creating conflicting entries in your Web.config files.
  2. Create a Visual Studio Web site in the root directory for the new site.
  3. Add the MapPoint staging Web service to this project. You just add a web reference via one of the urls on this page: http://msdn2.microsoft.com/en-us/library/aa491863.aspx. Note that there are secure and non-secure urls for both the staging and production versions of the service. In my project, This sample uses the non-secure staging version for simplicity’s sake, but you will need to take into account your own requirements when deciding which to use. You will also need to sign up for a free MapPoint developer account here: https://mappoint-css.live.com/mwssignup/.
  4. The Map.aspx document does very little. You just need to make sure that it is using the correct code-behind file and inheriting from the correct class.
  5. The Map.aspx.cs code-behind file does all the work. It takes the query string and passes it to MapPoint’s Find Service, which retrieves the location coordinates (latitude + longitude). It creates a very simple RSS feed containing the first item (if any) of the map result set, including a title (the address string) along with the location coordinates.
  6. The Web.config file contains some boilerplate config for the MapPoint web service. It also contains placeholders for the "mpUser" and "mpPwd" values. You will need to include your own MapPoint credentials as values for these keys. Ideally you would place encrypted values here and decrypt them in your code-behind file, but as written, the sample will accept only plain-text user/password combinations.
  7. Load your new page, whose URL should look something like this: http:<server name:port>/Map.aspx?q=<structured address string>
  8. Verify that location coordinates are being returned in the RSS source.
  9. Add a Federated location, configuring it so that it uses the RSS feed that you have just created. The sample includes a Federated location definition file that you can import directly into your site, if you wish. The location definition includes some Javascript that you can also add directly to the Federated results XSL property for this location. The XSL retrieves the latitude and longitude from the RSS feed and passes it to the Virtual Earth LoadMap method. That method is documented here: http://msdn2.microsoft.com/en-us/library/bb412546.aspx.
  10. Add a Federated Results Web Part that uses this particular location (configuring it so that it loads synchronously), and verify that it is working correctly. Note that the MapPoint query works best when you pass it a correctly formatted address (street address, city, state). This is because the sample uses a MapPoint method that requires a correctly formatted address string. It is also important to note that the Javascript will not work correctly unless you configure your location to load synchronously.

I include below sample Javascript that you can add to your new Federated locations XSL property in order to display a Virtual Earth map. The Javascript assumes that your RSS feed will provide values for "latitude" and "longitude." Add this script underneath the <xsl:call-template name="MainTemplate.description"> section (directly underneath that section's closing tag: </xsl:call-template>):

<script type="text/javascript" language="javascript" xmlns="">
<xsl:text disable-output-escaping="yes">
<![CDATA[
<!--
document.write('<script type="text/javascript" language="javascript" src="http://dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=6"></script>');
//-->
]]>
</xsl:text>
var map = null;
// set page event handlers for onload and unload
if (window.attachEvent) {
window.attachEvent("onload", Page_Load);
window.attachEvent("onunload", Page_Unload);
} else {
window.addEventListener("DOMContentLoaded", Page_Load, false);
window.addEventListener("unload", Page_Unload, false);
}
// load map
function Page_Load() {
GetMap();
}
// Clean up all objects
function Page_Unload() {
if (map!=null) {
map.Dispose();
map = null;
}
}
function GetMap()
{
map = new VEMap('FedMapDiv');
map.SetDashboardSize(VEDashboardSize.Small);
map.LoadMap(new VELatLong(<xsl:value-of select="latitude"/>,<xsl:value-of select="longitude"/>), 10, 'r', false);
var mapdiv = document.getElementById("FedMapDiv");
var yPos = mapdiv.clientHeight - 195;
var location = new VELatLong(<xsl:value-of select="latitude"/>,<xsl:value-of select="longitude"/>);
var shape = new VEShape(VEShapeType.Pushpin, location);
map.AddShape(shape);
map.SetCenterAndZoom(location, 12);
}
</script>
<div id='FedMapDiv' style="position:relative; width:300px; height:300px;">
</div>

Last edited Apr 1 at 8:25 PM  by jamescro, version 7
Updating...