An analysis of the basic methods of geographic location in IOS app _ios

Source: Internet
Author: User

The iOS system's own positioning service can achieve a lot of requirements. For example: Get the current latitude and longitude, get the current position information and so on.
There are 3 different ways to locate it:
1,gps, the most accurate way to locate
2, the cellular base station triangle positioning, this positioning in the signal base station comparison cheats city more accurate.
3,wifi, this approach appears to be the data obtained through the network operator's database, the most imprecise in 3 kinds of locating species

First you have to add two connection libraries, Mapkit and corelocation to your Xcode, as shown in

The core location provides the positioning function, can locate the device's current coordinates, simultaneously can obtain the device movement information, the most important class is Cllocationmanager, the localization management.
IOS8 began, the core Location framework changes mainly have the following points:
1. Introduce the concept of always and wheninuse in the positioning state.
2. Add visit monitoring features, which are particularly suitable for travel category applications where the monitor begins to function when the user reaches a specified area.
3. Add indoor positioning technology, increase clfloor, in the room can get floor information.

Get the current latitude and longitude degree

First, import #import <corelocation/corelocation.h>, define Cllocationmanager instance, and implement Cllocationmanagerdelegate.

Copy Code code as follows:

@interface Viewcontroller () <CLLocationManagerDelegate>
{
Cllocationmanager *_locationmanager;
}

@end


How to start positioning:
Copy Code code as follows:

-(void) startlocating
{
if ([Cllocationmanager locationservicesenabled])
{
_locationmanager = [[Cllocationmanager alloc] init];
Setting the precision of positioning
[_locationmanager Setdesiredaccuracy:kcllocationaccuracybest];
_locationmanager.distancefilter = 100.0f;
_locationmanager.delegate = self;
if ([[[[Uidevice Currentdevice] systemversion] floatvalue] > 8.0)
{
[_locationmanager requestalwaysauthorization];
[_locationmanager requestwheninuseauthorization];
}
Start real-time positioning
[_locationmanager startupdatinglocation];
}
}

To implement a proxy method:
Copy Code code as follows:

-(void) Locationmanager: (Cllocationmanager *) Manager didchangeauthorizationstatus: (clauthorizationstatus) status
{
NSLog (@ "longitude =%f", manager.location.coordinate.longitude);
NSLog (@ "Latitude =%f", manager.location.coordinate.latitude);
[_locationmanager stopupdatinglocation];
}

get Current Location information

In the proxy method above

Copy Code code as follows:

-(void) Locationmanager: (Cllocationmanager *) Manager didchangeauthorizationstatus: (clauthorizationstatus) status
{
NSLog (@ "longitude =%f", manager.location.coordinate.longitude);
NSLog (@ "Latitude =%f", manager.location.coordinate.latitude);
[_locationmanager stopupdatinglocation];

Clgeocoder * Geocoder = [[Clgeocoder alloc] init];
[Geocoder reverseGeocodeLocation:manager.location completionhandler:^ (Nsarray *placemarks, Nserror *error) {
For (Clplacemark * Placemark in Placemarks) {
Nsdictionary *test = [Placemark addressdictionary];
Country (country) state (city) sublocality (district)
NSLog (@ "%@", [Test objectforkey:@ "Country"]);
NSLog (@ "%@", [Test objectforkey:@ "State"]);
NSLog (@ "%@", [Test objectforkey:@ "sublocality"]);
NSLog (@ "%@", [Test objectforkey:@ "Street"]);
}
}];

}


This makes it easy to get the details of the current position.

Get the latitude and longitude of a certain place

Copy Code code as follows:

-(void) Getlongitudeandlatitudewithcity: (NSString *) City
{
City can be used for Chinese
NSString *oreillyaddress = city;
Clgeocoder *mygeocoder = [[Clgeocoder alloc] init];
[Mygeocoder geocodeaddressstring:oreillyaddress completionhandler:^ (Nsarray *placemarks, NSError *error) {
if ([placemarks count] > 0 && error = = nil)
{
NSLog (@ "Found%lu placemark (s).", (unsigned long) [placemarks Count]);
Clplacemark *firstplacemark = [Placemarks objectatindex:0];
NSLog (@ "longitude =%f", firstPlacemark.location.coordinate.longitude);
NSLog (@ "Latitude =%f", firstPlacemark.location.coordinate.latitude);
}
else if ([placemarks count] = = 0 && error = nil)
{
NSLog (@ "Found no placemarks.");
}
else if (Error!= nil)
{
NSLog (@ "An error occurred =%@", error);
}
}];
}

calculate the distance between two locations
Copy Code code as follows:

-(double) Distancebylongitude: (double) longitude1 Latitude: (double) latitude1 Longitude: (double) Longitude2 latitude :(double) latitude2{
cllocation* curlocation = [[Cllocation alloc] Initwithlatitude:latitude1 longitude:longitude1];
cllocation* otherlocation = [[Cllocation alloc] Initwithlatitude:latitude2 Longitude:longitude2];
Double distance = [curlocation distancefromlocation:otherlocation];//unit is M
return distance;
}

First we can use the above Getlongitudeandlatitudewithcity method to obtain the latitude and longitude of a certain place. For example, we get the latitude and longitude of Beijing and Shanghai respectively: Beijing longitude = 116.405285,latitude = 39.904989 Shanghai longitude = 121.472644, Latitude = 31.231706, then Beijing and Shanghai The distance between the two is:
Copy Code code as follows:

Double distance = [self distancebylongitude:116.405285 latitude:39.904989 longitude:121.472644 latitude:31.231706];
NSLog (@ "Latitude =%f", distance);

Calculated is the approximate distance, may not be so accurate. Enter the result as:

Distance = 1066449.749194

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.