MapPoint URL Windows Mobile中的Web Service应用 - 《msdn开发精选》

来源:互联网 发布:起名软件在线 编辑:程序博客网 时间:2024/05/16 14:42
 

Verify Credentials

After creating a password on the Manage Password page, you can use this page to verify that your Bing Maps developer account credentials (account ID and password) are valid. These credentials are separate from the credentials that you use to access this Customer Services site.

Note If you are using an evaluation account, you cannot access the production environment.

  • Bing Maps Token Service Production WSDL: https://common.virtualearth.net/find-30/common.asmx?wsdl
  • Bing Maps Token Service Staging WSDL: https://staging.common.virtualearth.net/find-30/common.asmx?wsdl
  • MapPoint Web Service Production WSDL: http://service.mappoint.net/standard-30/mappoint.wsdl
  • MapPoint Web Service Staging WSDL: http://staging.mappoint.net/standard-30/mappoint.wsdl

 

 

声明
  本文来自《msdn开发精选》杂志2005年第4期“特别策划”栏目

 

  Web Service对于开发者来说已经不再是一个陌生概念了。Web Service允许应用程序通过Internet进行通讯和数据交换。随着微软.NET战略的推行,这种可编程的、通过SOAP协议、基于XML的网络协议已经赢得了开发者的心。

  现在,随着无线数据网络的发展,移动设备如何访问网络资源的问题显得格外突出。我们目前已经拥有了GPRS和CDMA的广域数据通讯网络,很多地方也覆盖了WI-FI的无线数据局域网。如何让我们的应用程序更好的利用网络资源?

  Windows Mobile移动设备为我们提供了.NET CompactFramework,为我们开发移动应用程序带来的了极大的方便。.NET Compact Framework也提供了方便的WebService访问方式,帮助开发者来编写基于Web Service的移动应用程序。

 目前来看,Windows Mobile应用程序访问Web Service的应用主要出现在两个方面:首先是个人应用程序需要获取公共数据服务,比如获取天气预报、地理信息等;其次是,基于Windows Mobile的企业级应用将核心服务发布成Web Service形式,由移动应用程序进行调用。

  我们这次主要讨论调用公众数据服务Web Service的情况,以Google和MapPoint Web Service为样本,讲述如何在Windows Mobile应用程序中调用搜索引擎和地图服务。

访问Google Web Service

  Google是目前网络上最流行的搜索引擎之一,关键字搜索完全改变了网络信息的组织形式。Google很早就提供了公共WebService服务,允许应用程序访问Google提供的搜索、拼写检查和页面缓存的功能。Google WebService的每个帐号一天最多查询一千次。

  在使用Google之前,我们必须去www.google.com/apis,申请一个Google Account。注册成功后,会收到一个Google的关键字,每次调用的时候,都要提供这个关键字。

  在申请成功后,我们可以动手开发自己的应用程序了。我们在Visual Studio 2005 Beta 2中创建一个DeviceApplication。然后我们要为Google Web Service添加一个Web引用。在Project菜单中选择Add WebReference,在URL里输入 http://api.google.com/GoogleSearch.wsdl,选择AddReference按钮就可以了。这样,我们就把Google Web Service添加到我们的应用程序中来了。

  我们先来认识一下Google Web Service中的对象。这些对象设计得和Google的主页一样简单。

对象 说明 DirectoryCategoryODP目录的内容和种类GoogleSearchResultGoolge查询结果GoogleSearchServiceGoogle查询服务对象ResultElement查询结果集元素

  我们下面就来用这些对象来编写一个简单的Google查询的程序。我们首先要介绍一个重要的API:GoogleSearchService下的doGoogleSearch。

public GoogleSearchResult doGoogleSearch(
    string key, // Google Key
    string q, // 查询内容
    int start, // 从第几条结果开始返回
    int maxResults, // 最多返回多少条(最多10条)
    bool filter, // 是否自动过滤相似或来源相同的结果
    string restrict, // 限制查询结果来自Google Web 索引的一个子集
    bool safeSearch, // 是否过滤成年人内容
 string lr, // 语言限制(”lang_zh-CN”为中文)
    string ie, // 输入编码
    string oe) // 输出编码

  在某些情况下,我们需要设置代理服务器来访问一些网络资源,所以,我们要引入System.Net的命名空间,然后创建一个Web Proxy赋给GlobalProxySelection,才能保证我们调用Web Service的正常。

WebProxy proxy = new WebProxy("itgproxy",80);
proxy.BypassProxyOnLocal = true;
GlobalProxySelection.Select = proxy;

  在上面的代码里我们首先创建一个WebProxy,地址为” itgproxy”,端口为80。然后选择本地地址不使用代理服务器,最后设置到GlobalProxySelection中。

  下面我们把完整的代码写出来:

