Corelocation positioning using the system

Source: Internet
Author: User
Tags reverse geocoding

//
Viewcontroller.m
LBS
//
Created by Tonnyhuang on 15/8/28.
Copyright (c) 2015 Tonnyhuang. All rights reserved.
//

#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>

First, we need to import the Corelocation system framework into the project. The header file is then introduced into our controller.
Then, declare a Cllocationmanager object as a member variable that is used to locate the latitude and longitude coordinates and to comply with the protocol Cllocationmanager.
@interface Viewcontroller () <CLLocationManagerDelegate>
{
Cllocationmanager *_locationmanager;
}

@end

@implementation Viewcontroller



Implementing proxy methods in them
#pragma mark-cllocationmanagerdelegate
-(void) Locationmanager: (Cllocationmanager *) Manager
Didupdatetolocation: (cllocation *) newlocation
Fromlocation: (cllocation *) oldlocation {
Get longitude
NSLog (@ "Longitude = =%lf", newLocation.coordinate.longitude);
Get latitude
NSLog (@ "latitude = =%lf", newLocation.coordinate.latitude);
Get the name of your current city
Clgeocoder *geocoder = [[Clgeocoder alloc] init];
Reverse geocoding of address information based on latitude and longitude
[Geocoder reversegeocodelocation:newlocation completionhandler:^ (Nsarray *placemarks, Nserror *error) {
Clplacemark *placemark = [Placemarks objectatindex:0];
NSLog (@ "%@", placemark.name);
Get the city
NSString *city = placemark.locality;

The city information of the four municipalities can not be obtained by locality, but only by obtaining the province's method (if it is empty, it is known as the municipality

if (!city) {
City = Placemark.administrativearea;
} else if (Error = = Nil && [placemarks count] = = 0) {
NSLog (@ "No result were returned");
} else if (Error! = nil) {
NSLog (@ "error =%@", error);
}
NSLog (@ "city =%@", city);
}];
The system will keep updating the data until you choose to stop the update, because we just need to get the latitude and longitude, so get it and stop the update.
}



Finally, the location manager is initialized in Viewdidload.
-(void) Viewdidload {
[Super Viewdidload];
[Self initializelocationservice];
}

-(void) Initializelocationservice {
Initializing the location Manager
_locationmanager = [[Cllocationmanager alloc] init];
Set up Proxy
_locationmanager.delegate = self;
Set positioning accuracy to meters
_locationmanager.desiredaccuracy = Kcllocationaccuracybest;
Set Filter to None
_locationmanager.distancefilter = Kcldistancefilternone;
Start positioning
[_locationmanager requestalwaysauthorization];
There are two ways to get location permissions, depending on your location usage
[_locationmanager startupdatinglocation];
}

If normal positioning is required, the IOS8 needs to handle two additional places relative to iOS7.
1. Two fields are added to the project's plist file: The Nslocationalwaysusagedescription,//nslocationwheninuseusagedescription,type type is string, Values can be filled according to your needs (or can not be filled out), the content will be displayed in the app prompts the user whether to allow positioning of the alert content, the specific effect can be self-testing, here is no additional.This place is not wrong. Add a field to the info.plist above2. Call the location method before you need to call a function, directly on the above IOS7 initialize the location service method inside the modification can, as follows:

Start positioning
There are two ways to get location permissions, depending on your location usage
One is requestalwaysauthorization, one is requestwheninuseauthorization.
[_locationmanager requestalwaysauthorization];//This sentence iOS8 above version use.
[_locationmanager startupdatinglocation];


-(void) didreceivememorywarning {
[Super didreceivememorywarning];
Dispose of any resources the can be recreated.
}
@end

Corelocation positioning using the system

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.