Most of the time when we do projects, we use the mobile phone to get the location, and then pass the data to the backstage, this is the iOS8 according to the latitude and longitude display place name of the demo
1. Import the framework
//
Nearbyviewcontroller.m
Neardemo
//
Created by Apple on 14/12/5.
Copyright (c) year Youdianshang. All rights reserved.
//
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
@interface nearbyviewcontroller () <Uitableviewdatasource,uitableviewdelegate, cllocationmanagerdelegate,datarequestmanagerdelegate>
{
nsuserdefaults *defaluts;
nsstring * JINGDUSTR;
nsstring *weidustr;
nsstring *locationaddress;
}
@property(nonatomic,retain)cllocationmanager *locationmanager;
@property(strong,nonatomic)UILabel *positionlabel;
/*
latitude and longitude related settings
*/
-(void) Getlocationall
{
self. Locationmanager = [[cllocationmanageralloc]init];
_locationmanager. Delegate = Self ;
_locationmanager. Desiredaccuracy =kcllocationaccuracybest;
_locationmanager. Distancefilter = Ten ;
[_locationmanagerrequestalwaysauthorization]; // Add this sentence
[_locationmanagerstartupdatinglocation];
}
/*
// get current location by latitude
-(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 the Cllocationmanagerdelegate proxy method to obtain a geographic location (wrought)============================ ===============================================
//(1) Gets the location data, returns an array of cllocation , typically using 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) gets the callback method for the user's location data failure, which notifies the user
-(void) Locationmanager: (cllocationmanager *) Manager didfailwitherror: (nserror *) error
{
if ([Error code] = =kclerrordenied)
{
// access is denied
}
if ([Errorcode] = = Kclerrorlocationunknown) {
// cannot get location information
}
}
IOS8 display of place names according to latitude and longitude