Some attributes of IOS Positioning

Source: Internet
Author: User

1. Obtain the current location

IOS provides a framework called corelocation. framework. The user can obtain his own positioning information (longitude and latitude ). See the following code snippet:

01if ([cllocationmanager locationservicesenabled]) {02 // when the positioning function is enabled, locate 03 cllocationmanager * manager = [[cllocationmanager alloc] init]; 04 manager. distancefilter = kcldistancefilternone; 05 manager. desiredaccuracy = kcllocationaccuracybest; 06 manager. delegate = self; 07 [Manager startupdatinglocation]; 08} 09-(void) locationmanager :( cllocationmanager *) manager10 didupdatetolocation :( cllocation *) newlocation11 fromlocation :( cllocation *) oldlocation12 {13 14 [Manager stopupdatinglocation]; 15} 16-(void) locationmanager :( cllocationmanager *) manager17 didfailwitherror :( nserror *) error18 {19 [Manager stopupdatinglocation]; 20}

As shown in the code above, cllocationmanager is an object class used to obtain positioning information. In actual applications, you can set the update frequency and positioning accuracy based on your own needs. The distancefilter in the Code indicates the distance from the update location. If the value exceeds the set value, the system locates and updates the location. Otherwise, the system does not update the location. Kcldistancefilternone in the Code indicates that no distance filtering is set, that is, the geographic location is updated at any time. The desiredaccuracy attribute indicates the accuracy of positioning. kcllocationaccuracybest indicates the most accurate positioning, but it also indicates that more time and power are required. Therefore, you should set it as needed.

So how does cllocationmanager enable location? He calls startupdatinglocation to enable the location function, and then uses stopupdatinglocation to stop the location. The location information is notified to the delegate object through loctionmanager: didupdatetolocation: fromlocation, therefore, the delegate object must implement the cllocationmanagerdelegate delegate. In the return location information principal method, the two main parameters are newlocation and oldlocation. newlocation indicates the latest location, and oldlocation indicates the last location. Both are cllocation objects. The attributes of cllocation are described as follows:

Attribute

Description

Altitude

Altitude

Coordinate

Longitude and latitude

Course

Driving direction

Horizontalaccuracy

Horizontal precision

Speed

Driving Speed

Timestamp

Timestamp

Verticalaccuracy

Vertical precision

 

2. Obtain geographic location information

When you get a latitude and longitude information, there may be another requirement, that is, what is the geographic location information corresponding to the current latitude and longitude. At this time, we need to use the framework to implement this function for us, that is, mapkit. framework. There is a class named mkreversegeocoder in this framework that can help us reverse parse the geographical location. Take a look at the Code:

01-(void) locationmanager :( cllocationmanager *) manager02 didupdatetolocation :( cllocation *) newlocation03 fromlocation :( cllocation *) oldlocation04 {05 bytes * geocoder = [[Export alloc] region: newlocation. coordinate]; 06 geocoder. delegate = self; 07 [geocoder start]; 08} 09 10-(void) reversegeocoder :( mkreversegeocoder *) geocoder11 didfindplacemark :( mkplacemark *) placem Ark12 {13 nslog (@ "\ n country: % @ \ n postalcode: % @ \ n isocountrycode: % @ \ n locality: % @ \ n sublocality: % @ \ n administrativearea: % @ \ n subadministrativearea: % @ \ n Thoroughfare: % @ \ n subthoroughfare: % @ \ n ", 14 placemark. country, 15 placemark. postalcode, 16 placemark. isocountrycode, 17 placemark. administrativearea, 18 placemark. subadministrativearea, 19 placemark. locality, 20 placemark. sublocality, 21 placemark. th Oroughfare, 22 placemark. subthoroughfare); 23} 24 25-(void) reversegeocoder :( handle *) geocoder26 handle :( nserror *) error27 {28 nslog (@ "reverse geocoder fail !! "); 29}

The above code immediately parses the reverse geographic location after obtaining the longitude and latitude. In fact, mkreversegeocoder is easy to use. After initialization by longitude and latitude, you can directly call the start method to implement reverse resolution, and then wait for the return. Its return is notified by delegation. Therefore, the delegate object must implement the mkreversegeocoderdelegate delegate. After successful resolution, a mkplacemark object is returned, which contains the relevant geographic location information (including country, region, street, and so on ).

However, mkreversegeocoder has become a class not recommended since ios5. Therefore, a new class replaces its function, that is, the clgeocoder class, which is also very easy to use for reverse parsing. Please refer to the following code:

01 clgeocoder * geocoder = [[clgeocoder alloc] init]; 02 [geocoder reversegeocodelocation: newlocation 03 completionhandler: ^ (nsarray * placemarks, 04 nserror * error) 05 {06 clplacemark * placemark = [placemarks objectatindex: 0]; 07 nslog (@ "Name: % @ \ n country: % @ \ n postalcode: % @ \ n isocountrycode: % @ \ n Ocean: % @ \ n inlandwater: % @ \ n locality: % @ \ n sublocality: % @ \ n administrativearea: % @ \ n subadministrativearea: % @ \ n Thoroughfare: % @ \ n subthoroughfare: % @ \ n ", 08 placemark. name, 09 placemark. country, 10 placemark. postalcode, 11 placemark. isocountrycode, 12 placemark. ocean, 13 placemark. inlandwater, 14 placemark. administrativearea, 15 placemark. subadministrativearea, 16 placemark. locality, 17 placemark. sublocality, 18 placemark. thoroughfare, 19 placemark. subthoroughfare); 20}];

From the code point of view, the clgeocoder class does not notify the return status in the form of delegation, but calls back through the block method, and the mkreversegeocoder delegate only returns a landmark location, clgeocoder returns an array containing multiple landmark locations, but this array usually has only one element, if multiple elements exist, the longitude and latitude of the given resolution are parsed to multiple different landmarks. If the parsing is incorrect or the cancel method is called, this parameter is nil.

3. Map Display

It is far from enough to describe the location information more vividly, because using a map to display the geographical location will bring a new user experience. Google maps have been encapsulated in the SDK in IOS. We can use a small amount of code to perform many map operations (such as marking positions and drawing lines ). The following code generates a map and displays it on the interface:

1-(void) viewdidload2 {3 [Super viewdidload]; 4 mkmapview * mapview = [[mkmapview alloc] initwithframe: cgrectmake (0.0, 0.0, 320.0, 460.0)]; 5 mapview. delegate = self; 6 [self. view addsubview: mapview]; 7 [mapview release]; 8}

This is simple enough. The preceding map delegate object is used to mark the geographical location. So how can we display the obtained longitude and latitude information on the map? In fact, each coordinate information displayed in the map corresponds to a mkannotationview, and mkannotationview is responsible for parsing a data object that implements the mkannotation protocol. Therefore, the first thing we need to do is to convert the obtained longitude and latitude to the mkannotation protocol object. First, define a class that implements the mkannotation protocol:

01 @ interface demoannotation: nsobject <mkannotation> {02 cllocationcoordinate2d _ coordinate; 03} 04 05-(ID) initwithcoordinate :( cllocationcoordinate2d) coordinate; 06 07 @ end08 09 @ implementation demoannotation10 11 @ synthesize coordinate = _ coordinate; 12 13-(ID) initwithcoordinate :( strong) Coordinate {14 if (Self = [Super init]) {15 _ coordinate = coordinate; 16} 17 return self; 18} 19 20-(void) setcoordinate :( cllocationcoordinate2d) newcoordinate {21 _ coordinate = newcoordinate; 22} 23 24-(nsstring *) Title {25 return @ "My location"; 26} 27 28-(nsstring *) Subtitle {29 return nil; 30} 31 32 @ end

The above class simply saves the latitude and longitude information. Remember to note the role of the title and subtitle in the mkannotation protocol. If you set the canshowcallout attribute to yes when annotationview is displayed, a callout view is displayed when you click annotationview, displays the title and subtitle. If the title is set to nil, The callout view is not displayed even if canshowcallout is set to yes.

Next, you need to rewrite the method after obtaining the location information. After obtaining the location information, set the latitude and longitude to the map for display. The implementation code is as follows:

01-(void) locationmanager :( cllocationmanager *) manager02 locate :( cllocation *) newlocation03 fromlocation :( cllocation *) oldlocation04 {05 demoannotation * annotation = [[demoannotation alloc] locate: newlocation. coordinate]; 06 [_ mapview addannotation: annotation]; 07 [annotation release]; 08 09 mkreversegeocoder * geocoder = [[mkreversegeocoder alloc] initwithcoordinate: newlocation. coordinate]; 10 geocoder. delegate = self; 11 [geocoder start]; 12}

In the above Code, the rough part of the Code encapsulates the latitude and longitude information into the defined object. Then the addannotation method is used to pass to mapview. Here _ mapview changes the temporary variables in the previous viewdidload method to class attributes for cross-method reference. My position has been introduced into the map after the above steps, but it is not displayed on the map yet, because we need to implement the Protocol in mapview to tell mapview how to display your annotation. The following code uses the default pin style in IOS to display the position. As follows:

01-(mkannotationview *) mapview :( mkmapview *) mapview02 viewforannotation :( id <mkannotation>) annotation03 {04 nsstring * annotationviewid = @ "comment"; 05 comment * annotationview = (comment *) 06 [mapview preview: annotationviewid]; 07 if (annotationview = nil) 08 {09 annotationview = [[comment alloc] Comment: annotation reuseidentifier: annotationviewid] autorelease]; 10 annotationview. canshowcallout = yes; 11} 12 Return annotationview; 13}

The above Protocol method is mapview to tell you which annotation he needs to display, and then the displayed style is determined by the user, but it must inherit the mkannotationview class.

The Application of positioning and map has come to an end. Of course, mkmapview has some more advanced features.




Http://hi.baidu.com/wolf_childer/item/cc7c2bf7a2b88311fe3582f8

Related Article

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.