Map-02. geocode,-02 geocode

Source: Internet
Author: User

Map-02. geocode,-02 geocode
Map-02. GeoCoding/ReverseGeoCoding)

The previous section briefly introduces how to obtain user locations and use longitude and latitude to represent locations. This section describes geographical encoding.

First, we need to know what is geocode

Concept:

Geographic code: it refers to the process of establishing the spatial coordinate relationship between statistical data or address information. It is called geographic code. converts a Chinese address or place name description to a longitude or latitude representation on a map (on the earth surface.

Anti-geographic code: converts the longitude and latitude of a map (on the earth's surface) to a Chinese address or place name description.

Preparations before encoding:

Before writing code, drag several controls in the storyboard, such:

After dragging these controls, remember to initialize tabBarController,

After completing the above steps, we will start to write code.

Because you want to display two uiviewcontrollers on The View, create two classes and associate them

After the association is complete, drag the required controls into the corresponding class.

Now you can start writing code (important ):

First, import the header file

# Import <CoreLocation/CoreLocation. h>: As mentioned in the previous section, if many classes are required for this header file, you can write a pch file. Here we will briefly describe the method:

After you finish the previous step, you will find that CoreLocation is still unavailable in the class. Don't worry, there is another step:

 

Code:

GeoCoding ):

1 // 2 // GeocodViewController. m 3 // geocode 4 // 5 // Created by admin on 16/5/21. 6 // Copyright©KXZDJ. all rights reserved. 7 // 8 9 # import "GeocodViewController. h "10 11 @ interface GeocodViewController () 12 @ property (weak, nonatomic) IBOutlet UITextField * addressFiled; 13 @ property (weak, nonatomic) IBOutlet UILabel * latitudeLabel; 14 @ property (weak, nonatomic) IBOutlet UILabel * lontitudeLabel; 15 @ property (weak, nonatomic) IBOutlet UITextView * detailTextView; 16 17 @ end18 19 @ implement Ation GeocodViewController20-(IBAction) geoCode :( id) sender {21 22 // create a geoCode object 23 CLGeocoder * geo = [[CLGeocoder alloc] init]; 24 25 // geocode 26 [geo geocodeAddressString: self. addressFiled. text completionHandler: ^ (NSArray <CLPlacemark *> * _ Nullable placemarks, NSError * _ Nullable error) {27 // determine whether placemarks has no value or error, print the error message 28 if (placemarks. count = 0 | error) {29 NSLog (@ "% @", error); 30 return; 31} 32 33 // obtain the landmark 34 CLPlacemark * cpl = [placemarks lastObject]; 35 36 // latitude 37 CLLocationDegrees latitude = cpl. location. coordinate. latitude; 38 39 // longitude 40 CLLocationDegrees longtitude = cpl. location. coordinate. longpolling; 41 42 // assign a value (note that the longitude and latitude are double type) 43 self. latitudeLabel. text = [NSString stringWithFormat: @ "% f", latitude]; 44 self. lontitudeLabel. text = [NSString stringWithFormat: @ "% f", longtitude]; 45 46 // define a temporary string The obtained address is 47 NSString * tempStr = @ ""; 48 49 // traverses placemarks and obtains the current Coordinate Position 50 for (CLPlacemark * cpl in placemarks) {51 // Why do I append a string here? Placemarks is an array with many important names (for example, there is Nanjing Road Pedestrian Street in Shanghai and chunxi Road Pedestrian Street in Chengdu. When users enter the Pedestrian Street, both of them will be displayed, if no value is appended, only one value is displayed.) 52 tempStr = [tempStr stringByAppendingString: [NSString stringWithFormat: @ "% @ \ n", cpl. name]; 53} 54 55 // value to self. detailTextView56 self. detailTextView. text = tempStr; 57 58 59}]; 60} 61 62-(void) viewDidLoad {63 [super viewDidLoad]; 64 65} 66 67 68 @ end

Run:

Now, you should understand the meaning of placemarks and the concatenated strings in the code.

ReverseGeoCoding ):

1 // 2 // ReverseCodeViewController. m 3 // geocode 4 // 5 // Created by admin on 16/5/21. 6 // Copyright©KXZDJ. all rights reserved. 7 // 8 9 # import "ReverseCodeViewController. h "10 11 12 @ interface ReverseCodeViewController () 13 @ property (weak, nonatomic) IBOutlet UITextField * latitudeLabel; 14 @ property (weak, nonatomic) IBOutlet UITextField * longtitudeLabel; 15 @ property (weak, nonatomic) IBOutlet UITextView * detailTextView; 16 17 @ end18 19 @ implementation ReverseCodeViewController20 21-(void) viewDidLoad {22 [super viewDidLoad]; 23 24} 25-(IBAction) reverseCode :( id) sender {26 // create geographic code 27 CLGeocoder * reverseCode = [[CLGeocoder alloc] init]; 28 29 // latitude 30 CLLocationDegrees latitude = [self. latitudeLabel. text doubleValue]; 31 32 // longitude 33 CLLocationDegrees longpolling = [self. longtitudeLabel. text doubleValue]; 34 35 // create location 36 CLLocation * local = [[CLLocation alloc] initWithLatitude: latitude longpolling: longpolling]; 37 38 // anti-geographic code 39 [reverseCode reverseGeocodeLocation: local completionHandler: ^ (NSArray <CLPlacemark *> * _ Nullable placemarks, NSError * _ Nullable error) {40 // determine whether placemarks has no value or an error, print the error message 41 if (placemarks. count = 0 | error) {42 NSLog (@ "geocode failed"); 43 return; 44} 45 46 NSLog (@ "% ld", placemarks. count); 47 48 // obtain the address 49 CLPlacemark * cpl = [placemarks lastObject]; 50 NSLog (@ "% @", cpl. name); 51 52 // assign the value to detailTextView53 self. detailTextView. text = cpl. name; 54}]; 55 56 57} 58 59 @ end

Run:

Pay attention to the following during anti-Geographic Encoding:

1. if the input latitude and longitude exist on the map, but the encoding fails, do not worry, it is because the system will automatically determine the country in which you are located, then you are only allowed to access the longitude and latitude of the country.

2. During Anti-geocoding, you may find that there is only one value in the array, which confirms the uniqueness of a specific latitude and longitude.

 

This is the end of geocode and anti-geocode. I hope this will help you. If there are any deficiencies or errors, please let me know and I will correct them as soon as possible. Thank you...

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.