使用ALJsonDoc,你可以在這裏下載:https://sourceforge.net/projects/alcinoe/
或使用svn得多好: (SVN)https://svn.code.sf.net/p/alcinoe/code/
你可以做到這一點很容易像這樣:
function _FindLatLongU(const aQuery: String;
var aID_Gmap: String;
var aLatitude,
aLongitude: Double;
const aHighAccuracy: boolean = True): boolean;
var aHttp: Twin_HttpTaskU;
aHTTPResponse: IHTTPResponse;
aBytes: Tbytes;
AResponseContentStream: TMemoryStream;
ADecodedResponseContentStream: TStream;
aFreeDecodedResponseContentStream: boolean;
aJsonDoc: TalJsonDocumentU;
S1: String;
begin
try
//init var
Result := False;
aID_Gmap := '';
aLatitude := 999;
aLongitude := 999;
//create local object
aHttp := Twin_HttpTaskU.Create;
AResponseContentStream := TMemoryStream.Create;
ADecodedResponseContentStream := nil;
aFreeDecodedResponseContentStream := False;
aJsonDoc := TalJsonDocumentU.Create(true);
try
//init aJsonDoc
aJsonDoc.Options := [doNodeAutoCreate];
//do the http request
aHTTPResponse := aHttp.HttpClient.get('http://maps.googleapis.com/maps/api/geocode/json?'+ // json indicates output as JSON
'address=' + TnetEncoding.URL.Encode(aQuery) + '&'+ // The address that you want to geocode.
'sensor=false&'+ // Indicates whether or not the geocoding request comes from a device with a location sensor. This value must be either true or false.
'language=en',
AResponseContentStream); // AResponseContent
//normally this never happen because it's raise an exception in previous step
if aHTTPResponse.StatusCode <> 200 then exit(False);
//decode the result if necessary
{$if defined(_USE_ZLIB)}
if ALSameTextU(aHTTPResponse.ContentEncoding, 'gzip') then begin
ADecodedResponseContentStream := TDecompressionStream.Create(AResponseContentStream, 15 + 16); // 15 is the default mode.
aFreeDecodedResponseContentStream := True; // 16 is to enable gzip mode. http://www.zlib.net/manual.html#Advanced
end
else ADecodedResponseContentStream := AResponseContentStream;
{$ELSE}
ADecodedResponseContentStream := AResponseContentStream;
{$ENDIF}
//put the response in aJsonDoc
ADecodedResponseContentStream.Position := 0;
setlength(aBytes, ADecodedResponseContentStream.Size);
ADecodedResponseContentStream.ReadBuffer(pointer(aBytes)^, ADecodedResponseContentStream.Size);
S1 := Tencoding.UTF8.GetString(aBytes); //{
// "results" : [
// {
// "address_components" : [
// {
// "long_name" : "Toledo",
// "short_name" : "Toledo",
// "types" : [ "locality", "political" ]
// },
// {
// "long_name" : "Toledo",
// "short_name" : "Toledo",
// "types" : [ "administrative_area_level_4", "political" ]
// },
// {
// "long_name" : "Vega de Toledo",
// "short_name" : "Vega de Toledo",
// "types" : [ "administrative_area_level_3", "political" ]
// },
// {
// "long_name" : "Toledo",
// "short_name" : "TO",
// "types" : [ "administrative_area_level_2", "political" ]
// },
// {
// "long_name" : "Castile-La Mancha",
// "short_name" : "CM",
// "types" : [ "administrative_area_level_1", "political" ]
// },
// {
// "long_name" : "Spain",
// "short_name" : "ES",
// "types" : [ "country", "political" ]
// }
// ],
// "formatted_address" : "Toledo, Toledo, Spain",
// "geometry" : {
// "bounds" : {
// "northeast" : {
// "lat" : 39.88605099999999,
// "lng" : -3.9192423
// },
// "southwest" : {
// "lat" : 39.8383676,
// "lng" : -4.0629256
// }
// },
// "location" : {
// "lat" : 39.8628316,
// "lng" : -4.027323099999999
// },
// "location_type" : "APPROXIMATE",
// "viewport" : {
// "northeast" : {
// "lat" : 39.88605099999999,
// "lng" : -3.9192423
// },
// "southwest" : {
// "lat" : 39.8383676,
// "lng" : -4.0629256
// }
// }
// },
// "place_id" : "ChIJ8f21C60Lag0R_q11auhbf8Y",
// "types" : [ "locality", "political" ]
// }
// ],
// "status" : "OK"
//}
aJsonDoc.LoadFromJSONString(S1);
// "OK" indicates that no errors occurred; the address was successfully parsed and at least one geocode was returned.
if ALSametextU(aJsonDoc.ChildNodes['status'].Text, 'OK') then begin
// get only the first result (it's the most accurate)
if ((aHighAccuracy) and (aJsonDoc.ChildNodes['results'].ChildNodes.Count = 1)) or
((not aHighAccuracy) and (aJsonDoc.ChildNodes['results'].ChildNodes.Count > 0)) then begin
// extract ID of Google Maps, it looks like hash and must be saved as text.
// An example: EisxMjAwMyBNYWluIFN0cmVldCwgQnJpYXJ3b29kLCBOWSAxMTQzNSwgVVNB
aID_Gmap := aJSONDoc.ChildNodes['results'].ChildNodes[0].ChildNodes['place_id'].Text;
//"geometry" : {
// "location" : {
// "lat" : 37.4224553,
// "lng" : -122.0843062
// },
// "location_type" : "ROOFTOP",
// "viewport" : {
// "northeast" : {
// "lat" : 37.42380428029149,
// "lng" : -122.0829572197085
// },
// "southwest" : {
// "lat" : 37.42110631970849,
// "lng" : -122.0856551802915
// }
// }
//}
with aJSONDoc.ChildNodes['results'].ChildNodes[0].ChildNodes['geometry'] do begin
aLongitude := ALStrToFloatU(ChildNodes['location'].ChildNodes['lng'].Text, ALDefaultFormatSettingsU);
aLatitude := ALStrToFloatU(ChildNodes['location'].ChildNodes['lat'].Text, ALDefaultFormatSettingsU);
end;
//set result to true
result := true;
end;
end;
finally
AResponseContentStream.Free;
if aFreeDecodedResponseContentStream then aDecodedResponseContentStream.Free;
aJsonDoc.Free;
aHttp.Free;
end;
except
result := False;
end;
end;
你得到什麼異常?其餘的相關代碼在哪裏? –
我得到了'訪問違規地址',REST結果爲 [link](https://maps.googleapis.com/maps/api/geocode/json?latlng=-6.408107,107.468262&key=AIzaSyCUzlhl-ibUJsKCwWDwsZqjiW7EP_On30g&sensor=false) 我使用'TrestClient'和'Restresponse' –
訪問衝突在什麼地址等?這些數字確實意味着什麼。你是否已經開始調試以查看這些任務之一是否返回'nil'?那會造成這樣的例外。哪一行你會得到例外? –