AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren

Google ReverseGeocoding

Ein Thema von MapMan · begonnen am 8. Mär 2011 · letzter Beitrag vom 9. Mär 2011
Antwort Antwort
MapMan

Registriert seit: 7. Apr 2008
11 Beiträge
 
Delphi 7 Architect
 
#1

Google ReverseGeocoding

  Alt 8. Mär 2011, 23:33
Beim Reverse Geocoding wird aus geografischen Koordinaten (Längengrad, Breitengrad) eine Adresse ermittelt.

Das geht recht einfach mit dem Google ReverseGeocoding-API http://code.google.com/intl/de-DE/ap...verseGeocoding

Eine kleine Klasse kapselt die Kernmethode ReverseGeocode. Zur Kommunikation mit dem API wird ein Indy HTTP-Client verwendet.

Delphi-Quellcode:
const
  Hdr='"formatted_address": "';
  URL='http://maps.google.com/maps/api/geocode/json?latlng=%3.8f,%3.8f&sensor=true';

function TGoogleReverseGeocode.ReverseGeocode(Latitude, Longitude: double):string;
begin
  s:=UTF8Decode(IdHTTP.Get(format(URL,[Latitude, Longitude],FormatSettings)));
  Idx:=pos(Hdr,s);
  if Idx<>0 then
    begin
      Idx:=Idx+22;
      while s[Idx]<>'"do
        begin
          Result:=Result+s[Idx];
          inc(Idx);
        end;
    end
  else
    Result:='?';
end;
Google liefert eine Menge Daten zurück, interessant ist aber primär die formatierte Adresse.
Wer möchte, kann sich aus dem Abfrageergebis sein individuelles Format erzeugen.

