Methods of using Corelocation framework to process geocoding in IOS development _ios

Source: Internet
Author: User

First, Introduction

1. In the mobile internet age, mobile apps can solve many of the user's life trivia, such as

(1) Navigation: Go to any strange place

(2) Around: Find a restaurant, find a hotel, find a bank, find a movie theater

2. In the above applications, maps and positioning functions have been used, in the development of iOS, to join these 2 functions, must be based on 2 frameworks for development

(1) Map Kit: for maps display

(2) Core Location: for geographical positioning

3. Two hot technical terms

(1) Lbs:location Based Service (location-based services)

(2) Solomo:social local Mobile (Solomon)

Ii. use of the corelocation framework

1.CoreLocation Framework Usage Prerequisites

(1) Import frame

Description: After XCODE5, we no longer need to manually import

(2) Import the main header file

  

Copy Code code as follows:
#import <CoreLocation/CoreLocation.h>

2.CoreLocation Frame Use Notice

All data types in the corelocation frame are prefixed with CL

Use Cllocationmanager object in corelocation to do user positioning

Third, latitude and longitude of geographical information literacy

1. Schematic diagram

2. Meridian: Through the London Greenwenger Observatory

Go east (to the right), it's longitude (E)

Go West (to the left), it's longitude (W)

Things by the 180 °, a total of 360 °

3. Equator: Zero dimensional dimension

Go North (top), northern latitude (N)

Go south (below), it's latitude (S)

South latitude each 90 °, a total of 180 °

Tip: The greater the Latitude (1°≈111km) across the longitude, the greater the range, the smaller the things you see on the map.

4. Latitude and longitude of our country:

(1) China's latitude and longitude range

Latitude range: N 3°51′~ n 53°33′

Longitude range: E 73°33′~ e 135°05′

(2) The latitude and longitude of some cities

Four, simulation location

Description: When testing the program, set the simulation position of the mobile phone simulator (latitude and longitude degree)

Corelocation Geographic Code
One, simple explanation

Clgeocoder: Geo-encoder, where Geo is a shorthand for geography's English word geography.

1. Use Clgeocoder to complete "geocoding" and "anti-geo coding"

Geocoding: Obtaining specific location information (such as latitude and longitude, address name, etc.) according to a given place name.

Anti-geocoding: To obtain specific location information according to the given latitude and longitude degree

(1) Geo-coding method

Copy Code code as follows:

-(void) geocodeaddressstring: (NSString *) addressstring Completionhandler: (Clgeocodecompletionhandler) Completionhandler;

(2) Anti-geo coding method
Copy Code code as follows:

-(void) Reversegeocodelocation: (cllocation *) location Completionhandler: (Clgeocodecompletionhandler) Completionhandler;


2.CLGeocodeCompletionHandler

When the geo-geo-coding is complete, the Clgeocodecompletionhandler is invoked

This block passes 2 parameters

Error: Value when coding error (for example, no specific information is encoded)

Placemarks: There are clplacemark objects in it.

3.CLPlacemark

Description: Clplacemark literally means landmark, encapsulates detailed address location information

Geographical location @property (nonatomic, readonly) cllocation *location;

Regional @property (nonatomic, readonly) clregion *region;

Detailed address information @property (nonatomic, readonly) nsdictionary *addressdictionary;

Address name @property (nonatomic, readonly) NSString *name;

City @property (nonatomic, readonly) NSString *locality;

Second, the code example:

The interface in storyboard is as follows:

Implementation code:

Copy Code code as follows:

YYVIEWCONTROLLER.M file
//
Yyviewcontroller.m
19-Geo Code
//
Created by Apple on 14-8-11.
Copyright (c) 2014 Yangyong. All rights reserved.
//

#import "YYViewController.h"
#import <CoreLocation/CoreLocation.h>

@interface Yyviewcontroller ()
@property (Nonatomic,strong) Clgeocoder *geocoder;
#pragma mark-geo-coding
-(ibaction) GeoCode;
@property (Weak, nonatomic) Iboutlet Uitextfield *addressfield;
@property (Weak, nonatomic) Iboutlet Uilabel *longitudelabel;
@property (Weak, nonatomic) Iboutlet Uilabel *latitudelabel;
@property (Weak, nonatomic) Iboutlet Uilabel *detailaddresslabel;

