Abfragen eines Standortes bei Google-Maps

In vielen Fällen ist es notwendig, den genauen Standort zu einer Adresse zu ermitteln.
Meist wird dies dazu benötigt, um eine Position auf einer Karte (z.B. Google-Map) darzustellen.
Das Client-Framework von Google nimmt von Hause aus die Längen- und Breitengrade entgegen, um Positionen auf der Karte darzustellen.
Aber wie erhalte ich diese Informationen?
Es existiert eine Art Webservice, über den man eine Anfrage an Google stellen kann. Der Anfragetext kann dann genauso aussehen, als ob es in die entsprechenden Textbox auf der Google-Maps-Seite eingegen wird. Zum Beispiel: Berlin, Brandenburger Tor usw.
Die Adresse dafür ist:

http://maps.google.com/maps/geo

Es werden noch zwei Parameter benötigt, um ein Abfrageergebnis zu erhalten.

Das ist einmal ein Key, den man sich bei Google-Maps besorgen muß, um sich zu authentifizieren. Website
Dieser Schlüssel gilt jeweils für ein Verzeichnis, so das man sich mehrere Schlüssel generieren lassen muß, wenn eine Visualisierung in mehreren Verzeichnissen geplant ist.
Ich habe dafür einfach ein Dictionary eingerichtet, das je nach Verzeichnis den entsprechenden Schlüssel übergibt.

Der andere Parameter ist die Anfragetext, der natürlich url-kodiert sein muß.

So könnte eine Abfrage-Url aussehen:

string url =
  string.Format(
    "http://maps.google.com/maps/geo?q={0}&output=xml&key={1}", HttpUtility.UrlEncode(address),key);

Zur Abfrage kann dann einfach das Webrequest-Object benutzt werden:

WebRequest webRequest =
  WebRequest.Create(url);
WebResponse webResponse =
  webRequest.GetResponse();
sResponse = new StreamReader(
  webResponse.GetResponseStream(), Encoding.Default).ReadToEnd();
webResponse.Close();
XmlDocument xmlDocument =
  new XmlDocument();

xmlDocument.LoadXml(sResponse);

Zurückgegeben wird ein XML-Dokument, das einen Returncode und den Standort enthält, falls einer gefunden wurde.
Der Code 200 heißt wie immer, das die Anfrage erfolgreich war.

Die Returncodes:

601 => "G_GEO_MISSING_ADDRESS: No address specified",

602 => "G_GEO_UNKNOWN_ADDRESS: The address entered cannot be identified",

603 => "G_GEO_UNAVAILABLE_ADDRESS: There is no geocoding information for this address",

610 => "G_GEO_BAD_KEY: The API KEY supplied is invalid",

620 => "G_GEO_TOO_MANY_QUERIES: This client has exceeded the query count.  Try later.",

500 => "G_GEO_SERVER_ERROR: Internal Geocode Service Error, Try later.",

Mit folgendem C#-Code können nun die gesuchten Koordianten aus dem XML-String ermittelt werden:

XmlNodeList ndCoord = xmlDocument.GetElementsByTagName("coordinates");
XmlNodeList ndCode = xmlDocument.GetElementsByTagName("code");
if ( 
  ndCode != null &&
 
 
ndCode.Count == 1 && 
 
  ndCode[0].InnerText.Equals("200") && 
  ndCoord != null &&  ndCoord.Count > 0    
    ) 
  {      
   
NumberFormatInfo ni = new NumberFormatInfo();
      
   
ni.NumberDecimalSeparator = ".";
      
   
ga = new GoogleAddress(address);
      
   
string sCoord = ndCoord[0].InnerText;
      
   
string[] arrCoord = sCoord.Split(',');
      
   
ga.Longitude = float.Parse(arrCoord[0], ni);
      
   
ga.Latitude = float.Parse(arrCoord[1],ni);

Jetzt kann das Ergebnis zum Beispiel zur Darstellung bei Google-Maps genutzt werden. Aber dazu ein anderes Mal... 

Currently rated 4.0 by 1 people

  • Currently 4/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Related posts

Comments

November 20. 2007 23:40

Peter

Kann man auch mal den gesamten Quellcode bekommen?

Peter

March 27. 2010 16:46

no credit check payday loan

I admire the valuable information you offer in your articles. I will bookmark your blog and have my children check up here often. I am quite sure they will learn lots of new stuff here than anybody else!

no credit check payday loan

March 28. 2010 08:28

cash advance usa

Valuable information and excellent design you got here! I would like to thank you for sharing your thoughts and time into the stuff you post!! Thumbs up!

cash advance usa

March 29. 2010 14:20

fast cash loan

I usually dont post in Blogs but your blog forced me to, amazing work.. beautiful

fast cash loan

April 4. 2010 01:17

genital warts cure

Excellent read, I just passed this onto a colleague who was doing a little research on that. And he actually bought me lunch because I found it for him smile So let me rephrase that: Thanks for lunch! Big thanks for the useful info i found on Abfragen eines Standortes bei Google-Maps.

genital warts cure

April 14. 2010 21:22

getting rid of cellulite

You made some good points there. I did a search on the topic and found most people will agree with your blog. Big thanks for the useful info i found on Abfragen eines Standortes bei Google-Maps.

getting rid of cellulite

April 28. 2010 23:47

poor credit personal loans

Thank you for another great article. Where else could anyone get that kind of information in such a perfect way of writing? I have a presentation next week, and I am on the look for such information.

poor credit personal loans

May 5. 2010 04:26

personal loan with bad credit

Aw, this was a really quality post. In theory I'd like to write like this too - taking time and real effort to make a good article... but what can I say... I procrastinate alot and never seem to get something done.

personal loan with bad credit

May 15. 2010 21:52

Pencey

I need to extend security in BE just a little. I need to make a user registration form and disable comments for non-registered users. Has anyone done this? What difficulties might arise?

Pencey

May 19. 2010 08:45

high risk personal loans

Wow and wow! Thank you! I always wished to write in my site something like that.

high risk personal loans

June 19. 2010 00:24

vigrx

Hey man, was just browsing through the internet looking for some information and came across your blog. I am impressed by the information that you have on this blog. It shows how well you understand this subject. Bookmarked this page, will come back for more. You, my friend, ROCK!!!

vigrx

July 2. 2010 19:34

La Martina

Sie haben ein sehr schönes BlogSmile

La Martina

July 19. 2010 17:21

designer jeans

I just found your blog via Ask Jeeves, a really good read, thanks.

designer jeans

July 20. 2010 10:48

dior handbags

The post what thread said is quiet well. With beautiful and easy cleaning, hardness, high strength, stable performance, etc. And widely used in interior decoration engineering.By the way,Iknow some websites about fashion The contents are wonderful. <a href="www.usa-basketball-shoes.com/...dier-IV.html" >Nike air Zoom Lebron soldier IV</a>

dior handbags

Add comment


(Will show your Gravatar icon)  

  Country flag





Live preview

September 9. 2010 22:27