CLGeocoder基本使用

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   使用   io   strong   ar   

////  HMViewController.m//  02-地理編碼////  Created by apple on 14-8-7.//  Copyright (c) 2014年 heima. All rights reserved.//#import "HMViewController.h"#import <CoreLocation/CoreLocation.h>@interface HMViewController ()@property (nonatomic, strong) CLGeocoder *geocoder;#pragma mark - 地理編碼- (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 - 反地理編碼- (IBAction)reverseGeocode;@property (weak, nonatomic) IBOutlet UITextField *longtitudeField;@property (weak, nonatomic) IBOutlet UITextField *latitudeField;@property (weak, nonatomic) IBOutlet UILabel *reverseDetailAddressLabel;@end@implementation HMViewController- (CLGeocoder *)geocoder{    if (!_geocoder) {        self.geocoder = [[CLGeocoder alloc] init];    }    return _geocoder;} - (void)viewDidLoad{    [super viewDidLoad];    }/** *  地理編碼:地名 -> 經緯度 */- (void)geocode{    // 1.獲得輸入的地址xx    NSString *address = self.addressField.text;    if (address.length == 0) return;        // 2.開始編碼    [self.geocoder geocodeAddressString:address completionHandler:^(NSArray *placemarks, NSError *error) {        if (error || placemarks.count == 0) {            self.detailAddressLabel.text = @"你輸入的地址找不到,可能在火星上";        } else { // 編碼成功(找到了具體的位置資訊)            // 輸出查詢到的所有地標資訊            for (CLPlacemark *placemark in placemarks) {                // 名字, 城市,國家,郵遞區號                NSLog(@"name=%@ locality=%@ country=%@ postalCode=%@", placemark.name, placemark.locality, placemark.country, placemark.postalCode);            }                        // 顯示最前面的地標資訊            CLPlacemark *firstPlacemark = [placemarks firstObject];            self.detailAddressLabel.text = firstPlacemark.name;            // 緯度            CLLocationDegrees latitude = firstPlacemark.location.coordinate.latitude;            // 經度            CLLocationDegrees longitude = firstPlacemark.location.coordinate.longitude;            self.latitudeLabel.text = [NSString stringWithFormat:@"%.2f", latitude];            self.longitudeLabel.text = [NSString stringWithFormat:@"%.2f", longitude];        }    }];}/** *  反地理編碼:經緯度 -> 地名 */- (void)reverseGeocode{    NSString *longtitudeText = self.longtitudeField.text;    NSString *latitudeText = self.latitudeField.text;    if (longtitudeText.length == 0 || latitudeText.length == 0) return;        CLLocationDegrees latitude = [latitudeText doubleValue];    CLLocationDegrees longtitude = [longtitudeText doubleValue];        // 開始反向編碼    CLLocation *location = [[CLLocation alloc] initWithLatitude:latitude longitude:longtitude];    [self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {        if (error || placemarks.count == 0) {            self.reverseDetailAddressLabel.text = @"你輸入的經緯度找不到,可能在火星上";        } else { // 編碼成功(找到了具體的位置資訊)            // 輸出查詢到的所有地標資訊            for (CLPlacemark *placemark in placemarks) {                NSLog(@"name=%@ locality=%@ country=%@ postalCode=%@", placemark.name, placemark.locality, placemark.country, placemark.postalCode);            }                        // 顯示最前面的地標資訊            CLPlacemark *firstPlacemark = [placemarks firstObject];            self.reverseDetailAddressLabel.text = firstPlacemark.name;                        CLLocationDegrees latitude = firstPlacemark.location.coordinate.latitude;            CLLocationDegrees longitude = firstPlacemark.location.coordinate.longitude;            self.latitudeField.text = [NSString stringWithFormat:@"%.2f", latitude];            self.longtitudeField.text = [NSString stringWithFormat:@"%.2f", longitude];        }    }];}- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{    [self.view endEditing:YES];}@end

 

CLGeocoder基本使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.