Registriert seit: 7. Apr 2008
11 Beiträge
Delphi 7 Architect
|
Google ReverseGeocoding
9. Mär 2011, 00: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
Geändert von MapMan ( 9. Mär 2011 um 14:31 Uhr)
|