Code:
,
{
  "status": "OK",
  "results": [ {
    "types": [ "street_address" ],
    "formatted_address": "Schloßstraße 27, 56068 Koblenz, Germany",
    "address_components": [ {
      "long_name": "27",
      "short_name": "27",
      "types": [ "street_number" ]
    }, {
      "long_name": "Schloßstraße",
      "short_name": "Schloßstraße",
      "types": [ "route" ]
    }, {
      "long_name": "Koblenz",
      "short_name": "Koblenz",
      "types": [ "sublocality", "political" ]
    }, {
      "long_name": "Koblenz",
      "short_name": "Koblenz",
      "types": [ "locality", "political" ]
    }, {
      "long_name": "Koblenz",
      "short_name": "KO",
      "types": [ "administrative_area_level_2", "political" ]
    }, {
      "long_name": "Rheinland-Pfalz",
      "short_name": "RP",
      "types": [ "administrative_area_level_1", "political" ]
    }, {
      "long_name": "Germany",
      "short_name": "DE",
      "types": [ "country", "political" ]
    }, {
      "long_name": "56068",
      "short_name": "56068",
      "types": [ "postal_code" ]
    } ],
    "geometry": {
      "location": {
        "lat": 50.3564800,
        "lng": 7.5956700
      },
      "location_type": "ROOFTOP",
      "viewport": {
        "southwest": {
          "lat": 50.3533324,
          "lng": 7.5925224
        },
        "northeast": {
          "lat": 50.3596276,
          "lng": 7.5988176
        }
      }
    }
  }, {
    "types": [ "postal_code" ],
    "formatted_address": "56068 Koblenz, Germany",
    "address_components": [ {
      "long_name": "56068",
      "short_name": "56068",
      "types": [ "postal_code" ]
    }, {
      "long_name": "Koblenz",
      "short_name": "Koblenz",
      "types": [ "locality", "political" ]
    }, {
      "long_name": "Koblenz",
      "short_name": "KO",
      "types": [ "administrative_area_level_2", "political" ]
    }, {
      "long_name": "Rheinland-Pfalz",
      "short_name": "RP",
      "types": [ "administrative_area_level_1", "political" ]
    }, {
      "long_name": "Germany",
      "short_name": "DE",
      "types": [ "country", "political" ]
    } ],
    "geometry": {
      "location": {
        "lat": 50.3511528,
        "lng": 7.5951959
      },
      "location_type": "APPROXIMATE",
      "viewport": {
        "southwest": {
          "lat": 50.3285887,
          "lng": 7.5818021
        },
        "northeast": {
          "lat": 50.3659432,
          "lng": 7.6090369
        }
      },
      "bounds": {
        "southwest": {
          "lat": 50.3285887,
          "lng": 7.5818021
        },
        "northeast": {
          "lat": 50.3659432,
          "lng": 7.6090369
        }
      }
    }
  }, {
    "types": [ "administrative_area_level_2", "political" ],
    "formatted_address": "Koblenz, Germany",
    "address_components": [ {
      "long_name": "Koblenz",
      "short_name": "KO",
      "types": [ "administrative_area_level_2", "political" ]
    }, {
      "long_name": "Rheinland-Pfalz",
      "short_name": "RP",
      "types": [ "administrative_area_level_1", "political" ]
    }, {
      "long_name": "Germany",
      "short_name": "DE",
      "types": [ "country", "political" ]
    } ],
    "geometry": {
      "location": {
        "lat": 50.3386144,
        "lng": 7.5885240
      },
      "location_type": "APPROXIMATE",
      "viewport": {
        "southwest": {
          "lat": 50.2829080,
          "lng": 7.4826460
        },
        "northeast": {
          "lat": 50.4096970,
          "lng": 7.6963740
        }
      },
      "bounds": {
        "southwest": {
          "lat": 50.2829080,
          "lng": 7.4826460
        },
        "northeast": {
          "lat": 50.4096970,
          "lng": 7.6963740
        }
      }
    }
  }, {
    "types": [ "locality", "political" ],
    "formatted_address": "Koblenz, Germany",
    "address_components": [ {
      "long_name": "Koblenz",
      "short_name": "Koblenz",
      "types": [ "locality", "political" ]
    }, {
      "long_name": "Koblenz",
      "short_name": "KO",
      "types": [ "administrative_area_level_2", "political" ]
    }, {
      "long_name": "Rheinland-Pfalz",
      "short_name": "RP",
      "types": [ "administrative_area_level_1", "political" ]
    }, {
      "long_name": "Germany",
      "short_name": "DE",
      "types": [ "country", "political" ]
    } ],
    "geometry": {
      "location": {
        "lat": 50.3566962,
        "lng": 7.5996166
      },
      "location_type": "APPROXIMATE",
      "viewport": {
        "southwest": {
          "lat": 50.2829080,
          "lng": 7.4826460
        },
        "northeast": {
          "lat": 50.4096970,
          "lng": 7.6963740
        }
      },
      "bounds": {
        "southwest": {
          "lat": 50.2829080,
          "lng": 7.4826460
        },
        "northeast": {
          "lat": 50.4096970,
          "lng": 7.6963740
        }
      }
    }
  }, {
    "types": [ "administrative_area_level_1", "political" ],
    "formatted_address": "Rhineland-Palatinate, Germany",
    "address_components": [ {
      "long_name": "Rheinland-Pfalz",
      "short_name": "RP",
      "types": [ "administrative_area_level_1", "political" ]
    }, {
      "long_name": "Germany",
      "short_name": "DE",
      "types": [ "country", "political" ]
    } ],
    "geometry": {
      "location": {
        "lat": 50.1183460,
        "lng": 7.3089527
      },
      "location_type": "APPROXIMATE",
      "viewport": {
        "southwest": {
          "lat": 48.9666617,
          "lng": 6.1123660
        },
        "northeast": {
          "lat": 50.9423250,
          "lng": 8.5081190
        }
      },
      "bounds": {
        "southwest": {
          "lat": 48.9666617,
          "lng": 6.1123660
        },
        "northeast": {
          "lat": 50.9423250,
          "lng": 8.5081190
        }
      }
    }
  }, {
    "types": [ "country", "political" ],
    "formatted_address": "Germany",
    "address_components": [ {
      "long_name": "Germany",
      "short_name": "DE",
      "types": [ "country", "political" ]
    } ],
    "geometry": {
      "location": {
        "lat": 51.1656910,
        "lng": 10.4515260
      },
      "location_type": "APPROXIMATE",
      "viewport": {
        "southwest": {
          "lat": 47.2701270,
          "lng": 5.8662579
        },
        "northeast": {
          "lat": 55.0815000,
          "lng": 15.0418321
        }
      },
      "bounds": {
        "southwest": {
          "lat": 47.2701270,
          "lng": 5.8662579
        },
        "northeast": {
          "lat": 55.0815000,
          "lng": 15.0418321
        }
      }
    }
  } ]
}
Im Anhang die Demo-App
Angehängte Dateien
Dateityp: zip GoogleReverseGeocoding.zip (254,3 KB, 79x aufgerufen)

Geändert von MapMan ( 9. Mär 2011 um 13:31 Uhr)
  Mit Zitat antworten Zitat
Thom

Registriert seit: 19. Mai 2006
570 Beiträge
 
Delphi XE3 Professional
 
#2

AW: Google ReverseGeocoding

  Alt 9. Mär 2011, 09:51
Hallo MapMan,

guter Beitrag!

