地圖解析錨點RwbMapView

#import "ViewController.h"

#import@interface ViewController ()

{? ??

UITextField *latitudeTF;? ??

UITextField *longtitudeTF;

}

@property(nonatomic,strong)MKMapView *mapview;

@property(nonatomic,strong)CLGeocoder *geocoder;

@end@implementation ViewController

- (void)viewDidLoad?

{? ??

[super viewDidLoad];? ??

// 初始化? ?

?self.geocoder = [[CLGeocoder alloc]init];? ??

// 初始化地圖? ?

?self.mapview = [[MKMapView alloc]initWithFrame:self.view.frame];? ??

// 設置屬性? ?

?// 設置標準地圖? ??

self.mapview.mapType = MKMapTypeStandard;? ?

?// 設置地圖可滾動??

? self.mapview.scrollEnabled = YES;? ?

?// 展示用戶當前顯示位置? ?

?self.mapview.showsUserLocation = YES;? ?

?// 設置代理? ??

self.mapview.delegate = self;? ??

// 設置允許縮放? ??

self.mapview.zoomEnabled = YES;? ?

?// 添加到視圖上? ?

?[self.view addSubview:_mapview];? ?

?// 設置地圖顯示的經緯度為39.5427 經度為116.2317? ?

?[self locateToLatitude:39.5427 longtitude:116.2317];? ?

?// 設置長按手勢? ??

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];? ?

?[self.mapview addGestureRecognizer:longPress];? ? ? ??

// 創建緯度

Label? ? UILabel *latitudeLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, 20, 40, 30)];? ??

latitudeLabel.text = @"緯度";? ?

?// 添加到視圖上? ??

[self.mapview addSubview:latitudeLabel];? ??

// 創建緯度文本框? ??

latitudeTF = [[UITextField alloc]initWithFrame:CGRectMake(45, 20, 100, 30)];? ??

latitudeTF.text = @"23.12672";? ??

latitudeTF.borderStyle = UITextBorderStyleRoundedRect;? ??

// 數字鍵盤? ??

latitudeTF.keyboardType = UIKeyboardTypeNumberPad;? ??

// 添加到視圖上? ??

[self.mapview addSubview:latitudeTF];? ??

// 創建經度Label? ??

UILabel *longtitudeLabel = [[UILabel alloc]initWithFrame:CGRectMake(145, 20, 40, 30)];? ?

?longtitudeLabel.text = @"經度";? ??

// 添加到視圖上? ?

?[self.mapview addSubview:longtitudeLabel];? ??

// 創建經度文本框? ??

longtitudeTF = [[UITextField alloc]initWithFrame:CGRectMake(185, 20, 100, 30)];? ?

?longtitudeTF.text = @"113.395";? ??

longtitudeTF.borderStyle = UITextBorderStyleRoundedRect;? ?

?// 數字鍵盤? ?

?longtitudeTF.keyboardType = UIKeyboardTypeNumberPad;? ??

// 添加到視圖上? ??

[self.mapview addSubview:longtitudeTF];? ? ? ?

?// 創建按鈕? ?

?UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];? ??

btn.frame =CGRectMake(285, 20, 40, 30);? ?

?[btn setTitle:@"GO" forState:UIControlStateNormal];? ??

// 添加點擊事件? ?

?[btn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];? ?

?[self.mapview addSubview:btn];? ? ? ??

CGFloat width = self.view.frame.size.width;? ??

NSLog(@"%f",width);

}

// 前往另一個地圖界面

-(void)click{? ??

[latitudeTF resignFirstResponder];? ??

[longtitudeTF resignFirstResponder];? ??

//經度? ?

?NSString *latitudeStr = latitudeTF.text;? ??

//緯度? ?

?NSString *longtitudeStr = longtitudeTF.text;? ??

//如果用戶輸入的經度、緯度為空? ?

?if (latitudeStr != nil && latitudeStr.length > 0? ? ? ? && longtitudeStr!= nil && longtitudeStr.length > 0)? ?

?{? ? ? ??

//設置經度、緯度? ? ? ?

?[self locateToLatitude:latitudeStr.floatValue longtitude:longtitudeStr.floatValue];? ??

}

}

-(void)locateToLatitude:(CGFloat)latitude longtitude:(CGFloat)longitude

{? ?

?//設置地圖中的的經度、緯度? ?

?CLLocationCoordinate2D center = {latitude,longitude};? ?

?//設置地圖顯示的范圍? ?

?MKCoordinateSpan span;? ?

?//地圖顯示范圍越小,細節越清楚;? ?

?span.latitudeDelta = 0.01;? ??

span.longitudeDelta = 0.01;? ??

//創建MKCoordinateRegion對象,該對象代表地圖的顯示中心和顯示范圍? ?

?MKCoordinateRegion region = {center,span};? ??

//設置當前地圖的顯示中心和顯示范圍? ?

?[self.mapview setRegion:region animated:YES];? ?

?// 設置一個固定的錨點? ?

?MKPointAnnotation *annotation = [[MKPointAnnotation alloc]init];? ?

?// 設置顯示的標題? ??

annotation.title = @"一個新的地點";??

? annotation.subtitle? = @"我喜歡的地方";? ?

?// 添加位置? ?

?CLLocationCoordinate2D coordinate = {latitude,longitude};?

?? annotation.coordinate = coordinate;? ?

?// 添加錨點到視圖上??

? [self.mapview addAnnotation:annotation];

}

// 設置長按手勢的觸發方法

-(void)longPress:(UILongPressGestureRecognizer *)press

{? ??

// 定義坐標值? ??

CGPoint cp = [press locationInView:self.mapview];? ?

?// 根據坐標獲取經緯度? ??

CLLocationCoordinate2D coordinate = [self.mapview convertPoint:cp toCoordinateFromView:self.mapview];

? ? // 將經緯度包裝成CLLocation對象? ??

CLLocation *location = [[CLLocation alloc]initWithLatitude:coordinate.latitude longitude:coordinate.longitude];? ??

// 根據經緯度反向解析地址? ?

?[self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray* _Nullable placemarks, NSError * _Nullable error)?

{? ? ? ? ? ? ??

? if (placemarks.count > 0)

?{? ? ? ? ? ??

CLPlacemark *placemark = placemarks[0];? ? ? ? ? ??

// 獲取地址信息對應的地址詳情? ? ? ? ? ??

NSArray *addrArr = [placemark.addressDictionary objectForKey:@"FormattedAddressLines"];? ? ? ? ? ?

?NSMutableString *addreStr = [[NSMutableString alloc]init];? ? ? ? ? ?

?for (int i = 0; i)annotation

{

static NSString *annoID = @"FKAnno";

// 設置可重用的錨點控件

MKAnnotationView *annoView = [mapView dequeueReusableAnnotationViewWithIdentifier:annoID];

if (!annoView) {

annoView = [[MKAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:annoID];

}

// 為錨點添加圖片

annoView.image = [UIImage imageNamed:@"pos.gif"];

// 設置該控件是否顯示氣泡

annoView.canShowCallout = YES;

UIButton *btn = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

[btn addTarget:self action:@selector(goclick) forControlEvents:UIControlEventTouchUpInside];

annoView.rightCalloutAccessoryView = btn;

// 返回annoView

return annoView;

}

-(void)goclick

{

NSLog(@"您點擊了錨點信息");

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容