iOS Geographic Information usage

Source: Internet
Author: User

Overview

Now many social, e-commerce, group buying applications have introduced maps and positioning capabilities, and it seems that map functionality is no longer unique to map applications and navigation applications. Indeed, having maps and positioning features really makes our lives more colorful and dramatically changes our way of life. For example, you go to a strange place to find nearby hotels, supermarkets and so on can open the software search surrounding; Similarly, there are a lot of buying software can automatically recommend certain items to you based on your location. In short, the current map and positioning functions have been introduced into the application development. Join us today to see how iOS maps and locates development.

    1. Positioning
    2. Map
Positioning

To implement maps and navigation, it is often necessary to familiarize yourself with the positioning function and navigate through the core location framework in iOS. The Core location itself can be used alone, and the map development framework Mapkit is completely independent, but often map development is used in conjunction with the positioning framework. Positioning, geocoding (including anti-coding) features are primarily included in the core location.

Positioning function

Positioning is a very common feature, such as some map software opened after the user allows the software to locate, then open the software will automatically lock to the current location, if the user mobile phone so the current location will also follow the change. To implement this feature requires the Cllocationmanager class in core loaction, first look at some of the main methods and properties of this class:

Class method Description
+ (BOOL) locationservicesenabled; Whether to enable location services, typically if the user does not have location services enabled to prompt the user to open Location services
+ (clauthorizationstatus) authorizationstatus; Locate the service authorization status and return the enumeration type:
kclauthorizationstatusnotdetermined: User has not made a decision whether to enable location services
Kclauthorizationstatusrestricted: The user is not authorized to use location services and may not be authorized by the user to disable access
Kclauthorizationstatusdenied: The user has explicitly banned the app from using location services or the current system location service is turned off
Kclauthorizationstatusauthorizedalways: App is authorized to use location services all the time, even if the app is not in use
Kclauthorizationstatusauthorizedwheninuse: Allow access to location services during use of this application
Property Description
Desiredaccuracy Positioning precision, enumeration type:

Kcllocationaccuracybest: The most accurate positioning
Cllocationaccuracy kcllocationaccuracynearesttenmeters: 10 m error Range
Kcllocationaccuracyhundredmeters: Hundred meters error Range
Kcllocationaccuracykilometer: km Error range
Kcllocationaccuracythreekilometers: 3-kilometer Error Range

Distancefilter Location information update minimum distance, only move is greater than this distance to update location information, default is kcldistancefilternone: no distance limit
Object methods Description
Startupdatinglocation Start location tracking, will be executed according to user set update frequency ( void) Locationmanager: (Cllocationmanager *) Manager didupdatelocations: (Nsarray *) locations; Method Feedback Positioning Information
Stopupdatinglocation STOP location Tracking
Startupdatingheading Start navigation direction tracking
Stopupdatingheading Stop navigation Direction tracking
Startmonitoringforregion: Start tracking a region and start locating a region. If a user enters or walks out of an area, it calls -(void) Locationmanager: (Cllocationmanager *) Manager
Didenterregion: (clregion *)
region and -(void) Locationmanager: (Cllocationmanager *) Manager
Didexitregion: (clregion *)
Region Agent method feedback related information
Stopmonitoringforregion: Stop location tracking for an area
Requestwheninuseauthorization Request for Location Service authorization when the app is used, note before using this method before you configure nslocationwheninuseusagedescription in Info.plist
Requestalwaysauthorization The request to get the app has been using Location services Licensing, note that you need to configure nslocationalwaysusagedescription in Info.plist before using this method
Proxy method Description
-(void) Locationmanager: (Cllocationmanager *) Manager didupdatelocations: (Nsarray *) locations; Execution after the position has changed (it will also be executed after the first position to a location)
-(void) Locationmanager: (Cllocationmanager *) Manager
Didupdateheading: (clheading *) newheading;
Execution after changes in navigation direction
-(void) Locationmanager: (Cllocationmanager *) Manager
Didenterregion: (Clregion *) region
Execute after entering an area
-(void) Locationmanager: (Cllocationmanager *) Manager
Didexitregion: (Clregion *) region
Go out of a region and execute