#pragma mark-anti-geo coding

-(ibaction) Reversegeocode;
@property (Weak, nonatomic) Iboutlet Uitextfield *longitudefield;
@property (Weak, nonatomic) Iboutlet Uitextfield *latitudefield;
@property (Weak, nonatomic) Iboutlet Uilabel *reverdedetailaddresslabel;
@end


Copy Code code as follows:

@implementation Yyviewcontroller

#pragma mark-lazy Load
-(Clgeocoder *) Geocoder
{
if (_geocoder==nil) {
_geocoder=[[clgeocoder Alloc]init];
}
return _geocoder;
}
-(void) viewdidload
{
[Super Viewdidload];
}
/**
* Geo-coding: Geographical names-> latitude and longitude coordinates
*/
-(ibaction) GeoCode {
1. Get the address you entered
NSString *address=self.addressfield.text;
if (address.length==0) return;

2. Start geo-coding
Description: Call the following method to begin coding, whether the encoding is successful or failed, the method in the block is invoked
[Self.geocoder geocodeaddressstring:address completionhandler:^ (Nsarray *placemarks, NSError *error) {
If there is an error message, or the number of name elements obtained in the array is 0, then the description is not found
if (Error | | placemarks.count==0) {
self.detailaddresslabel.text=@ "You have entered the address did not find, may be on the moon";
}else//coding succeeded, found specific location information
{
Print and view all location information found
/*
Name: Names
Locality: City
Country: National
PostalCode: Postal Code
*/
For (Clplacemark *placemark in Placemarks) {
NSLog (@ "name=%@ locality=%@ country=%@ postalcode=%@", Placemark.name,placemark.locality,placemark.country, Placemark.postalcode);
}

The first display in the array of retrieved geographic information is displayed on the interface
Clplacemark *firstplacemark=[placemarks Firstobject];
Detailed Address name
Self.detailaddresslabel.text=firstplacemark.name;
Latitude
Cllocationdegrees Latitude=firstplacemark.location.coordinate.latitude;
Longitude
Cllocationdegrees Longitude=firstplacemark.location.coordinate.longitude;
Self.latitudelabel.text=[nsstring stringwithformat:@ "%.2f", Latitude];
Self.longitudelabel.text=[nsstring stringwithformat:@ "%.2f", longitude];
}
}];
}

/**
* Anti-geo-coding:-> geographical Names by latitude and coordinates
*/
-(ibaction) Reversegeocode {
1. Get the latitude and longitude of the input
NSString *longtitudetext=self.longitudefield.text;
NSString *latitudetext=self.latitudefield.text;
if (longtitudetext.length==0| | latitudetext.length==0) return;

Cllocationdegrees Latitude=[latitudetext Doublevalue];
Cllocationdegrees Longitude=[longtitudetext Doublevalue];

Cllocation *location=[[cllocation Alloc]initwithlatitude:latitude Longitude:longitude];
2. Anti-geo coding
[Self.geocoder reversegeocodelocation:location completionhandler:^ (Nsarray *placemarks, NSError *error) {
if (error| | placemarks.count==0) {
self.reverdedetailaddresslabel.text=@ "You have entered the address did not find, may be on the moon";
}else//Encoding Succeeded
{
Show the first landmark information
Clplacemark *firstplacemark=[placemarks Firstobject];
Self.reverdedetailaddresslabel.text=firstplacemark.name;
Warp latitude
Cllocationdegrees Latitude=firstplacemark.location.coordinate.latitude;
Cllocationdegrees Longitude=firstplacemark.location.coordinate.longitude;
Self.latitudefield.text=[nsstring stringwithformat:@ "%.2f", Latitude];
Self.longitudefield.text=[nsstring stringwithformat:@ "%.2f", longitude];
}
}];
}

-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) event
{
[Self.view Endediting:yes];
}
@end


Implementation effect:

(1) geocoding: (place name-> latitude and longitude coordinates)

Print output:

(2) Anti-geo code: (Latitude and longitude-> place name)

(3) Note: Adjust the keyboard

Click the latitude and longitude TextField for input, the pop-up keyboard is as follows

(4) Note: All the results of the search are in China, because the Apple Map service provider in China is a gold map.

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.