IOS8 Display place names according to latitude and longitude

Source: Internet
Author: User
Tags error code

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

}

}

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.