MapPoint Forums

MapForums

Community of VE/MapPoint Users and Developers




Geocoding with the Virtual Earth Web Service

This is a discussion on Geocoding with the Virtual Earth Web Service within the Virtual Earth Blogs forums, part of the Blogs category; For those of you who may want to get access to the 85 million unique rooftop or parcel centroid accurate ...


Go Back   MapPoint Forums > Blogs > Virtual Earth Blogs

Register Blogs FAQ Members List Calendar Search Today's Posts Mark Forums Read



Click here to register

Reply

 

LinkBack (1) Thread Tools Display Modes
  1 links from elsewhere to this Post. Click to view. #1 (permalink)  
Old 09-30-2008
Senior Member
Blue Belt
 
Join Date: Sep 2007
Posts: 202
Geocoding with the Virtual Earth Web Service

For those of you who may want to get access to the 85 million unique rooftop or parcel centroid accurate points we've aggregated in the Virtual Earth Web Service (VEWS), I figured it was only fair that I post a little something to help move you along. Hopefully, this helps you get past a sticking point in your application development. Also, I did all of this on staging so everyone who has an account can do this.

Okay, so, first things first, you need to authenticate - see my post Authentication and Tokens with Virtual Earth for authentication. You'll need a token to use any part of the VEWS and there's a good explanation on how to do that for VEWS in my other post (and information on getting a map) - Getting a Map with the Virtual Earth Web Service, but I'll copy the code to be nice.

The application consists of 3 files - Authentication.cs (a class file for getting me a token); Geocoding.aspx (the UI file for adding web parts); and, Geocoding.aspx.cs (the code behind file where the magic happens). As for my code, it's only meant to get you up and running to understand architecture - it is not production code. You'll want error handling and try catches and all the good stuff - this is just the meat and potatoes.

Authentication.cs

using System;
using System.Data;
using System.Configuration;
using VEWSStaging; //VEWS is a reference to http://staging.common.virtualearth.net/find-30/common.asmx

///


/// Authenticating for VE Tokens
///

public class Authentication
{
    public static string strVEWSToken;
    public static string Authenticate(string strIP)
    {
        CommonService commonService = new CommonService();
        commonService.Url = "https://staging.common.virtualearth.net/find-30/common.asmx";
        commonService.Credentials = new System.Net.NetworkCredential("XXXX", "YYYY");

        // Create the TokenSpecification object to pass to GetClientToken.
        TokenSpecification tokenSpec = new TokenSpecification();

        // Use the Page object to retrieve the end-client’s IPAddress.
        tokenSpec.ClientIPAddress = strIP;

        // The maximum allowable token duration is 480 minutes (8 hours).
        // The minimum allowable duration is 15 minutes.
        tokenSpec.TokenValidityDurationMinutes = 480;

        // Now get a token from the Virtual Earth Platform Token service.
        strVEWSToken = commonService.GetClientToken(tokenSpec);
        return strVEWSToken;
    }
}

Next, I'll need to set a reference to the geocoding service (WCF) - Website | Add Service Reference. I named my service "GeocodeService" which you can see has two methods - Geocode and ReverseGeocode.

image 

Next, we'll need to add UI elements to Geocoding.aspx. I added a Label (as a title), a Text box (to input addresses or places), a Button (to submit the data) and a Table (to store the results). When I'm done, Geocoding.aspx looks like this...

Geocoding.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Geocoding.aspx.cs" Inherits="Geocoding" %>

http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

http://www.w3.org/1999/xhtml">

    CP's Geocoding Page


   


   

        Enter a location

   
            Text="Geocode!" Width="80px" />
            Width="500px">
   

   


And now for the magic. We'll use these elements to make a service request to VEWS to submit the data in the text box and geocode the location. I'll take the entire response and break up each part so you can see most of what gets returned in the response. Make sure you add a reference to the GeocodeService we added above.

Geocoding.aspx.cs

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using GeocodeService;