IOS 8 also provides a more user-friendly positioning service option. The app's location service is no longer just turned off or turned on, and now the location service is enabled with three options, "never", "Use Application period" and "always". At the same time, given the power consumption issue, if an app requires that location services always be available in the background, IOS 8 will not only proactively ask you when the app is first opened, but also remind you that the app has been using the location service in the background and asks if you want to continue to allow it. In IOS7 and previous versions, if you use location services in your application as long as the Startupdatinglocation method app is called in the program, the user is asked whether or not the app is allowed to use location services. At the same time, this configuration is optional by configuring privacy-location usage Description to tell the user what to use in Info.plist during the prompt.
But configuring configuration items in IOS8 changes, you can configure nslocationalwaysusagedescription or Nslocationwheninuseusagedescription to tell the user the purpose of using location services, and note that this configuration is required, and if not configured, the app cannot use location services by default. Opening the app does not give you a hint to open the location service unless you set up the app's location service after installation. At the same time, the Requestalwaysauthorization or locationservicesenabled method needs to be requested in the application according to the configuration. As I machine has been updated to the latest iOS8.1 content is mainly for iOS8, using iOS7 friends need a little adjustment.

kcmainviewcontroller.m//corelocation////Created by Kenshin Cui on 14-03-27.//Copyright (c) 2014 Kenshin Cui. All rights reserved.//#import "KCMainViewController.h" #import <CoreLocation/CoreLocation.h> @interface Kcmainviewcontroller () <cllocationmanagerdelegate>{Cllocationmanager *_locationmanager;}        @end @implementation kcmainviewcontroller-(void) viewdidload {[Super viewdidload];        Location Manager _locationmanager=[[cllocationmanager Alloc]init]; if (![ Cllocationmanager locationservicesenabled]) {NSLog (@ "Location service may not be open now, please set it open!        ");    Return }//If no authorization is requested, the user is authorized to authorize if ([Cllocationmanager authorizationstatus]==kclauthorizationstatusnotdetermined) {[_lo    Cationmanager Requestwheninuseauthorization]; }else if ([Cllocationmanager authorizationstatus]==kclauthorizationstatusauthorizedwheninuse) {//Set proxy _locati        onmanager.delegate=self;     Set positioning accuracy _locationmanager.desiredaccuracy=kcllocationaccuracybest;   Positioning frequency, every number of meters to locate once cllocationdistance distance=10.0;//10 meters positioning once _locationmanager.distancefilter=distance;    Start track positioning [_locationmanager startupdatinglocation]; }} #pragma mark-corelocation agent #pragma mark tracks the location agent method, which will be executed each time the position changes (as long as the location is located)//You can set a virtual location through the emulator, otherwise this method cannot be called in the Emulator-(void) Locationmanager: (Cllocationmanager *) Manager didupdatelocations: (Nsarray *) locations{cllocation *location=[ Locations firstobject];//take out the first position cllocationcoordinate2d coordinate=location.coordinate;//position coordinates NSLog (@ "Longitude:%f, Latitude:%f, Elevation:%f, heading:%f, walking speed:%f ", coordinate.longitude,coordinate.latitude,location.altitude,location.course,location.speed    ); If you do not need real-time positioning, use the end even if you close the location service [_locationmanager stopupdatinglocation];} @end

Attention:

1. The positioning frequency and positioning accuracy should not be more accurate and better, depending on the actual situation, because the more accurate the more cost-performance, the more electricity.

2. If the positioning is successful, it will be called frequently according to the setting condition -(void) Locationmanager: (Cllocationmanager *) Manager didupdatelocations: (Nsarray *) Locations method, this method returns an array of geo-location objects, each of which represents the location information (including longitude, latitude, poster, walking speed, and so on) and returns the array because sometimes a location point may contain multiple locations.

3. If you do not need real-time monitoring after using location services, you should immediately turn off location services to conserve resources.

4. In addition to providing positioning capabilities, Cllocationmanager can also call the Startmonitoringforregion: method to monitor the specified area.

iOS Geographic Information usage

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.