IOS map positioning uses several small Functions

Source: Internet
Author: User

IOS map positioning uses several small Functions
The positioning service provided by the iOS system can meet many requirements. For example, obtain the current longitude and latitude, and obtain the current location information.

Obtain the current longitude and latitude

First import # import Defines the instance of CLLocationManager to implement CLLocationManagerDelegate.

@interface ViewController ()
 
  {    CLLocationManager *_locationManager;}@end
 

Start locating:

-(Void) startLocating {if ([CLLocationManager locationServicesEnabled]) {_ locationManager = [[CLLocationManager alloc] init]; // sets the positioning precision [_ locationManager setDesiredAccuracy: Disabled]; _ locationManager. distanceFilter = 100366f; _ locationManager. delegate = self; if ([[[UIDevice currentDevice] systemVersion] floatValue]> 8.0) {[_ locationManager requestAlwaysAuthorization]; [_ locationManager requestWhenInUseAuthorization];} // start locating in real time [_ locationManager startUpdatingLocation];}

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 :( clthorizationstatus) status {NSLog (@ "longpolling = % f", manager. location. coordinate. longpolling); 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 (Zone) NSLog (@ "% @", [test objectForKey: @ "Country"]); NSLog (@ "% @", [test objectForKey: @ "State"]); NSLog (@ "% @", [test objectForKey: @ "SubLocality"]); NSLog (@ "% @", [test objectForKey: @ "Street"]) ;}}] ;}

In this way, the detailed information of the current location is obtained.

Obtain the longitude and latitude of a location

 

-(Void) login :( NSString *) city {// city can be a 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 * firstPlace Mark = [placemarks objectAtIndex: 0]; NSLog (@ "longpolling = % f", firstPlacemark. location. coordinate. longpolling); 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) distancebylongdistance :( double) longitude1 latitude :( double) latitude1 longdistance :( double) longitude2 latitude :( double) latitude2 {CLLocation * curLocation = [[CLLocation alloc] initWithLatitude: latitude1 longdistance: longitude1]; CLLocation * otherLocation = [[CLLocation alloc] initWithLatitude: latitude2 longpolling: longitude2]; double distance = [curLocation distanceFromLocation: otherLocation]; // The unit is m return distance ;}

First, we can use the getLongitudeAndLatitudeWithCity method to obtain the longitude and latitude of a location. For example, if we obtain the Longitude and Latitude of Beijing and Shanghai respectively: Beijing longpolling = 116.405285, Latitude = 39.904989 Shanghai longpolling = 121.472644, and Latitude = 31.231706, the distance between Beijing and Shanghai 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, which may be less accurate. The input result is:

distance = 1066449.749194

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

For more iOS technologies, please pay attention to iOS development

iOSDevTip


 

 

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.