Map: Clgeocoder address resolution and anti-address resolution

Source: Internet
Author: User

1. Importing the System framework

/**
* Interface effect 1 to navigate to the input address, and to indicate the latitude and longitude of the address
*/

2.viewcontroller.h

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
@interface Viewcontroller:uiviewcontroller

@property (Strong, nonatomic) Iboutlet Uisearchbar *searchbar;
@property (Strong, nonatomic) Iboutlet Mkmapview *mapview;
@property (Strong, nonatomic) Clgeocoder *geocoder;
@end

3.viewcontroller.m


@interface Viewcontroller () <UISearchBarDelegate>

@end

@implementation Viewcontroller

-(void) Viewdidload {
[Super Viewdidload];
Self.geocoder = [[Clgeocoder alloc]init];
self.mapView.zoomEnabled = YES;
self.mapView.rotateEnabled = NO;
self.mapView.scrollEnabled = YES;
Self.mapView.showsUserLocation = YES;
Self.mapView.mapType = Mkmaptypestandard;
Self.searchBar.text = @ "Tiananmen Square";
Self.searchBar.delegate = self;
}
Click to search
-(void) searchbarsearchbuttonclicked: (Uisearchbar *) Searchbar
{
Call the Searchbar method to search
[Self startsearch:searchbar];
}
Click Cancel
-(void) searchbarcancelbuttonclicked: (Uisearchbar *) Searchbar
{
Call the Searchbar method to search
[Self startsearch:searchbar];

}
Perform a search
-(void) Startsearch: (Uisearchbar *) Searchbar
{
Turn off the Searchbar virtual keyboard
[Self.searchbar Resignfirstresponder];
NSString *searchtext = Self.searchBar.text;
if (searchtext! = Nil && searchtext.length>0) {
[Self locateat:searchtext];
}
}
When you enter text
-(void) searchbartextdidbeginediting: (Uisearchbar *) Searchbar
{
Show Cancel button
Searchbar.showscancelbutton = YES;
Find the Cancel button by traversing and set the text of the Cancel button to search
For (id cc in [searchbar.subviews[0]subviews]) {
if ([Cc Iskindofclass:[uibutton class]]) {
UIButton *btn = (UIButton *) cc;
[Btn settitle:@ "search" forstate:uicontrolstatenormal];
}
}
}
Converts a string address to latitude and longitude, and performs a positioning
-(void) Locateat: (NSString *) add
{
[Self.geocoder geocodeaddressstring:add completionhandler:^ (nsarray<clplacemark *> * _Nullable Placemarks, Nserror * _nullable error) {
if ([placemarks count]>0 && error = = nil) {
NSLog (@ "Search to match%lu address data", (unsigned long) placemarks.count);
Process the first address
Clplacemark *placemark = [Placemarks objectatindex:0];
NSLog (@ "longitude =:%f", placemark.location.coordinate.longitude);
NSLog (@ "latitude =:%f", placemark.location.coordinate.latitude);
NSLog (@ "country =%@", placemark.country);
NSLog (@ "postcode =%@", placemark.postalcode);
NSLog (@ "position =%@", placemark.locality);
NSString *msg = [NSString stringwithformat:@ "longitude =%f, Latitude =%f", Placemark.location.coordinate.longitude, Placemark.location.coordinate.latitude];
[[[Uialertview alloc]initwithtitle:@ Information] message:msg delegate:self cancelbuttontitle:@ "OK" otherbuttontitles:nil] Show];
Set the extent of the map display
Mkcoordinatespan span;
The smaller the map display, the clearer the details
Span.latitudedelta = 0.0001;
Span.longitudedelta = 0.0001;
Mkcoordinateregion region = {Placemark.location.coordinate,span};
Set the location of the map center to the search
[Self.mapview setregion:region];
Creates an object that will be used as the map stroke.
Mkpointannotation *point = [[Mkpointannotation alloc]init];
Set the coordinates of a map anchor point
Point.coordinate = placemark.location.coordinate;
Set the title of the map anchor Point
Point.title = Placemark.name;
Set the subtitle of the map anchor Point
Point.subtitle = [NSString stringwithformat:@ "%@-%@-%@-%@", Placemark.country,placemark.administrativearea, Placemark.locality,placemark.sublocality];
Add a map anchor point to the map
[Self.mapview Addannotation:point];
Select the specified anchor point
[Self.mapview Selectannotation:point Animated:yes];
}
Else
{
NSLog (@ "No search results");
}
}];
}

@end

/**
* Interface Effect 2 Click the Parse button of interface 1 to jump to interface 2

* Implementation of input address, resolution to latitude and longitude

* Input latitude and longitude, show detailed address
*/

4. Interface 2. h

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
@interface Jiexiviewcontroller:uiviewcontroller
@property (Strong, nonatomic) Iboutlet Uitextfield *dizhijiexi;
@property (Strong, nonatomic) Iboutlet Uitextfield *jingdu;
@property (Strong, nonatomic) Iboutlet Uitextfield *weidu;
@property (Strong, nonatomic) Iboutlet Uitextview *showtv;
-(Ibaction) Jiexi: (ID) sender;
-(Ibaction) Fanjeixi: (ID) sender;
@property (Strong, nonatomic) clgeocoder* Geocoder;
@end
5. Interface 2 of the. m


@interface Jiexiviewcontroller ()

@end

@implementation Jiexiviewcontroller

-(void) Viewdidload {
[Super Viewdidload];
Creating an Address resolver
Self.geocoder = [[Clgeocoder alloc]init];
}

-(Ibaction) Jiexi: (ID) Sender {
Gets the address string entered by the user
NSString *inputadd = Self.dizhijiexi.text;
if (inputadd! = Nil && inputadd.length>0) {
[Self.geocoder geocodeaddressstring:inputadd completionhandler:^ (nsarray<clplacemark *> * _Nullable placemarks , Nserror * _nullable 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.showTV.text = [NSString stringWithFormat:
@ "Longitude of%@:%g, Latitude:%g", Inputadd,
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) Fanjeixi: (ID) Sender {

nsstring* longitudestr = Self.jingdu.text;
nsstring* latitudestr = Self.weidu.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.showTV.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];
}
}];
}
}
-(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Uievent *) event
{
[Self.view Endediting:yes];
}
@end

Map: Clgeocoder address resolution and anti-address resolution

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.