加载中

Building on Windows Phone is great, but not always easy. So, here was my situation. I had the string “Denver, Colorado”. I needed to show it on a map. To show anything on a map I just add a Pushpin. The problem is, a Pushpin requires latitude and longitude.

Fortunately, the Virtual Earth Geocoding Web Service (rebranded the Bing Maps Geocoding Web Service) is here to help. Simply create a developer account and pass the service your address string. It quickly returns every geo-coordinate you could ever need. 

image

I stitched this together and thought I would share it with you – personally. ;) Certainly, I realize you could have figured this out on your own, I just wanted to save you time and get you started.

在Windows Phone上开发的体验是很好的,但也不总是容易。最近我遇到一个问题就是需要在地图上标记一个地点。我需要标记的是“丹佛市,卡罗莱纳州”。现在问题是,要在地图上标记地点就需要用Pushpin,而要用Pushpin就需要知道经度和纬度。 

还好Virtual Earth Geocoding Web Service,也叫做Bing Maps Geocoding Web Service可以解决这个问题。注册一个开发者账号,传入地址信息,Web Service就会返回该地址的经纬度。

Step 1: Create an Bing Map Account

None of this works without a Bing Application Key. Get one free with your Windows Live Id. Just go to https://www.bingmapsportal.com, sign in, and generate a key. Here’s a snapshot of my account; see that long alphanumeric string? That’s what you need in your application.

image13

第一步:创建Bing Map账户 
你需要一个Bing Application Key来完成这个功能。你可以通过你的Windows Live ID来获取这个Key,地址  https://www.bingmapsportal.com/ 
 

Step 2: Add a Map to your Phone Application

Look, it’s totally up to you where you put your Map. You can put it full screen somewhere or in a Pivot or wherever. The panning and zooming is gesture-controlled so you might not even need any control buttons. Either way, your XAML will look something like this:

image24

Yes, that’s it! Now, jam your Bing Application Key from Step 1 into the CredentialsProvider attribute and your map will start working. You can also inject your key in code like this:

第二步:向你的手机APP添加地图 
你可以在你想要的地方放你的地图,它可以是全屏的,也可以是在一个Pivot中。地图的移动和缩放都是手势控制的,所以你不需要放置任何的按钮。你的XAML看起来会是这样的: 
 

然后你需要把你的第一步中拿到的Application Key填充到 CredentialsProvider 中。然后,你的地图就可以开始工作了。当然你也可以用程序的方式注入Application Key: 
 

Step 3: Reference the Geocoding Service

Your Bing Application Key will work for the Geocoding service, too. But first you need to add a Service Reference to your Project. The Geocoding service will parse our address string. These services are amazing – their biggest potential in my mind is address standardization.

Use this address when you setup your reference: 
http://dev.virtualearth.net/webservices/v1/geocodeservice/geocodeservice.svc/mex

After you add the service, calling it is a syntactic cluster. There’s a 1% chance you would ever figure it out without copying the code from somewhere. Here’s a great resource on MSDN. The good news is, once written you can reuse the code easily. Here’s mine:

image

This method is as simple as I think you can write this. It takes an address string like “123 Main Street, Conifer, CO 80433” or something as simple as “Denver, Colorado” and returns the best possible location. It is asynchronous, so it uses a callback; use my method like this:

第三步:引用 Geocoding 服务 
首先你需要添加一个 Service Reference 到你的工程中。然后 Geocoding 服务就会解析地址字符串。这个服务非常好用,我觉得他们很有可能推动地址的标准化。 

你可以参考下面的链接来设置你的reference 
http://dev.virtualearth.net/webservices/v1/geocodeservice/geocodeservice.svc/mex 

你添加了该服务后,调用这个服务是一个标准流程。这个代码非常难懂,基本上你需要Copy&Paste。你可以参考一下MSDN。我的代码如下: 
 

这个方法和简单,它需要一个地址字符串作为输入,例如“123 Main Street, Conifer, CO 80433”或者“Denver, Colorado”,然后这个方法就会返回最接近的地址,它是异步的调用,所以需要一个回调函数,我的方法是这样的: 
 

Step 4: Run it

Believe it or not, you are done. You have established your Bing account, added the Map to your Windows Phone Page, and parsed your address string to a Pushpin-ready location. Wire it all together and run it, hopefully it looks like this:

image

There’s so much more you can do with the Bing Map control. Look through the SDK documentation on MSDN. But hopefully this gets you started so your Phone applications are geo-cool.

第四步:运行 
到这里为止,你的开发任务已经完成了。你下面要做的就是运行这个程序,它应该看起来是这样的: 
 

你还可以用Bing Map做很多事情,你可以参考MSDN上面的SDK。希望我的文章能让你开始使用这个功能,让你的应用也具有位置的特性。

返回顶部
顶部