Obtain an address Asynchronously

Source: Internet
Author: User
<title></title>

/Obtain Location Information Based on longitude and latitude

-(Nsstring*) Getaddressbycoordinate :(Double) Latitude longpolling :(Double) Longitud

{

Nslog(@"LoadEeeeee");

Nsstring* LD = [[Nsstring Alloc]Initwithformat:@ "% F", Latitude];

Nsstring* Lt = [[Nsstring Alloc]Initwithformat:@ "% F", Longitud];

Nsstring* LDT = [Nsstring Stringwithformat:@ "% @, % @", LD, lt];// [[LD stringbyappendingformat: @ ","] stringbyappendingformat: Lt];

Nsstring* Url1 =@ "Http://maps.google.com/maps/api/geocode/json? Latlng =";

Nsstring* Url2 =@ "& Sensor = true & language = ZH-CN";

Nsstring* URLRequest = [Nsstring Stringwithformat:@ "% @", Url1, LDT, url2];// [[Url1 stringbyappendingformat: LDT] stringbyappendingformat: url2];

Nslog(@ "URLRequest: % @", URLRequest );

Nsurlrequest* Therequest = [Nsurlrequest Requestwithurl:[Nsurl Urlwithstring: URLRequest]

Cachepolicy:Nsurlrequestuseprotocolcachepolicy

Timeoutinterval:60.0];

// Create the connection with the request

// And start loading the data

Nsurlconnection* THECONNECTION = [[Nsurlconnection Alloc]Initwithrequest: TherequestDelegate:Self];

If(THECONNECTION ){

// Create the nsmutabledata to hold the specified ed data.

// Receiveddata is an instance variable declared elsewhere.

Receiveddata= [[Nsmutabledata Data]Retain];

}Else{

// Inform the user that the connection failed.

Nslog(@ "Fail");

}

// Nsstring * jsonstring = [[nsstring alloc]

// Initwithbytes: [receiveddata bytes]

// Length: [receiveddata length]

// Encoding: nsasciistringencoding];

// Nslog (@ "jsonstring", jsonstring );

// Nsstring * jsonstring = [[nsstring alloc] initwithcontentsofurl: URL encoding: nsutf8stringencoding error: Nil];

// Nsdictionary * jsondic = [jsonstring jsonvalue];

// Nsarray * dict = [jsondic valueforkeypath: @ "results. formatted_address"];

//

// Nsstring * address = [dict objectatindex: 0];

Return @ "Address";

}

-(Void) Connection :(Nsurlconnection*) Connection didreceiveresponse :(Nsurlresponse*) Response

{

// This method is called when the server has determined that it

// Has enough information to create the nsurlresponse.

// It can be called multiple times, for example in the case of

// Redirect, so each time we reset the data.

// Receiveddata is an instance variable declared elsewhere.

[Receiveddata Setlength:0];

}

//ReceiveNsdataData

-(Void) Connection :(Nsurlconnection*) Connection didreceivedata :(Nsdata*) Data

{

// Append the new data to receiveddata.

// Receiveddata is an instance variable declared elsewhere.

[Receiveddata Appenddata: Data];

}

-(Void) Connection :(Nsurlconnection*) Connection

Didfailwitherror :(Nserror*) Error

{

// Release the connection, and the Data Object

[ConnectionRelease];

// Receiveddata is declared as a Method Instance elsewhere

[Receiveddata Release];

// Inform the user

Nslog(@ "Connection failed! Error-% @",

[ErrorLocalizeddescription],

[[ErrorUserinfo]Objectforkey:Nsurlerrorfailingurlstringerrorkey]);

}

//Received,Display result

-(Void) Connectiondidfinishloading :(Nsurlconnection*) Connection {

Nslog(@"Loading times: Dddddd");

Nsstring* Jsonstring = [[Nsstring Alloc]

Initwithbytes:[Receiveddata Bytes]

Length:[Receiveddata Length]

Encoding:Nsutf8stringencoding];

Nslog(@ "Jsonstring", Jsonstring );

//Http://maps.google.com/maps? Saddr ={% E6 % B7 % B1 % E5 % 9C % B3} & daddr = {% E5 % A4 % A9 % E6 % B4 % A5}Navigation

// Nsstring * jsonstring = [[nsstring alloc] initwithcontentsofurl: URL encoding: nsutf8stringencoding error: Nil];

Nsdictionary* Jsondic = [jsonstringJsonvalue];

//: Format not a string literal and no format arguments

// Nsdictionary * dict = [jsondic objectforkey: @ "Results"]; // valueforkeypath

Nsarray* Dict = [jsondicValueforkeypath:@ "Results. formatted_address"];

Nsstring* Address = [dictObjectatindex:0];

Curaddress= Address;

[Addannotation Settitle:@"Current location"];

[Addannotation Setsubtitle: Address];

[ConnectionRelease];

[Receiveddata Release];

}

Built-in Methods

// Mkreversegeocoder * geocoder = [[mkreversegeocoder alloc] initwithcoordinate: newlocation. Coordinate];

// Geocoder. Delegate = self;

// [[Nsuserdefaults standarduserdefaults] setobject: [nsarray arraywithobjects: @ "ZH-CN", nil] forkey: @ "applelanguages"];

// [Geocoder start];

-(Void) Reversegeocoder :(Mkreversegeocoder*) Geocoder didfailwitherror :(Nserror*) Error {

Nslog(@ "Mkreversegeocoder has failed .");

}

-(Void) Reversegeocoder :(Mkreversegeocoder*) Geocoder didfindplacemark :(Mkplacemark*) Placemark {

Nsstring* Address = [Nsstring Stringwithformat:@ "% @",

 

Placemark.Country,

 

Placemark.Administrativearea,

 

Placemark.Locality,

 

Placemark.Sublocality,

 

Placemark.Thoroughfare,

 

Placemark.Subthoroughfare];

Nslog(@"Details of longitude and latitude: % @", Address );

[Addannotation Settitle:@"Current location"];

[Addannotation Setsubtitle: Address];

Geocoder =Nil;

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.