views:

2065

answers:

2

Google has this fine new Google Maps API v3 which is a "Javascript API".

Is there any possibility to invoke this API from a Windows Forms application written in Visual C# .net 3.5?

EDIT:
The goal is to convert addresses to lat/long format using the Google Maps Geocoder.

EDIT 2:
I'd like to use v3 of the API as it doesn't require an API key anymore. API keys should be bound to a webserver which can't be the case for a Windows Forms app.

+1  A: 

You could host the WebBrowser control on your form and use that to interact with the Google Maps API.

But some more information about what you are really trying to do would be nice.

mlarsen
You're right, I've edited the question. I also thought about using the WebBrowser control. But I don't see how I would be able to retrieve the values from it other than saving the resulting web page and parsing it afterwards or so...
Marc
+5  A: 

Edit: Looks like the api key is no longer required.

You can use the REST APIs and parse the response (XML/JSON/CSV).

http://maps.google.com/maps/geo?q=State+St,+Troy,+NY&output=csv&oe=utf8&sensor=false

Would output:

200,6,42.730070,-73.690570

Which is:

  • 200 - G_GEO_SUCCESS
  • 6 - Street level accuracy
  • 42.730070 - Latitude
  • -73.690570 - Longitude

If you are not familiar with the System.Net APIs, they would go something like this:

const string GeoCodeUrlFormat = "http://maps.google.com/maps/geo?q={0}&output=csv&oe=utf8&sensor=false";

string location = "State St, Troy, NY";
string url = String.Format(GeoCodeUrlFormat, HttpUtility.UrlEncode(location));

HttpRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
HttpResponse response = (HttpResponse)request.GetResponse(); // Can throw an exception
Stream responseStream = response.GetResponseStream();

using (StreamReader reader = new StreamReader(responseStream)
{
    string firstLine = reader.ReadLine();

    string[] lineParts = firstLine.Split();

    GeolocationResult result = GoogleMapper.MapGeolocationResult(lineParts);

    // TADA
}

Obviously there's no error handling, it doesn't support multiple return values and I haven't actually implemented MapGeolocationResult, but it should give you a start.

Richard Szalay
Great response thanks. However part of the problem is that I'd like to use V3 (not v2) of the API as it doesn't require the API key anymore.The app will be rolled-out to various customers and the API key should be linked to a webserver but this won't be the case for a Windows Forms app...
Marc
Looks like they've removed that requirement on the services as I just removed the key argument and it returned the correct data.
Richard Szalay
Yes you're right. So I'm going to use that then. Hope it remains like that...
Marc