iOS development-MAP-positioning

Source: Internet
Author: User

During the development of iOS, there are some development needs, such as positioning, navigation and so on. Especially in recent days, there are more and more maps in app development, so we have to grasp it for the simple map development.

First of all, we will use the Apple's own map function for related development, of course, in the later we can also use some domestic maps, such as Baidu map and so on.

I personally in the process of learning map development, began to feel more cumbersome, many classes Ah, frame Ah, related library Ah, can not remember. But as long as the practice of the map development is relatively simple. Beginners began to learn the map as long as the simple application of the map can be.

First of all, for positioning, we will first import a library:coreloaction.framework.

IOS7 and iOS8 positioning are different, iOS8.0 after the opening of the positioning needs to perform requestalwaysauthorization/requestwheninuseauthorization, and also configure Info.plist. nslocationalwaysusagedescription/nslocationwheninuseusagedescription

First step: We need to create a location manager

Cllocationmanager * manager = [[Cllocationmanager alloc]init];

After creation, you can also set related properties

Set longitude for positioning

_manager.desiredaccuracy = Kcllocationaccuracybest;

Set the update frequency for the location to be updated in meters beyond this range

_manager.distancefilter = 5;

Also need to set start positioning

[_manager startupdatinglocation];

The second step: for the iOS7 and iOS8 positioning methods, I summed up a few three ways.

Method One: Determine the version

CGFloat systemversion = [Uidevice currentdevice].systemversion.floatvalue;

if (systemversion >= 8.0)

{

Open position when used

[Self.manager requestwheninuseauthorization];

}

Method Two: Determine whether the method can be executed

if ([Self.manager respondstoselector: @selector (requestwheninuseauthorization)])

{

If it can respond, execute

[Self.manager requestwheninuseauthorization];

}

Method Three: Determine whether location services open the recommended Method!

if (![ Cllocationmanager locationservicesenabled]) {

NSLog (@ "Prompt user to open location service");

}

Else

{

1. Get the status of the current location

Clauthorizationstatus status = [Cllocationmanager authorizationstatus];

If the positioning state is not open

if (status = = kclauthorizationstatusnotdetermined) {

Requestwheninuseauthorization Front-end positioning

Requestalwaysauthorization front-end and background positioning

[Self.manager requestwheninuseauthorization];

}

Turn on positioning

[Self.manager startupdatinglocation];

}

The third step: when we solve the version problem, and successfully opened the positioning, then we will get our current coordinate position.

This is the method returned by the Cllocationmanagerdelegate in the locations can get the coordinate position data we want

-(void) Locationmanager: (Cllocationmanager *) Manager didupdatelocations: (nsarray<cllocation *> *) Locations

We will then write the following code in this method:

This agent only needs to perform all stop location updates Once the positioning is successful

[Manager Stopupdatinglocation];

Get the current location information

cllocation * location = [locations Lastobject];

Get latitude and longitude

cllocationcoordinate2d coordinate = location.coordinate;

Longitude longitude latitude latitude

NSLog (@ "Longitude%f latitude%f", coordinate.longitude,coordinate.latitude);

Finally, we get the latitude and longitude of our current position and realize the function of positioning. The current latitude and longitude can be set in the debug-loaction of the simulator.

iOS development-MAP-positioning

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.