try
{
    WebProxy proxy = new WebProxy("itgproxy",80);
    proxy.BypassProxyOnLocal = true;
    GlobalProxySelection.Select = proxy;

    GoogleSearchService service = new GoogleSearchService();
    GoogleSearchResult result = service.doGoogleSearch(
        key, // 查询Key
        textBox1.Text, // 内容
        0, // 从第几条结果开始返回
        10, // 最多返回的条数(最大10条)
        false, // 是否自动过滤相似或来源相同的结果
        "", // 限制查询结果来自Google Web索引的一个子集
        false, // 是否过滤成年人内容
 "", //"lang_zh-CN", // 语言限制
        "", // 输入编码
        ""); // 输出编码

    for(int i = 0; i <= (result.endIndex - result.startIndex); i++)
    {
        ResultElement element = result.resultElements[i];
        listBox1.Items.Add(element.title);
        listBox1.Items.Add(element.snippet);
        listBox1.Items.Add(element.URL);
    }

    MessageBox.Show(result.estimatedTotalResultsCount.ToString());
}
catch (System.Web.Services.Protocols.SoapException ex)
{
    MessageBox.Show(ex.Message);
}

 在设置好代理服务器后,我们创建一个GoogleSearchService的对象,然后调用该对象的doGoogleSearch方法。我们将得到一个GoogleSearchResult的结果集,通过endIndex和startIndex属性,我们可以获得查询结果的数量。我们从结果集对象的resultElements中获得ResultElement对象,然后把标题、简介和URL显示到listBox中去。最后我们可以用结果集对象的estimatedTotalResultsCount属性,来获取一共找到了多少个结果。我们上边曾经说过,每次查询最多只能返回10个结果,但是estimatedTotalResultsCount返回的是所有符合条件的记录。

访问MapPoint Web Service

  MapPoint是微软提供的一个地图信息的WebService,提供全球各地的地理信息和商业网点信息(主要是北美和欧洲地区)。MSN Search推出的地理搜索服务也是基于MapPointWeb Service服务的。目前MapPoint还不包括中国的地理信息。

  Windows Mobile设备的存储空间比较小,所以很难保存大量的地图数据。但是随着无线网络的普及,WindowsMobile设备就可以方便地查询保存在服务器上的地图数据和黄页信息。MapPoint Web Service还可以结合WindowsMobile设备上的GPS定位系统或者LBS服务,查询附近的旅游景点和商业场所,这对于移动应用也具有十分现实的意义。

  MapPoint是一个收费的WebService,不过,我们可以申请一个60天的测试帐号。MSDN的订户还可以申请一个为期一年的帐号,用于应用程序的开发。在申请到帐号后,我们不但可以查询公共的地理信息,还可以上传自己的地理信息,用于支持自定义的地理信息查询。

  我们创建一个Windows Mobile Device Application,然后添加一个Web Reference,URL为“http://staging.mappoint.net/standard-30/mappoint.wsdl”。

 在编写程序之前,我们先来看一下MapPoint中需要用到的类和方法。

类方法 说明 FindServiceSoap包括查询服务(find service)的方法和属性RenderServiceSoap包括查询结果表示服务(render service)的方法和属性FindSpecification用于指定FindServiceSoap.Find的查询信息FindResultsFindServiceSoap查询操作的单个结果ViewByScale包含地图定义地图比例和基准点MapOptions包含RenderServiceSoap.GetMap方法的地图显示选项Pushpin包括图钉的图标、标签、定位、用户指定数字MapSpecification包含地图显示的详细规范MapImageRenderServiceSoap.GetMap返回的地图

  我们首先来创建MapPoint查询过程中最重要的两个对象:

FindServiceSoap fws = new FindServiceSoap();
RenderServiceSoap ws = new RenderServiceSoap();
fws.Credentials = new NetworkCredential(myUserName, myPassword);
ws.Credentials = new NetworkCredential(myUserName, myPassword);

  FindServiceSoap是提供地图查询服务的主要对象,RenderServiceSoap是表现查询结果地图的主要对象。在使用之前,我们要先创建一个NetworkCredential对象,将MapPoint的用户名和密码传给FindServiceSoap和RenderServiceSoap的Credentials属性。

  然后我们创建一个FindSpecification对象,指定地图数据库、要查询的地点,然后将其作为FindServiceSoap的Find方法的参数执行查询,并返回FindResults对象中。

FindSpecification myFindSpec = new FindSpecification();
myFindSpec.DataSourceName = "MapPoint.NA";
myFindSpec.InputPlace = this.textBox1.Text;

