標籤:
1,在VC中添加地圖介面MKMapView(全屏)
2、選工程介面相對應的介面中倒數第二行,Build Phases選項中 選擇第三個 的+號,彈出來搜尋方塊輸入mapk,選擇搜尋結果確認添加,此時地圖能顯示出來。 (3.在SB中的地圖右側選項有三個模式,(二維線路,衛星無線路,衛星有線路)) 4、關聯代碼在vc中關聯myMV。但此時並不被系統識別,此時要添加import<MapKit/MapKit.h>協議,此時就被識別了 5、此時要從網路擷取到經緯度,比如天安門經緯度, 此時要用到方法——建立經緯度的結構體:
// 北緯:39.90960456049752
// 東經:116.3972282409668
***這是經緯度的結構體**
CLLocationCoordinate2D coord = CLLocationCoordinate2DMake(39.90960456049752, 116.3972282409668);
***這是表示縮放層級***
MKCoordinateSpan span = MKCoordinateSpanMake(.001, .001);
並且此時要建立一個結構體把經緯度和縮放層級裝載一起,讓地圖顯示到制定的位置:
[self.myMV setRegion:MKCoordinateRegionMake(coord, span)];
6、在地圖中添加圖釘:
需要建立一個自訂的類,繼承NSObject取名MyAnnotation。此時要讓此類在.h中import<MapKit/MapKit.h>,並且實現一個協議:
@interface MyAnnotation : NSObject<MKAnnotation>————但是這個協議有些限制,要給其添加屬性,進入協議,其中有一個必須和兩個可選屬性,把其三個都複製到。h中:
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
@property (nonatomic, readonly, copy) NSString *title;
@property (nonatomic, readonly, copy) NSString *subtitle;
readonly是唯讀意思,不能編輯,修改,所以去掉,所以:
@property (nonatomic) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
7、此時在VC中import myAnnotation,
在VDL中添加圖釘:
MyAnnotation *ann = [[MyAnnotation alloc]init];——添加圖釘的對象
ann.coordinate = coord;——這個是圖釘的經緯度
ann.title = @"天安門”;——這是圖釘的標題內容
ann.subtitle = @"我愛北京天安門!”;————備忘內容
[self.myMV addAnnotation:ann];
————————————————————————————————————————————————————
8、添加自訂圖釘:
建立一個類繼承於MKAnnotationView(前提是要在sb中拖入MKMapView)
取名為MKAnnotationView
並且在sb中連線delegate線
9、在vc中添加協議
@interface ViewController ()<MKMapViewDelegate>
進入此協議,
複製出來
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{
——————複用效果
引入import”MKAnnotationView“
在方法中寫入
MyAnnotationView *av = (MyAnnotationView *)這個()中是個此類型的內容[mapView dequeueReusableAnnotationViewWithIdentifier:@"ann"];
if (!av) {
av = [[MyAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"ann"];
}
return av;
}
10.在MyAnnotationView.m中初始化
- (instancetype)initWithAnnotation:(id<MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
if (self) {
//修改圖釘的樣式
self.image = [UIImage imageNamed:@"index_map.png"];
return self;
}
此時顯示完畢。
————————————————————————
注意:在此初始化方法中可以添加任意物,
11、此時還可以在其他座標上添加其他的圖釘
首先擷取經緯度,
然後在vc中添加ann2
MyAnnotation *ann2 = [[MyAnnotation alloc]init];
ann2.coordinate = CLLocationCoordinate2DMake(39.8068719483, 116.4608274052);
[self.myMV addAnnotation:ann2];
12、當圖釘被點擊出現的事件:
進入協議中,找到方法:當顯示地區發生改變時。
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated{
MyAnnotation *ann2 = [[MyAnnotation alloc]init];
ann2.coordinate = mapView.centerCoordinate;(螢幕顯示中央)
[self.myMV addAnnotation:ann2];
}
此時每移動一次 中央添加一個圖釘
13、再進入協議中找到方法:點擊選中時
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view{
NSLog(@"點擊了圖釘");
}
1,在VC中添加地圖介面MKMapView(全屏)
2、選工程介面相對應的介面中倒數第二行,Build Phases選項中 選擇第三個 的+號,彈出來搜尋方塊輸入mapk,選擇搜尋結果確認添加,此時地圖能顯示出來。 (3.在SB中的地圖右側選項有三個模式,(二維線路,衛星無線路,衛星有線路)) 4、關聯代碼在vc中關聯myMV。但此時並不被系統識別,此時要添加import<MapKit/MapKit.h>協議,此時就被識別了 5、此時要從網路擷取到經緯度,比如天安門經緯度, 此時要用到方法——建立經緯度的結構體:
// 北緯:39.90960456049752
// 東經:116.3972282409668
***這是經緯度的結構體**
CLLocationCoordinate2D coord = CLLocationCoordinate2DMake(39.90960456049752, 116.3972282409668);
***這是表示縮放層級***
MKCoordinateSpan span = MKCoordinateSpanMake(.001, .001);
並且此時要建立一個結構體把經緯度和縮放層級裝載一起,讓地圖顯示到制定的位置:
[self.myMV setRegion:MKCoordinateRegionMake(coord, span)];
6、在地圖中添加圖釘:
需要建立一個自訂的類,繼承NSObject取名MyAnnotation。此時要讓此類在.h中import<MapKit/MapKit.h>,並且實現一個協議:
@interface MyAnnotation : NSObject<MKAnnotation>————但是這個協議有些限制,要給其添加屬性,進入協議,其中有一個必須和兩個可選屬性,把其三個都複製到。h中:
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
@property (nonatomic, readonly, copy) NSString *title;
@property (nonatomic, readonly, copy) NSString *subtitle;
readonly是唯讀意思,不能編輯,修改,所以去掉,所以:
@property (nonatomic) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
7、此時在VC中import myAnnotation,
在VDL中添加圖釘:
MyAnnotation *ann = [[MyAnnotation alloc]init];——添加圖釘的對象
ann.coordinate = coord;——這個是圖釘的經緯度
ann.title = @"天安門”;——這是圖釘的標題內容
ann.subtitle = @"我愛北京天安門!”;————備忘內容
[self.myMV addAnnotation:ann];
————————————————————————————————————————————————————
8、添加自訂圖釘:
建立一個類繼承於MKAnnotationView(前提是要在sb中拖入MKMapView)
取名為MKAnnotationView
並且在sb中連線delegate線
9、在vc中添加協議
@interface ViewController ()<MKMapViewDelegate>
進入此協議,
複製出來
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{
——————複用效果
引入import”MKAnnotationView“
在方法中寫入
MyAnnotationView *av = (MyAnnotationView *)這個()中是個此類型的內容[mapView dequeueReusableAnnotationViewWithIdentifier:@"ann"];
if (!av) {
av = [[MyAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"ann"];
}
return av;
}
10.在MyAnnotationView.m中初始化
- (instancetype)initWithAnnotation:(id<MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
if (self) {
//修改圖釘的樣式
self.image = [UIImage imageNamed:@"index_map.png"];
return self;
}
此時顯示完畢。
————————————————————————
注意:在此初始化方法中可以添加任意物,
11、此時還可以在其他座標上添加其他的圖釘
首先擷取經緯度,
然後在vc中添加ann2
MyAnnotation *ann2 = [[MyAnnotation alloc]init];
ann2.coordinate = CLLocationCoordinate2DMake(39.8068719483, 116.4608274052);
[self.myMV addAnnotation:ann2];
12、當圖釘被點擊出現的事件:
進入協議中,找到方法:當顯示地區發生改變時。
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated{
MyAnnotation *ann2 = [[MyAnnotation alloc]init];
ann2.coordinate = mapView.centerCoordinate;(螢幕顯示中央)
[self.myMV addAnnotation:ann2];
}
此時每移動一次 中央添加一個圖釘
13、再進入協議中找到方法:點擊選中時
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view{
NSLog(@"點擊了圖釘");
}
2015 IOS 地圖——在藍懿教育 學習筆記