views:

13

answers:

1

using the bing map API, can I make a server-side call and get lat/long for a given city?

I can do it on the client side using JavaScript, but I need something more robust in this case and want to use the server side.

A: 

Posting this so it's out there and easier to find the answer for everyone:

Use the Bing Maps SOAP Services API (http://msdn.microsoft.com/en-us/library/cc980922.aspx). Its a public web service that implements most of the Bing Maps API and serves static maps (sorry, no interactivity from the user). From the provided Microsoft SDK (slightly modified: After adding the service reference to your project, run this code:

private GeocodeResponse GeocodeAddress(string address) { GeocodeRequest geocodeRequest = new GeocodeRequest();

        // Set the credentials using a valid Bing Maps key
        geocodeRequest.Credentials = new GeocodeService.Credentials();
        geocodeRequest.Credentials.ApplicationId = BingMapsAPIKey;

        // Set the full address query
        geocodeRequest.Query = address;

        // Set the options to only return high confidence results 
        ConfidenceFilter[] filters = new ConfidenceFilter[1];
        filters[0] = new ConfidenceFilter();
        filters[0].MinimumConfidence = GeocodeService.Confidence.High;

        // Add the filters to the options
        GeocodeOptions geocodeOptions = new GeocodeOptions();
        geocodeOptions.Filters = filters;
        geocodeRequest.Options = geocodeOptions;

        // Make the geocode request
        GeocodeServiceClient geocodeService = new GeocodeServiceClient("BasicHttpBinding_IGeocodeService");
        GeocodeResponse geocodeResponse = geocodeService.Geocode(geocodeRequest);

        return geocodeResponse;
}

and you can check your return values with this code snippet:

result.Locations[0].Latitude.ToString();
result.Locations[0].Longitude.ToString();
ben f.