-(void) viewdidload
{
[Super Viewdidload];
Creating an Address resolver
Self.geocoder = [[Clgeocoder alloc] init];
}
-(Ibaction) encodetapped: (ID) sender
{
Gets the address string entered by the user
nsstring* addr = Self.addrField.text;
if (addr! = Nil && addr.length > 0)
{
[Self.geocoder geocodeaddressstring:addr
Completionhandler: ^ (Nsarray *placemarks, Nserror *error)
{
If the number of collection elements of the parsed result is greater than 1, the longitude and latitude information is obtained.
if (Placemarks.count > 0)
{
Only the first parse result is processed, and the list is used in the actual project to allow the user to select
clplacemark* placemark = placemarks[0];
cllocation* location = placemark.location;
Self.resultView.text = [NSString stringWithFormat:
@ "Longitude of%@:%g, Latitude:%g", addr,
Location.coordinate.longitude,
Location.coordinate.latitude];
}
Did not get the result of parsing.
Else
{
Use Uialertview to alert users
[[[Uialertview alloc] initwithtitle:@ "Reminder"
message:@ "The address you entered cannot be resolved" Delegate:nil
cancelbuttontitle:@ "OK" otherbuttontitles:nil]
Show];
}
}];
}
}
-(Ibaction) reversetapped: (ID) sender
{
nsstring* longitudestr = Self.longitudeField.text;
nsstring* latitudestr = Self.latitudeField.text;
if (longitudestr! = nil && longitudestr.length > 0
&& latitudestr! = Nil && latitudestr.length > 0)
{
Encapsulates the latitude and longitude entered by the user into a Cllocation object
cllocation* location = [[Cllocation alloc]
Initwithlatitude:[latitudestr Floatvalue]
Longitude:[longitudestr Floatvalue]];
[Self.geocoder reversegeocodelocation:location Completionhandler:
^ (Nsarray *placemarks, Nserror *error)
{
If the number of collection elements of the parsed result is greater than 1, the longitude and latitude information is obtained.
if (Placemarks.count > 0)
{
Only the first parse result is processed, and the actual project can use the list to let the user choose
clplacemark* placemark = placemarks[0];
Get Detailed address information
nsarray* Addrarray = [placemark.addressdictionary
objectforkey:@ "Formattedaddresslines"];
Stitching the detailed address into a string
nsmutablestring* addr = [[Nsmutablestring alloc] init];
for (int i = 0; i < addrarray.count; i + +)
{
[addr Appendstring:addrarray[i]];
}
Self.resultView.text = [NSString stringWithFormat:
@ "Longitude:%g, Latitude:%g's address is:%@",
Location.coordinate.longitude,
Location.coordinate.latitude, addr];
}
Did not get the result of parsing.
Else
{
Use Uialertview to alert users
[[[Uialertview alloc] initwithtitle:@ "Reminder"
message:@ "The address you entered cannot be resolved" Delegate:nil
cancelbuttontitle:@ "OK" otherbuttontitles:nil]
Show];
}
}];
}
}
iOS Address encoding parsing