Vier kleine Anmerkungen:
  1. Das Hauptfenster der Demo ist zu weit rechts und deshalb auf kleineren Bildschirmen (wie z.B. meinem Netbook mit 1024 Pixeln) nicht mehr sichbar (auch mein Windows XP bekommt das nicht hin, obwohl TForm1.Position:=poDefaultPosOnly eingestellt ist).
    Logischerweise funktioniert das in der Delphi-IDE auch nicht, wenn nicht mit eingebettetem Designer gearbeitet wird. Klar: Eine manuelle Eingabe von TForm1.Left tut's auch - sorgt aber erst einmal für einen kleinen Moment der Verwirrung...
  2. Du arbeitest bestimmt mit einem englischsprachigen Gebietsschema!? Die Zeile
    Delphi-Quellcode:
    s:=UTF8Decode(IdHTTP.Get(format(URL,[Latitude, Longitude])));
    //...
    formatiert nämlich normalerweise deutschsprachig und damit werden die Fließkommazahlen mit einem Komma, statt dem von Goggle erwartetem Punkt versehen:
    Code:
    http://maps.google.com/maps/api/geocode/json?latlng=50,35662,7,59566f&sensor=true
    Das mag Google nicht und liefert als Antwort
    Code:
    '{'#$A' "status": "ZERO_RESULTS",'#$A' "results": [ ]'#$A'}'#$A
    Da in der Demo keine Fehler ausgewertet werden, bekommt der Anwender auch nichts zu sehen - aus seiner Sicht passiert einfach nichts.
  3. Nach dem Icon Deiner Exe-Datei zu urteilen, arbeitest Du noch mit Delphi 7 und damit ohne Unicode. Mein Delphi XE produziert nämlich einige Warnungen:
    Code:
    [DCC Warnung] GoogleReverseGeocoding.pas(67): W1000 Symbol 'UTF8Decode' ist veraltet: 'Use UTF8ToWideString or UTF8ToString'
    [DCC Warnung] GoogleReverseGeocoding.pas(67): W1058 Implizite String-Umwandlung mit potenziellem Datenverlust von 'string' zu 'RawByteString'
  4. Dann hast Du auch noch zwei ungenutzte Variablen:
    Code:
    [DCC Hinweis] GoogleReverseGeocoding.pas(82): H2164 Variable 'Longitude' wurde deklariert, aber in 'TForm1.btnReverseGeocodeClick' nicht verwendet
    [DCC Hinweis] GoogleReverseGeocoding.pas(82): H2164 Variable 'Latitude' wurde deklariert, aber in 'TForm1.btnReverseGeocodeClick' nicht verwendet
Noch einige Tips für den Benutzer:
  • Man könnte fertige JSON (JavaScript Object Notation)-Parser zur Auswertung der Server-Antwort benutzen (gibt's auch für Delphi).
  • Oder man erzwingt in der Abfrage das XML-Format
    Code:
    http://maps.google.com/maps/api/geocode/xml?latlng=%3.8f,%3.8f&sensor=true
    und nutzt zur Auswertung die Komponente TXMLDocument oder irgend einen anderen XML-Parser.
  • Als Parameter sensor kann auch ohne Probleme der Wert false übergeben werden - schließlich besitzen wohl die wenigsten Computer einen GPS-Sensor. Außerdem ist das für die Abfrage völlig irrelevant und befriedigt nur die Neugierde von Google...
Thomas Nitzschke
Google Maps mit Delphi

Geändert von Thom ( 9. Mär 2011 um 09:56 Uhr)
  Mit Zitat antworten Zitat
MapMan

Registriert seit: 7. Apr 2008
11 Beiträge
 
Delphi 7 Architect
 
#3

AW: Google ReverseGeocoding

  Alt 9. Mär 2011, 13:27
Hallo Thom,

Zitat:
1.Das Hauptfenster der Demo ist zu weit rechts und deshalb auf kleineren Bildschirmen (wie z.B. meinem Netbook mit 1024 Pixeln) nicht mehr sichbar (auch mein Windows XP bekommt das nicht hin, obwohl TForm1.Position:=poDefaultPosOnly eingestellt ist).
Problem beseitigt...

Zitat:
2.Du arbeitest bestimmt mit einem englischsprachigen Gebietsschema!?
So war's.
Sollte durch
FormatSettings.DecimalSeparator:='.'; jetzt überall laufen.

Zitat:
3.Nach dem Icon Deiner Exe-Datei zu urteilen, arbeitest Du noch mit Delphi 7
Genau...
Das gute, alte Arbeitspferd an das ich auch gekettet bin.
Ich habe einfach nicht die Zeit meine Produktivprojekte zu migrieren.
Aber da schein ich nicht allein zu sein.

Zitat:
Noch einige Tips für den Benutzer:
•Man könnte fertige JSON (JavaScript Object Notation)-Parser zur Auswertung der Server-Antwort benutzen (gibt's auch für Delphi).
•Oder man erzwingt in der Abfrage das XML-Format
Klar...
Macht Sinn, wenn man mehr braucht als die formatierte Adresse.
Aber dafür ist das mit Kanonen auf Spatzen geschossen.

Und XML bläst die Google-Response nochmal um Faktor 2 auf.


Übrigens zieht Google wohl die Notbremse bei mehr als 2500 ReverseGeocodings pro Tag.
Schreiben die, muss ich aber mal testen...
  Mit Zitat antworten Zitat
Themen-Optionen Thema durchsuchen
Thema durchsuchen:

Erweiterte Suche
Ansicht

Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 00:54 Uhr.
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz