Map, location Cllocationmanager Clgeocoder Clplacemark

Source: Internet
Author: User
Tags reverse geocoding

Map, positioning one, basic knowledge point positioning: 1, Info.plist file Settings iOS8, use location to add two fields to the Info.plist file Nslocationalwaysusagedescription and NSLocationWhenInUseUsageDescription2 , import the Corelocation.framework framework and import the header file #import <corelocation/corelocation.h>3, determine if the location service opens if (![ Cllocationmanager locationservicesenabled]) {       NSLog (@ "Location not available");    }4, create location manager Cllocationmanager *_manager = [[Cllocationmanager alloc]init];5, determine if authorization is granted, if not authorized, send an authorization request if ( [Cllocationmanager authorizationstatus]==kclauthorizationstatusnotdetermined] {             [_manager requestwheninuseauthorization]; }6, Set Agent (cllocationmanagerdelegate) _manager.delegate=self;7, set precision _manager.desiredaccuracy=kcllocationaccuracybest;8 , set the positioning frequency, how many meters are positioned once _manager.distancefilter=10.0;9, start positioning [_manager startupdatinglocation];10, stop positioning [_manager Stopupdatinglocation];11, proxy Method-(void) Locationmanager: (Cllocationmanager *) Manager didfailwitherror: (Nserror *) error{   //Locating lossFail}-(void) Locationmanager: (Cllocationmanager *) Manager didupdatelocations: (Nsarray *) locations{    Cllocation *location = [locations Firstobject]; Cllocationcoordinate2d coordinate=location.coordinate;    NSLog (@ "Longitude:%f, Latitude:%f, Elevation:%f, heading:%f, travel speed:%f ", Coordinate.longitude,coordinate.latitude,location.altitude,location.course,location.speed);     Geocoding, according to the address of latitude and longitude, details, etc.     clgeocoder *geocode = [[clgeocoder alloc]init];     [GeoCode geocodeaddressstring:@ "Taishan"  completionhandler:^ (Nsarray *placemarks, Nserror *error) {         Clplacemark *place = [Placemarks firstobject];         cllocation *location = place.location;//position         clregion *region = place.region;//area         nsdictionary *dic = place.addressdictionary;//Detailed address information dictionary , including the following fields         Nsstring *name=place.name;//place name         nsstring *thoroughfare=place.thoroughfare;//Street         NSString *subthoroughfare=place.subthoroughfare; Street related information, such as house numbers         NSString *locality=place.locality; City         NSString *sublocality=place.sublocality; City-related information, such as iconic buildings         NSString *administrativearea=place.administrativearea ; State         Nsstring*subadministrativearea=place.subadministrativearea; Other administrative area information         NSString *postalcode=place.postalcode; Zip         nsstring *isocountrycode=place. Isocountrycode; Country code         NSString *country=place.country; National         NSString *inlandwater=place.inlandwater; Water, Lake         NSString *ocean=place.ocean; Ocean         Nsarray *areasofinterest=place.areasofinterest; Associated or interest-related landmarks    }]; Reverse geocoding, according to latitude and longitude to obtain the specific address information     cllocation*location=[[cllocation alloc]initwithlatitude:36.228 Longitude :117.042];    Clgeocoder *geocoder = [[clgeocoder alloc]init];    [Geocoder Reversegeocodelocation:location completionhandler:^ (Nsarray *placemarks, Nserror *error) {         Clplacemark*placemark=[placemarks firstobject];        NSLog (@ "More info:%@", Placemark.addressdictionary);   }]; Map: 12, import mapkit.framework, and import #import < Mapkit/mapkit.h> header file, implement Mkmapviewdelegate Protocol 13, create _mapview = [[Mkmapview alloc] initWithFrame: SELF.VIEW.BOUNDS];14, set proxy _mapview.delegate  = self;15, show user Location _mapview.showsuserlocation = YES;16, user Location Tracking _ Mapview.usertrackingmode = mkusertrackingmodefollow;//can be omitted, but needSet your map's zoom level 17, Proxy method: Get the user's current location-(void) Mapview: (Mkmapview *) Mapview didupdateuserlocation: (mkuserlocation*) userlocation{        Mkcoordinatespan Span=mkcoordinatespanmake (0.01, 0.01);         mkcoordinateregion Region=mkcoordinateregionmake ( UserLocation.location.coordinate, span);        [_mapview setregion: Region animated:true];//Set Map zoom level}18, map display range changed-(void) Mapview: (Mkmapview *) Mapview regiondidchangeanimated: (BOOL) animated{    NSLog (@ "Map display range has changed");} 19. Add pin mkpointannotation *point = [[mkpointannotation alloc]init];//Initialize point.title = @ "pin";// Title Point.subtitle = @ "I am a pin";//Sub-title Point.coordinate = Cllocationcoordinate2dmake (36.236867, 117.054895);//Latitude and longitude [_ MAPVIEW&NBSP;ADDANNOTATION:POINT];20, PIN is clicked-(void) Mapview: (Mkmapview *) Mapview Didselectannotationview: ( Mkannotationview *) view{    NSLog (@ "pin is clicked");} 21. Custom Pin View-(Mkannotationview *) Mapview: (Mkmapview*) Mapview viewforannotation: (id<mkannotation>) annotation{    if ([annotation isKindOfClass:[ Mkpointannotation class]) {//Determine if you have added pins         static nsstring * [email protected] "AnnotationKey1";        mkannotationview* annotationview=[_mapview dequeuereusableannotationviewwithidentifier:key1];//Get pin View         //If the cache pool does not exist, then new         if (!annotationview) {             Annotationview=[[mkannotationview alloc] Initwithannotation:annotation reuseidentifier:key1];            annotationview.canshowcallout=true;//allow interactive clicks                      annotationview.calloutoffset=cgpointmake (0, 1);// Define detail View Offset            UIButton *button = [uibutton buttonwithtype:uibuttontypecustom];           button.frame = CGRectMake (0, 0,;   ,         [Button setbackgroundimage:[uiimage imagenamed:@ icon_classify_ Cafe.png "] forstate:uicontrolstatenormal];             annotationview.leftcalloutaccessoryview=button;//definition details left view        }               //Modify PIN View         //Reset the PIN model for such a pin view (because it might be taken from the cache pool, where it was placed in the cache pool)          annotationview.annotation=annotation;        annotationView.image=[ uiimage imagenamed:@ "Icon_paopao_waterdrop_streetscape"];//set pin view picture                return annotationview;   }else {         return nil;   }}22, pin left or right view is clicked-(void) Mapview: (Mkmapview *) Mapview Annotationview: (Mkannotationview *) View calloutaccessorycontroltapped: (Uicontrol *) control{    if ([ Control Iskindofclass:[uibutton class]]) {        NSLog (@ "121212");    }}23, call system with map to navigate-(void) turnbyturn{   //According to "Taishan College" geocoding     Clgeocoder *_ Geocoder = [[Clgeocoder alloc] init];    [_geocoder geocodeaddressstring:@ "Taishan College"   completionhandler:^ (Nsarray *placemarks, Nserror *error) {        CLPlacemark * Clplacemark1=[placemarks firstobject];//Get the first landmark         Mkplacemark * mkplacemark1=[[mkplacemark alloc]initwithplacemark:clplacemark1];        //Note geocoding once onlyCan be positioned to a position, can not be located at the same time, placed in the first position to locate the completion callback function again positioning         [_geocoder geocodeaddressstring : @ "Shandong Province Taian Taishan Station"  completionhandler:^ (Nsarray *placemarks, Nserror *error) {             Clplacemark *clplacemark2=[placemarks firstobject];//get the first landmark              Mkplacemark *mkplacemark2=[[mkplacemark alloc] initwithplacemark:clplacemark2];            NSDictionary *[email protected]{mklaunchoptionsmaptypekey:@ (Mkmaptypestandard), Mklaunchoptionsdirectionsmodekey: mklaunchoptionsdirectionsmodedriving};//Set Navigation type              Mkmapitem *mapitem1=[[mkmapitem alloc]initwithplacemark:mkplacemark1];             Mkmapitem *mapitem2=[[mkmapitem alloc]initwithplacemark:mkplacemark2];             [mkmapitem openmapswithitems:@[mapitem1,mapitem2]  launchoptions:options];       }];    }];}  second, the specific code#import "ViewController.h"
1, first import header file #import <corelocation/corelocation.h>//Import header file 2, Info.plist file Settings, after iOS8, use location to add two fields Nslocationalwaysusagedescription and nslocationwheninuseusagedescription to the Info.plist file. @interface Viewcontroller () <cllocationmanagerdelegate>{
Cllocationmanager *manager;//Location Management} @end @implementation viewcontroller-(void) viewdidload {[Super Viewdidload]; 3, Determines whether access is allowed to locate if ([cllocationmanagerlocationservicesenabled]) {manager = [[Cllocationmanager alloc]init];// Initiation
NSLog (@ "Allow positioning");
Determine if the user has selected location access rights
if ([cllocationmanager authorizationstatus] = = 0) {
Re-eject the request if it is not already selected
[Manager Requestalwaysauthorization];
}
Set up Proxy
Manager.delegate = self;
Manager.distancefilter = 10;//How many meters to access one location
Manager.desiredaccuracy = accuracy of kcllocationaccuracybest;//positioning [manager startupdatinglocation];//start positioning [self get    Infobuyaddress]; }} 4, error message-(void) Locationmanager: (Cllocationmanager *) Manager didfailwitherror: (Nserror *) error{
NSLog (@ "%@", error);} 5. Print specific Content-(void) Locationmanager: (Cllocationmanager *) Manager didupdatelocations: (nsarray<cllocation *> *) locations{
NSLog (@ "--%zi---%@", locations.count,locations);
Cllocation *location = [locations Firstobject];
NSLog (@ "weidu:%f--jingdu:%f", location.coordinate.latitude,location.coordinate.longitude);
}6, geocoding-(void) getinfobuyaddress{
Clgeocoder *geo = [[Clgeocoder alloc]init];//Initialize encoding management
Geocoding, incoming addresses, getting specific information
[Geo geocodeaddressstring:@ "Zhoukou railway station" completionhandler:^ (Nsarray<clplacemark *> * _nullable placemarks, NSError * _ Nullable error) {
NSLog (@ "--Quantity:%zi information:%@", placemarks.count,placemarks);
Clplacemark *place = [Placemarks firstobject];
NSLog (@ "Latitude and Longitude:%f---%f", place.location.coordinate.latitude,place.location.coordinate.longitude);
NSLog (@ "Street:%@", Place.ocean);
}];
} 7, anti-geocoding: Get location information based on latitude and longitude-(void) getinfobuycoordinate{
Clgeocoder *geocoder = [[Clgeocoder alloc]init];
Cllocation *location = [[cllocation alloc]initwithlatitude:34.709895 longitude:113.509167];//defining position objects by latitude]
[Geocoder reversegeocodelocation:location completionhandler:^ (nsarray<clplacemark *> * _Nullable Placemarks, Nserror * _nullable error) {
NSLog (@ "Quantity:%zi Reverse:%@", placemarks.count,placemarks);

Clplacemark *place = [Placemarks firstobject];
NSLog (@ "Latitude and Longitude:%f,%f", place.location.coordinate.longitude,place.location.coordinate.latitude);
NSLog (@ "Street:%@", Place.ocean); }]; }

Map, location Cllocationmanager Clgeocoder Clplacemark

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.