轉:Virtual Earth and AJAX--Part Three

 最後一篇了。發現最近真的懶了,都是轉帖了,唉……難道所有的博客都是這樣的命運嗎?哈哈,一定會努力的

引用地址:http://virtualearth.spaces.live.com/blog/cns!2BBC66E99FDCDB98!536.entry

Tutorial Part 3 - AJAX and Virtual Earth

In part 2 of this tutorial we got the AJAX stuff working, but all the app did was echo back it's input - not terribly exciting. Today we’ll add some simple code to the server component to make a call to the MapPoint Web Service (MWS) to reverse-geocode the coordinate passed in. Reverse geocoding is taking a point on Earth and determining what the corresponding Street Address, City, County, etc… are. Start by trying the application out. When you click the link below the map, notice that you are now presented the approximate address of the center marker on the map.

 

[Note: there will be a part 4 of the tutorial a well - I've had some requests from developers to add other server side functionality and turn on birds eye view. I'll add that in the next day or 2]

 

There aren’t any changes in the client side JavaScript since part 2 – All of the changes are in the Server component. Download the C# source here. You’ll recall from part 2 that the server is responding with javascript telling the browser to present a returned string as the HTML for a DIV tag. We’ve added a function GetLocationInfo() that makes the call to MWS and gets the street address. Lets look at that code.

 

First up, we get a reference to MWS's Find Service. you'll need to fill in your own MWS credentials if building this on your own, which you can do here.

 

FindServiceSoap findService = new FindServiceSoap();

findService.Credentials = new System.Net.NetworkCredential("DEVID","DEVPW");

 

Next up, Create a LatLong object corresponding to the coordinate passed in from the client:

 

LatLong ll = new LatLong();

ll.Latitude = Y;

ll.Longitude = X;

 

We need to tell MWS that we want full street address information returned, not just city state and postcode:

 

GetInfoOptions myOptions = new GetInfoOptions();

myOptions.IncludeAllEntityTypes = true;

myOptions.IncludeAddresses = true;

 

Finrally, we make the call to MWS to do the acual Reverse Geocoding. To keep things simple, we'll return the DIsplay text for the first candidate returned, since they are ordered from best match to least exact match.

 

Location[] locs;

locs = findService.GetLocationInfo(ll, "MapPoint.NA", myOptions);

 

RevGeoResult = locs[0].Entity.DisplayName;

 

That does it for the base of this tutorial. I hope it has been helpful to give you a little peek into the goodness of AJAX and will encourage you to apply it in your own applications. As mentioned above, I'll continue with a part 4 to add some more web service calls and turn on birds eye imagery.

 

發佈了45 篇原創文章 · 獲贊 0 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章