public partial class Geocoding : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        //Get user IP for token
        string token = Authentication.Authenticate(Page.Request.UserHostA ddress);
        //Instantiate geocode request
        GeocodeRequest myGeocodeRequest = new GeocodeRequest();
        //Instantiate credentials for geocode request
        myGeocodeRequest.Credentials = new GeocodeService.Credentials();
        //Add token to geocode request
        myGeocodeRequest.Credentials.Token = token;
        //Using GeocodeRequest.Query so we don't need a structured request - just single box
        myGeocodeRequest.Query = TextBox1.Text;
        //Instantiate confidence filter - I just want one filter
        ConfidenceFilter[] myConfidenceFilter = new ConfidenceFilter[1];
        //Instantiate filter
        myConfidenceFilter[0] = new ConfidenceFilter();
        //!Had to specify Geocode Service because Confidence is defined also in Imagery Service!
        //Setting confidence to low so I get maximum results
        myConfidenceFilter[0].MinimumConfidence = GeocodeService.Confidence.Low;
        //Instantiate Geocode options
        GeocodeOptions myGeocodeOptions = new GeocodeOptions();
        //I only want at most 5 results
        myGeocodeOptions.Count = 5;
        //Pass in my confidence filter
        myGeocodeOptions.Filters = myConfidenceFilter;
        //Pass in my geocode options
        myGeocodeRequest.Options = myGeocodeOptions;
        //Instatiate geocode service client for connection to VEWS
        GeocodeServiceClient myGeocodeServiceClient = new GeocodeServiceClient();
        //Physically making VEWS request!
        GeocodeResponse myGeocodeResponse = myGeocodeServiceClient.Geocode(myGeocodeRequest);
        //Loop through results to pull out response and put into a table
        //I'll only get 1 response, but it could have multiple results
        for(int i = 0; i < myGeocodeResponse.Results.Length; i++)
        {
            TableRow tRow = new TableRow();
            Table1.Rows.Add(tRow);
            TableCell tCell0 = new TableCell();
            tRow.Cells.Add(tCell0);
            tCell0.Text = "Formatted Address = ";
            tCell0.Text += myGeocodeResponse.Results[i].Address.FormattedAddress;
            tCell0.Text += "
Address Line= ";
            tCell0.Text += myGeocodeResponse.Results[i].Address.AddressLine;
            tCell0.Text += "
Locality = ";
            tCell0.Text += myGeocodeResponse.Results[i].Address.Locality;
            tCell0.Text += "
Admin District = ";
            tCell0.Text += myGeocodeResponse.Results[i].Address.AdminDistrict;
            tCell0.Text += "
Postal Code = ";
            tCell0.Text += myGeocodeResponse.Results[i].Address.PostalCode;
            tCell0.Text += "
Country = ";
            tCell0.Text += myGeocodeResponse.Results[i].Address.CountryRegion;
            tCell0.Text += "
District = ";
            tCell0.Text += myGeocodeResponse.Results[i].Address.District;
            tCell0.Text += "
Postal Town = ";
            tCell0.Text += myGeocodeResponse.Results[i].Address.PostalTown;
            tCell0.Text += "
Confidence = ";
            tCell0.Text += myGeocodeResponse.Results[i].Confidence;
            tCell0.Text += "
Display Name = ";
            tCell0.Text += myGeocodeResponse.Results[i].DisplayName;
            tCell0.Text += "
Entity Type = ";
            tCell0.Text += myGeocodeResponse.Results[i].EntityType;
            tCell0.Text += "
Altitude= ";
            tCell0.Text += myGeocodeResponse.Results[i].Locations[i].Altitude;
            /* If there are multiple response types, the latitude, longitude and
             * Calculation methods will have more than one result in their array. This
             * is where you'll want to differentiate them to use either interpolated,
             * rooftop or parcel centroids depending on how accurate you want them. */
            for (int j = 0; j < myGeocodeResponse.Results[i].Locations.Length; j++)
            {
                tCell0.Text += "
Latitude = ";
                tCell0.Text += myGeocodeResponse.Results[i].Locations[j].Latitude;
                tCell0.Text += "; Longitude = ";
                tCell0.Text += myGeocodeResponse.Results[i].Locations[j].Longitude;
                tCell0.Text += "; (";
                tCell0.Text += myGeocodeResponse.Results[i].Locations[j].CalculationMethod;
                tCell0.Text += ")";
            }
        }
    }
}

The end results is a nice little parsing of some address information returned from VEWS:

image

There isn't any difference in the geocoding results you get back between VEWS and the Virtual Earth AJAX control. Both now have rooftop and parcel centroid geocoding points totaling 85 million in the United States. Now, if I can just figure out what's up with the space between my text box and my table....

CP



Click here to view the article.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply


LinkBacks (?)
LinkBack to this Thread: http://www.mapforums.com/geocoding-virtual-earth-web-service-8716.html

Posted By For Type Date
The Magazine for MapPoint - MP2K Magazine This thread Refback 09-30-2008 01:44 PM

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads

Thread Thread Starter Forum Replies Last Post
Getting a Map with the Virtual Earth Web Service VirtualEarth MSDN Blog Virtual Earth Blogs 0 09-26-2008 07:22 PM
Lancement de Virtual Earth Web Service et du Virtual Earth Map Control 6.2 Virtual Earth Europe Virtual Earth Blogs 0 09-25-2008 04:22 AM
Announcing The Virtual Earth Web Service and Virtual Earth Map Control 6.2 VirtualEarth MSDN Blog Virtual Earth Blogs 0 09-25-2008 01:21 AM
Geocoding with Virtual Earth VirtualEarth MSDN Blog Virtual Earth Blogs 0 04-08-2008 03:10 AM
Virtual Earth Geocoding Accuracy Put to the Test VE For Government Virtual Earth Blogs 1 01-15-2008 01:59 PM


All times are GMT -5. The time now is 09:35 PM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0
MP2K Magazine
Visitor Map


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55