Reprint]ios lbs function detailed [0] (get latitude and longitude) [1] (Get current location text)

Source: Internet
Author: User

Original Address:IOS lbs feature detailed [0] (get latitude and longitude) [1] (get current geo-location text Kojiro Sasaki

Because the function of lbs is to be used on the recent project. So I did a bit of pre-research.

in general, the LBS function is generally divided into two pieces, one is geo-positioning, is to obtain the current accuracy, latitude and location of the function, this part of the function is mainly used in corelocation.frameworks. Part of this is to display the map information, enrich the map content, and so on, this part mainly uses mapkit.frameworks. The test of these functions is best done on the real machine. The simulator is typically located at Apple's California headquarters.

First introduce corelocation. Let's talk about the main features first.

The first feature, and also the most basic function, is to get the longitude latitude.

Toss the original is the assignment of the wrong posture, frustration feeling really strong AH.

Well, to get down to the chase, getting longitude latitude on iOS is fairly straightforward.

Add the corelocation.frameworks and place its header file in the header file of the class that you want to refer to. Creates a Cllocationmanager object. Sets the proxy for the current Cllocationmanager object. Set other basic properties as follows:

Set addressing longitude

Self.locManager.desiredAccuracy = Kcllocationaccuracybest;

Self.locManager.distanceFilter = 5.0;

Then call -(void) startupdatinglocation; method to begin updating the current coordinate position.

So the preparatory work is finished.

Then you have to complete the proxy method.

If only to achieve the current latitude and longitude of the function, as long as the implementation of the following two agents is good.

-(void) Locationmanager: (Cllocationmanager *) Manager

Didupdatetolocation: (cllocation *) newlocation

Fromlocation: (cllocation *) oldlocation;

-(void) Locationmanager: (Cllocationmanager *) Manager

Didfailwitherror: (Nserror *) error;

The second agent is called when the addressing fails. Cheese in the inside to do the corresponding treatment is good.

We mainly talk about the first agent. The first proxy method passed to us three parameters,Manager,NewLocation and oldlocation. If only the location, as long as care newlocation this is good.

Where do we put the longitude dimension?

Cllocation has a property called @property (readonly, nonatomic) cllocationcoordinate2d coordinate; The information we want is stored here.

typedef struct {

Cllocationdegrees latitude;

Cllocationdegrees longitude;

} cllocationcoordinate2d;

Cllocationdegrees In fact is a double type, we will be the text of it.

Attention:. It's good to use startupdatinglocation when using the positioning function for the first time. The system will automatically eject the pop-up window that you are authorized to use. In the second use, determine whether the user opens the location function using the + (BOOL) Locationservicesenabled method to determine that no error is generated. Call Stopupdatinglocation to stop the last request before the request ends or a second request is made .

The following is an implementation code:

Initialize and start the update

self. Locmanager = [[cllocationmanager alloc] init];

self. Locmanager. Delegate = self;

self. Locmanager. Desiredaccuracy = kcllocationaccuracybest;

self. Locmanager. Distancefilter = 5.0;

[self. Locmanager startupdatinglocation];

Proxy method implementation

-(void) Locationmanager: (cllocationmanager *) Manager didupdatetolocation: (cllocation *) NewLocation fromlocation: (cllocation *) oldlocation{

debuglog(@ "%f,%f", NewLocation. Coordinate. Latitude, NewLocation. Coordinate. Longitude);

}

-(void) Locationmanager: (cllocationmanager *) Manager Didfailwitherror: (nserror *) error{

debuglog(@ "%@", error);

}

Obtaining location information for the current location requires the use of a new class,Mkreversegeocoder. This class is in mapkit.framework. We add the framework and import the header file to use.

Hit a code, the results found that the class iOS5.0 is not necessary. Really In order to take care of compatibility, we first study Mkreversegeocoder, and then to study the new class, well, the name is Clgeocoder, well, not misspelled. inside the corelocation.

Mkreversegeocoder *geocoder = [[Mkreversegeocoder alloc] initwithcoordinate:currentcoordinate2d];

Geocoder.delegate = self;

[Geocoder start];

After calling the above code, the reverse address encoding API is automatically invoked . We use proxies to receive this. As for the proxy method, we need to implement two.

-(void) Reversegeocoder: (Mkreversegeocoder *) Geocoder Didfindplacemark: (Mkplacemark *) Placemark {

}

-(void) Reversegeocoder: (Mkreversegeocoder *) Geocoder didfailwitherror: (Nserror *) error{

}

The first method is to obtain a reverse-coded. The second is when the reverse encoding fails, the processing error is used.

We mainly discuss the first method.

Placemark (anobject of the Mkplacemark Class) is actually a property of the Geocoder (the object of the Mkreversegeocoder class). From Geocoder inside take placemark this and direct take placemark This actually difference is not big. And the information we need is mostly in this.

This dictionary holds the underlying data

@property (nonatomic, readonly) nsdictionary *addressdictionary;

Let's try to get something out of this dictionary.

Here is the dictionary I poured out with this addressdictionary property. Let's look at the analysis.

{

City = "U897FU5B89U5E02";//Urban name

Country = "U4E2DU56FD";//Country name

CountryCode = cn;//Country Code

Formattedaddresslines = (

"U4E2DU56FD",

"U9655U897FU7701U897FU5B89U5E02U96C1U5854U533A",

"U9ad8u65b0u516du8def34u53f7"

); This is supposed to be the formatted address.

State = "u9655u897fu7701"; Province

Street = "U9ad8u65b0u516du8def 34u53f7";//Street Full Name

sublocality = "u96c1u5854u533a";//Zone name

Subthoroughfare = "34U53F7";//Specific address

Thoroughfare = "U9ad8u65b0u516du8def";//Street name

}

Note: The above dictionary is a dictionary that can be converted directly to a contact, via this abcreatestringwithaddressdictionary property.

The following are the other properties of Placemark. You can take it at your own discretion.

Address Dictionary Properties

@property (nonatomic, readonly) nsstring *name;//EG. Apple INC.

@property (nonatomic, readonly) nsstring *thoroughfare;//street address, eg. 1 Infinite Loop

@property (nonatomic, readonly) nsstring *subthoroughfare;//eg. 1

@property (nonatomic, readonly) nsstring *locality;//city, eg. Cupertino

@property (nonatomic, readonly) nsstring *sublocality;//neighborhood, common name, eg. Mission District

@property (nonatomic, readonly) nsstring *administrativearea;//state, eg. CA

@property (nonatomic, readonly) nsstring *subadministrativearea;//county, eg. Santa Clara

@property (nonatomic, readonly) nsstring *postalcode;//zip code, eg. 95014

@property (nonatomic, readonly) nsstring *isocountrycode;//EG. US

@property (nonatomic, readonly) nsstring *country;//EG. States

@property (nonatomic, readonly) nsstring *inlandwater;//EG. Lake Tahoe

@property (nonatomic, readonly) nsstring *ocean;//EG. Pacific Ocean

@property (nonatomic, readonly) Nsarray *areasofinterest;//EG. Golden Gate Park

Note: I found in the process that if the network is suspended from death, it may not be possible to get adverse results for a long time. This may require attention.

It is not recommended to use a lot of the IOS5 I said above. We need to use this Clgeocoder class.

The method of use is also very simple. Refer to the following steps:

First create a Clgeocoder object, and then call his -(void) Reversegeocodelocation: (cllocation *) Location Completionhandler: (clgeocodecompletionhandler) Completionhandler; method. The parameter types are passed as required. Some cheese will ask this clgeocodecompletionhandler how to write something? This is actually IOS4 after the official highly recommended BLOCK, will not use the classmate quickly go to see the document it.

This is the definition of Clgeocodecompletionhandler. typedef void(^clgeocodecompletionhandler) (nsarray *placemarks, nserror *error); All we need to do is write a block object and pass it in.

The following is a reference code that uses Clgeocoder. No agent, is it very happy?

clgeocoder* Geocoder = [[clgeocoder alloc] init];

[Geocoder reversegeocodelocation: NewLocation completionhandler:

^ (nsarray* placemarks, nserror* error) {

NSLog(@ "%@", placemarks);

}];

The content of the positioning section is introduced here, as to what the forward encoding, what the earlier mentioned rate, distance and so on, we look at the document. It is a good package to look at the document. 、

In iOS development, if you want to get your current location, you only need the following steps:

First, import the Corelocation framework into the project and reference

[Plain]View Plaincopy
    1. #import <CoreLocation/CoreLocation.h>
Then, declare the implementation of the Proxy method,

[Plain]View Plaincopy
    1. @interface Locationmanager:nsobject <CLLocationManagerDelegate>
Then, start updating location information:

[Plain]View Plaincopy
    1. Locationmanager = [[Cllocationmanager alloc] init];
    2. Locationmanager.delegate = self;
    3. Locationmanager.desiredaccuracy = Kcllocationaccuracybest;
    4. Locationmanager.distancefilter = Kcldistancefilternone;
    5. [Locationmanager startupdatinglocation];

Locationmanager need to be defined as private or attribute variables. After each location update, cllocationmanagerdelegate-(void) Locationmanager is called: (Cllocationmanager *) Manager didupdatetolocation: ( Cllocation *) newlocation fromlocation: (cllocation *) Oldlocation method, the NewLocation of the method is the current position.

After acquiring the location, if you want to get the geographic information of the current location, you need to use Clgeocoder (Mkreversegeocoder is obsolete).

[Plain]View Plaincopy
    1. Clgeocoder *geocoder = [[Clgeocoder alloc] init];
    2. [Geocoder reversegeocodelocation:newlocation completionhandler:^ (Nsarray *array, Nserror *error) {
    3. if (Array.count > 0) {
    4. Clplacemark *placemark = [array objectatindex:0];
    5. NSString *country = Placemark. Isocountrycode;
    6. NSString *city = placemark.locality;
    7. }
    8. }];

Placemark contains the geographic information of the newlocation.

Reprint] IOS lbs feature detailed [0] (get latitude and longitude) [1] (Get current location text)

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.