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