IOS8 display place names based on longitude and latitude

Source: Internet
Author: User

IOS8 display place names based on longitude and latitude

We often use mobile phones to retrieve location information for projects and then transmit data to the background. This is a demo of iOS8 displaying place names based on longitude and latitude.

// 1. Import the framework

//

// NearByViewController. m

// NearDemo

//

// Created by apple on 14/12/5.

// Copyright (c) 2014 youdianshang. All rights reserved.

//

# Import

# Import

@ Interface NearByViewController ()


{

NSUserDefaults * defaluts;

NSString * jingduStr;

NSString * weiduStr;

NSString * LocationAddress;

}

@ Property (nonatomic, retain) CLLocationManager * locationManager;

@ Property (strong, nonatomic) UILabel * positionLabel;

/*

Latitude and longitude settings

*/


-(Void) getLocationAll

{


Self. locationManager = [[CLLocationManageralloc] init];

_ LocationManager. delegate = self;

_ LocationManager. desiredAccuracy = kCLLocationAccuracyBest;

_ LocationManager. distanceFilter = 10;

[_ LocationManagerrequestAlwaysAuthorization]; // Add this sentence

[_ LocationManagerstartUpdatingLocation];

}

/*

// Obtain the current geographical location through latitude and longitude

-(Void) change {

Defaluts = [NSUserDefaultsstandardUserDefaults];


CLLocationCoordinate2D coordinate;

Coordinate. latitude = [[NSStringstringWithFormat: @ "% @", jingduStr] floatValue];

Coordinate. longpolling = [[NSStringstringWithFormat: @ "% @", weiduStr] floatValue];


CLLocation * newLocation = [[CLLocationalloc] initWithLatitude: coordinate. latitudelongstrap: coordinate. longpolling];

CLGeocoder * geocoder = [[CLGeocoderalloc] init];

[Geocoder reverseGeocodeLocation: newLocationcompletionHandler: ^ (NSArray * placemarks, NSError * error)

{

MKPlacemark * placemark = [placemarks objectAtIndex: 0];

LocationAddress = [NSStringstringWithFormat: @ "% %%@%@", placemark. locality, placemark. subLocality, placemark. thoroughfare, placemark. subThoroughfare];

[Defaluts setObject: LocationAddress forKey: @ "address"];

NSLog (@ "ultimate goal: 1: % @ 2: % @ 3: % @ 4: % @", placemark. locality, placemark. subLocality, placemark. thoroughfare, placemark. subThoroughfare );


}];


# Pragma mark -- Implement the CLLocationManagerDelegate proxy method to obtain the geographical location) ========================================================== ==========================================


// (1) obtain the location data and return a CLLocation array.


-(Void) locationManager :( CLLocationManager *) manager didUpdateLocations :( NSArray *) locations

{

CLLocation * currLocation = [locations lastObject];

NSLog (@ "longitude = % f latitude = % f Height = % f", currLocation. coordinate. latitude, currLocation. coordinate. longpolling, currLocation. altitude );

JingduStr = [NSStringstringWithFormat: @ "% f", currLocation. coordinate. latitude];

WeiduStr = [NSStringstringWithFormat: @ "% f", currLocation. coordinate. longpolling];

NSLog (@ "jijijijijij: % @", jingduStr );

[Self change];


}

// (2) callback Method for retrieving user location data failure, which notifies the user

-(Void) locationManager :( CLLocationManager *) manager didFailWithError :( NSError *) error

{

If ([error code] = kCLErrorDenied)

{

// Access Denied

}

If ([errorcode] = kCLErrorLocationUnknown ){

// Unable to obtain location information

}

}




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.