We often do projects when the use of mobile phone to obtain positioning, and then pass data to the background, which is iOS8 according to the latitude and longitude of the display name of the demo
1. Import Frame
//
Nearbyviewcontroller.m
Neardemo
//
Created by Apple on 14/12/5.
Copyright (c) 2014 Youdianshang. All rights reserved.
//
#import
#import
@interface Nearbyviewcontroller ()
{
Nsuserdefaults *defaluts;
NSString * JINGDUSTR;
NSString *weidustr;
NSString *locationaddress;
}
@property (Nonatomic,retain) Cllocationmanager *locationmanager;
@property (strong,nonatomic) Uilabel *positionlabel;
/*
Latitude Related Settings
*/
-(void) Getlocationall
{
Self.locationmanager = [[Cllocationmanageralloc]init];
_locationmanager.delegate =self;
_locationmanager.desiredaccuracy =kcllocationaccuracybest;
_locationmanager.distancefilter = 10;
[_locationmanagerrequestalwaysauthorization];//Add this sentence
[_locationmanagerstartupdatinglocation];
}
/*
Through latitude to get the current location
-(void) change{
Defaluts=[nsuserdefaultsstandarduserdefaults];
cllocationcoordinate2d coordinate;
Coordinate.latitude = [[nsstringstringwithformat:@ "%@", Jingdustr]floatvalue];
Coordinate.longitude = [[nsstringstringwithformat:@ "%@", Weidustr]floatvalue];
Cllocation *newlocation=[[cllocationalloc]initwithlatitude:coordinate.latitudelongitude:coordinate.longitude];
Clgeocoder *geocoder=[[clgeocoderalloc] init];
[Geocoder reversegeocodelocation:newlocationcompletionhandler:^ (nsarray* placemarks,nserror* error)
{
Mkplacemark*placemark = [Placemarks objectatindex:0];
locationaddress = [nsstringstringwithformat:@ "%@%@%@%@", Placemark.locality, Placemark.sublocality, Placemark.thoroughfare, Placemark.subthoroughfare];
[Defaluts setobject:locationaddress forkey:@ "Address"];
NSLog (@ "ultimate goal: 1:%@2:%@3:%@4:%@", Placemark.locality, Placemark.sublocality,placemark.thoroughfare, Placemark.subthoroughfare);
}];
#pragma mark--Implement Cllocationmanagerdelegate proxy method, obtain geographic location (wrought) ======================================================= ====================
(1) Get to the position data, return is a cllocation array, generally use one of the
-(void) Locationmanager: (Cllocationmanager *) Manager didupdatelocations: (Nsarray *) Locations
{
Cllocation *currlocation = [locations Lastobject];
NSLog (@ "Longitude =%f latitude =%f height =%f", CurrLocation.coordinate.latitude, CurrLocation.coordinate.longitude, Currlocation.altitude);
Jingdustr =[nsstringstringwithformat:@ "%f", CurrLocation.coordinate.latitude];
Weidustr =[nsstringstringwithformat:@ "%f", CurrLocation.coordinate.longitude];
NSLog (@ "jijijijijij:%@", jingdustr);
[Self change];
}
(2) To obtain a callback method where the user's location data fails, notifying the user
-(void) Locationmanager: (Cllocationmanager *) Manager didfailwitherror: (nserror *) error
{
if ([error code] ==kclerrordenied)
{
Access Denied
}
if ([errorcode] = = Kclerrorlocationunknown) {
Unable to get location information
}
}