#import "ViewController.h"
#import "MapKit/MapKit.h"
@interface ViewController ()<MKMapViewDelgate>
{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];
// 設(shè)置屬性
// 設(shè)置標(biāo)準(zhǔn)地圖
self.mapview.mapType = MKMapTypeStandard;
// 設(shè)置地圖可滾動(dòng)
self.mapview.scrollEnabled = YES;
// 展示用戶當(dāng)前顯示位置
self.mapview.showsUserLocation = YES;
// 設(shè)置代理
self.mapview.delegate = self;
// 設(shè)置允許縮放
self.mapview.zoomEnabled = YES;
// 添加到視圖上
[self.view addSubview:_mapview];
// 設(shè)置地圖顯示的經(jīng)緯度為39.5427 經(jīng)度為116.2317
[self locateToLatitude:39.5427 longtitude:116.2317];
// 設(shè)置長(zhǎng)按手勢(shì)
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];
[self.mapview addGestureRecognizer:longPress];
// 創(chuàng)建緯度Label
UILabel *latitudeLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, 20, 40, 30)];
latitudeLabel.text = @"緯度";
// 添加到視圖上
[self.mapview addSubview:latitudeLabel];
// 創(chuàng)建緯度文本框
latitudeTF = [[UITextField alloc]initWithFrame:CGRectMake(45, 20, 100, 30)];
latitudeTF.text = @"23.12672";
latitudeTF.borderStyle = UITextBorderStyleRoundedRect;
// 數(shù)字鍵盤
latitudeTF.keyboardType = UIKeyboardTypeNumberPad;
// 添加到視圖上
[self.mapview addSubview:latitudeTF];
// 創(chuàng)建經(jīng)度Label
UILabel *longtitudeLabel = [[UILabel alloc]initWithFrame:CGRectMake(145, 20, 40, 30)];
longtitudeLabel.text = @"經(jīng)度";
// 添加到視圖上
[self.mapview addSubview:longtitudeLabel];
// 創(chuàng)建經(jīng)度文本框
longtitudeTF = [[UITextField alloc]initWithFrame:CGRectMake(185, 20, 100, 30)];
longtitudeTF.text = @"113.395";
longtitudeTF.borderStyle = UITextBorderStyleRoundedRect;
// 數(shù)字鍵盤
longtitudeTF.keyboardType = UIKeyboardTypeNumberPad;
// 添加到視圖上
[self.mapview addSubview:longtitudeTF];
// 創(chuàng)建按鈕
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame =CGRectMake(285, 20, 40, 30);
[btn setTitle:@"GO" forState:UIControlStateNormal];
// 添加點(diǎn)擊事件
[btn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
[self.mapview addSubview:btn];
CGFloat width = self.view.frame.size.width;
NSLog(@"%f",width);
}
// 前往另一個(gè)地圖界面
-(void)click
{
[latitudeTF resignFirstResponder];
[longtitudeTF resignFirstResponder];
//經(jīng)度
NSString *latitudeStr = latitudeTF.text;
//緯度
NSString *longtitudeStr = longtitudeTF.text;
//如果用戶輸入的經(jīng)度喘漏、緯度為空
if (latitudeStr != nil && latitudeStr.length > 0
&& longtitudeStr!= nil && longtitudeStr.length > 0)
{
//設(shè)置經(jīng)度腺兴、緯度
[self locateToLatitude:latitudeStr.floatValue longtitude:longtitudeStr.floatValue];
}
}
-(void)locateToLatitude:(CGFloat)latitude longtitude:(CGFloat)longitude{? ? //設(shè)置地圖中的的經(jīng)度、緯度? ? CLLocationCoordinate2D center = {latitude,longitude};? ? //設(shè)置地圖顯示的范圍? ? MKCoordinateSpan span;? ? //地圖顯示范圍越小阴汇,細(xì)節(jié)越清楚遭垛;? ? span.latitudeDelta = 0.01;? ? span.longitudeDelta = 0.01;? ? //創(chuàng)建MKCoordinateRegion對(duì)象尼桶,該對(duì)象代表地圖的顯示中心和顯示范圍? ? MKCoordinateRegion region = {center,span};? ? //設(shè)置當(dāng)前地圖的顯示中心和顯示范圍? ? [self.mapview setRegion:region animated:YES];? ? // 設(shè)置一個(gè)固定的錨點(diǎn)? ? MKPointAnnotation *annotation = [[MKPointAnnotation alloc]init];? ? // 設(shè)置顯示的標(biāo)題? ? annotation.title = @"一個(gè)新的地點(diǎn)";? ? annotation.subtitle? = @"我喜歡的地方";? ? // 添加位置? ? CLLocationCoordinate2D coordinate = {latitude,longitude};? ? annotation.coordinate = coordinate;? ? // 添加錨點(diǎn)到視圖上? ? [self.mapview addAnnotation:annotation];}// 設(shè)置長(zhǎng)按手勢(shì)的觸發(fā)方法-(void)longPress:(UILongPressGestureRecognizer *)press{? ? // 定義坐標(biāo)值? ? CGPoint cp = [press locationInView:self.mapview];? ? // 根據(jù)坐標(biāo)獲取經(jīng)緯度? ? CLLocationCoordinate2D coordinate = [self.mapview convertPoint:cp toCoordinateFromView:self.mapview];? ? // 將經(jīng)緯度包裝成CLLocation對(duì)象? ? CLLocation *location = [[CLLocation alloc]initWithLatitude:coordinate.latitude longitude:coordinate.longitude];? ? // 根據(jù)經(jīng)緯度反向解析地址? ? [self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray* _Nullable placemarks, NSError * _Nullable error) {? ? ? ? ? ? ? ? if (placemarks.count > 0) {? ? ? ? ? ? CLPlacemark *placemark = placemarks[0];? ? ? ? ? ? // 獲取地址信息對(duì)應(yīng)的地址詳情? ? ? ? ? ? NSArray *addrArr = [placemark.addressDictionary objectForKey:@"FormattedAddressLines"];? ? ? ? ? ? NSMutableString *addreStr = [[NSMutableString alloc]init];? ? ? ? ? ? for (int i = 0; i)annotation
{
static NSString *annoID = @"FKAnno";
// 設(shè)置可重用的錨點(diǎn)控件
MKAnnotationView *annoView = [mapView dequeueReusableAnnotationViewWithIdentifier:annoID];
if (!annoView) {
annoView = [[MKAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:annoID];
}
// 為錨點(diǎn)添加圖片
annoView.image = [UIImage imageNamed:@"pos.gif"];
// 設(shè)置該控件是否顯示氣泡
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(@"您點(diǎn)擊了錨點(diǎn)信息");
}