iOS map targeting uses several small features

Source: Internet
Author: User

There are many requirements that can be achieved with the iOS system's own location services. For example: Get current latitude and longitude, get current location information, and so on.

get current latitude and longitude

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

@interface Viewcontroller () <cllocationmanagerdelegate>{    cllocationmanager *_locationmanager;} @end

How to start positioning:

-(void) startlocating{    if ([Cllocationmanager locationservicesenabled])    {        _locationmanager = [[ Cllocationmanager alloc] init];        Set the accuracy 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 the Proxy method:

-(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

-(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 more information about the current location.

Get latitude and longitude of a certain place

-(void) Getlongitudeandlatitudewithcity: (NSString *) city{//city can be 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).", (uns            igned 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

-(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 Getlongitudeandlatitudewithcity method above to obtain the latitude and longitude of a certain place. For example, we get Beijing and Shanghai latitude and longitude are: Beijing longitude = 116.405285,latitude = 39.904989 Shanghai longitude = 121.472644, Latitude = 31.231706, then Beijing and Shanghai The distance between the two is:

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

The approximate distance is calculated, and may not be so precise. Enter the result:

Distance = 1066449.749194

Code: Https://github.com/worldligang/MapDistanceMaster.git

For more iOS technologies, follow iOS development

Iosdevtip



iOS map targeting uses several small features

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.