步骤 2:为联合搜索 Virtual Earth 映射连接器添加代码

备注

本主题介绍 Infrastructure Update for Microsoft Office Servers中的功能。若要下载此更新,请参阅 SharePoint Server 2007 基础结构更新说明:2008 年 7 月 15 日

以下代码会将用户查询传递到 MapPoint Web 服务,然后在作为连接器的 RSS 源中发布位置坐标。

Federated Search Connector for Virtual Earth Maps Sample中提供了本示例的完整代码。

创建 RSS 源

  1. 创建一个名为 Map.aspx 的文件(通过重命名 Default.aspx 或创建一个新的 Web 表单来创建)。

  2. 在 Map.aspx 文件中,添加或更改 Inherits 页属性,以便该属性使用您将在 Map.aspx.cs(代码隐藏)文件中创建的新类。

    <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="Map" %>
    
  3. 在 Map.aspx.cs 文件中,添加以下命名空间指令。

    using System;
    using System.Configuration;
    using System.Data;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Text;
    using System.IO;
    using System.Web.Services.Protocols;
    using net.mappoint.staging;
    
  4. 修改默认的类声明,使该类声明使用用于连接器的类名称。

    public partial class Map : System.Web.UI.Page
    
  5. 用以下代码替换默认的 Page_Load 方法。

        protected override void Render(HtmlTextWriter writer)
        {
            //Retrieve query term from query string; construct search url
            string queryTerm = Request.QueryString["q"];
            Response.ContentType = "text/xml";
            //Write the RSS document to the HTMLTextWriter
            writer.Write(GetResultsXML(queryTerm));
        }
    
  6. 为 GetResultsXML 方法添加代码,该方法会将查询词传递给 GetMapPointMap 方法。如果查询词是纬度坐标和经度坐标可用的有效位置,则它会创建发布位置坐标的 RSS 源。

        //Return results in RSS format for federated location
        private string GetResultsXML(string queryTerm)
        {
            //Query MapPoint Web service with the URL's query string
            FindResults myFindResults = GetMapPointMap(queryTerm);
            //Begin writing RSS document
            StringBuilder resultsXML = new StringBuilder();
            resultsXML.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
            resultsXML.Append("<rss version=\"2.0\">");
            resultsXML.AppendFormat("<channel><title><![CDATA[HTML to RSS Conversion: {0}]]></title><link/><description/><ttl>60</ttl>", queryTerm);
            //Populate RSS feed with latitude and longitude coordinates, if available, for the first location found.
            if (myFindResults.NumberFound > 0 )
            {
                string title = "Map of " + queryTerm;
                string link = "http://maps.live.com/?q=" + queryTerm;
                double latitude = myFindResults.Results[0].FoundLocation.LatLong.Latitude;
                double longitude = myFindResults.Results[0].FoundLocation.LatLong.Longitude;
                //string description = "&lt;img src=&quot;" + RenderMap(latitude, longitude) + "&quot;&gt;";
                string description = title;
                resultsXML.AppendFormat("<item><title>{0}</title><latitude>{3}</latitude><longitude>{4}</longitude></item>", title, link, description, latitude, longitude);
            }
    
            //Complete RSS document
    
            resultsXML.Append("</channel></rss>");
            return resultsXML.ToString();
        }
    
  7. 为 GetMapPointMap 方法添加代码。此方法假定通过 URL 传递的查询字符串是一个由省/自治区、市/县和街道地址组成的有效的结构化地址。如果查询字符串不是有效地址,则该方法将返回空结果。该方法将在网站的 web.config 文件中查找有效的 MapPoint 凭据。

        private FindResults GetMapPointMap(string queryTerm)
        {
    
            // Create and populate Address object for the FindAddress() operation.
            Address myAddress = new Address();
            myAddress.FormattedAddress = queryTerm;
    
            // Set the ThresholdScore option value to zero so maximum results are returned.
            FindOptions myFindOptions = new FindOptions();
            myFindOptions.ThresholdScore = 0;
    
            // Set up the specification object.
            FindAddressSpecification findAddressSpec = new FindAddressSpecification();
            findAddressSpec.InputAddress = myAddress;
            findAddressSpec.Options = myFindOptions;
            findAddressSpec.DataSourceName = "MapPoint.NA";
    
            // Create a FindResults object to store the results returned by FindAddress.
            FindServiceSoap myFindService = new FindServiceSoap();
            // Set credentials for MapPoint Web service -- retrieve and decrypt the user/password combination from the web.config file.
            myFindService.Credentials = new System.Net.NetworkCredential(ConfigurationSettings.AppSettings["mpUser"], ConfigurationSettings.AppSettings["mpPwd"]);
            myFindService.PreAuthenticate = true;
            FindResults myFindResults = new FindResults();
    
            // Create a FindResults object to store the results returned by FindAddress.
            try
            {
                myFindResults = myFindService.FindAddress(findAddressSpec);
            }
    
            catch (Exception ex)
            {
                //Handle exception
            }
    
            return myFindResults;
    
        }
    
  8. 将必需的 MapPoint 用户凭据添加到您的 web.config 文件中。MapPoint Web 服务将在 web.config 文件的 system.net 和 applicationSettings 节点中查找所需条目。当您将 MapPoint Web 服务添加到网站中时,将为您添加这些条目。此示例中的代码还将查找存储在此文件中的 MapPoint 用户凭据。在网站的 web.config 文件的 configuration 节点中添加以下条目,并为 mpUser 和 mpPwd 键提供适当的值。

      <appSettings>
        <add key="mpUser" value="user"/>
        <add key="mpPwd" value="password"/>
      </appSettings>
    
  9. 将此解决方案部署到您的 Office SharePoint Server 2007 网站。将此解决方案的内容保存在 _layouts 目录中。有关详细信息,请参阅如何:在 SharePoint 网站中创建 Web 应用程序

  10. 在您的 Web 浏览器或 RSS 阅读器中加载 Map.aspx 文件,以验证它是否将创建 RSS 源。向 URL 添加测试地址(?q=地址),并查看来源以验证源是否将返回结果。

后续步骤

步骤 3:创建 Virtual Earth 映射位置并自定义 XSL

See Also

概念

步骤 1:设置用于联合搜索 Virtual Earth 映射连接器的项目