FindResults results = fws.Find(myFindSpec);
if (0 == results.NumberFound)
{
    MessageBox.Show("你找的是什么鬼地方?");
    return;
}

  我们选择的地图数据源是北美地区的数据集“MapPoint.NA”,而InputPlace是要查询的地点,从界面上的文本框中获得。然后调用FindSpecification的Find方法,返回FindResults对象。如果结果集对象的NumberFound属性为0,则说明没找到合适的结果。

  然后我们创建一个ViewByScale对象,为该对象的CenterPoint属性创建LatLong对象。CenterPoint属性的经纬度分别results.Results[0].FoundLocation.LatLong中的经纬度。然后在ViewByScale对象的MapScale属性中设置地图的比例尺。创建MapOptions对象,在MapOptions.Format设置地图的宽度和长度。创建一个图钉对象Pushpin,将图钉对象的经纬度设置为ViewByScale对象的经纬度。

  在创建完这些对象后,我们创建一个MapSpecification对象用于指定要显示地图的详细情况。我们要指定MapSpecification对象的地图数据集,并将前边创建的三个对象分别赋给Options、Pushpins、Views属性中。

MapSpecification myMapSpec = new MapSpecification();
myMapSpec.DataSourceName = "MapPoint.NA";
myMapSpec.Options = options;
myMapSpec.Pushpins = pushpins;
myMapSpec.Views = views;

  在设置完成MapSpecification对象后,我们将其作为参数,调用RenderServiceSoap对象的GetMap方法,获取MapImage对象。然后将MapImage对象中的数据作为MemoryStream内存流,用于创建一个Bitmap对象,最后显示到PictureBox中。

MapImage[] images = ws.GetMap(myMapSpec);
this.pictureBox1.Image = new System.Drawing.Bitmap(new System.IO.MemoryStream(images[0].MimeData.Bits));

  完整代码如下:

WebProxy proxyObject = new WebProxy(proxy, 80);

// Disable proxy use when the host is local.
proxyObject.BypassProxyOnLocal = true;

// All requests this proxy information.
GlobalProxySelection.Select = proxyObject;

FindServiceSoap fws = new FindServiceSoap();
RenderServiceSoap ws = new RenderServiceSoap();
fws.Credentials = new NetworkCredential(myUserName, myPassword);
ws.Credentials = new NetworkCredential(myUserName, myPassword);

FindSpecification myFindSpec = new FindSpecification();
myFindSpec.DataSourceName = "MapPoint.NA";
myFindSpec.InputPlace = this.textBox1.Text;

FindResults results = fws.Find(myFindSpec);
if (0 == results.NumberFound)
{
    MessageBox.Show("你找的是什么鬼地方?");
    return;
}

ViewByScale[] views = new ViewByScale[1];
views[0] = new ViewByScale();
views[0].CenterPoint = new LatLong();
views[0].CenterPoint.Latitude = results.Results[0].FoundLocation.LatLong.Latitude;
views[0].CenterPoint.Longitude = results.Results[0].FoundLocation.LatLong.Longitude;
views[0].MapScale = 150000;

MapOptions options = new MapOptions();
options.Format = new ImageFormat();
options.Format.Height = this.pictureBox1.Height;
options.Format.Width = this.pictureBox1.Width;

Pushpin[] pushpins = new Pushpin[1];
pushpins[0] = new Pushpin();
pushpins[0].IconDataSource = "MapPoint.Icons";
pushpins[0].IconName = "0";
pushpins[0].Label = results.Results[0].FoundLocation.Entity.Name;
pushpins[0].LatLong = views[0].CenterPoint;
pushpins[0].ReturnsHotArea = true;

MapSpecification myMapSpec = new MapSpecification();
myMapSpec.DataSourceName = "MapPoint.NA";
myMapSpec.Options = options;
myMapSpec.Pushpins = pushpins;
myMapSpec.Views = views;

MapImage[] images = ws.GetMap(myMapSpec);
this.pictureBox1.Image = new System.Drawing.Bitmap(new System.IO.MemoryStream(images[0].MimeData.Bits));

  我们上面演示的仅仅是最简单的地图查询功能,我们还可以通过FindAddress, FindByID, FindByProperty,FindNearby 或者 FindNearRoute等方法来查询地址、查询附近的地点等。我们还可以通过MapPoint WebService来实现智能路标的功能。

写在最后

  上面我们一起了解了如何在Windows Mobile应用程序中加入Google和MapPoint的WebService。我们还可以通过Web Service来发送短信,获取天气预报。越来越多的服务提供商也开始把自己的服务发布成WebService,使自己的服务不但可以提供给终端用户,还可以提供给应用程序开发者,从而扩大自己的用户群。

  现在Web Service和Windows Mobile应用程序都处在发展期。随着无线网络的成熟,加上其他的智能手机操作系统也开始支持Web Service,我们会看到越来越多应用于移动设备上的Web Service案例。

原创粉丝点击