MapView 地圖參考

#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è)置地圖可滾動

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è)置長按手勢

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];

// 添加點擊事件

[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];

//經(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對象,該對象代表地圖的顯示中心和顯示范圍? ? MKCoordinateRegion region = {center,span};? ? //設(shè)置當(dāng)前地圖的顯示中心和顯示范圍? ? [self.mapview setRegion:region animated:YES];? ? // 設(shè)置一個固定的錨點? ? MKPointAnnotation *annotation = [[MKPointAnnotation alloc]init];? ? // 設(shè)置顯示的標(biāo)題? ? annotation.title = @"一個新的地點";? ? annotation.subtitle? = @"我喜歡的地方";? ? // 添加位置? ? CLLocationCoordinate2D coordinate = {latitude,longitude};? ? annotation.coordinate = coordinate;? ? // 添加錨點到視圖上? ? [self.mapview addAnnotation:annotation];}// 設(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對象? ? 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];? ? ? ? ? ? // 獲取地址信息對應(yīng)的地址詳情? ? ? ? ? ? NSArray *addrArr = [placemark.addressDictionary objectForKey:@"FormattedAddressLines"];? ? ? ? ? ? NSMutableString *addreStr = [[NSMutableString alloc]init];? ? ? ? ? ? for (int i = 0; i)annotation

{

static NSString *annoID = @"FKAnno";

// 設(shè)置可重用的錨點控件

MKAnnotationView *annoView = [mapView dequeueReusableAnnotationViewWithIdentifier:annoID];

if (!annoView) {

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

}

// 為錨點添加圖片

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(@"您點擊了錨點信息");

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容