原生地圖及自定義大頭針

地圖設(shè)置分為三個(gè)步驟:

  1. 引入#import <MapKit/MapKit.h>,遵守代理MKMapViewDelegate,創(chuàng)建地圖視圖 MKMapView
  2. 設(shè)置經(jīng)緯度 CLLocationCoordinate2D
  3. 設(shè)置跨度 MKCoordinateSpan
  4. 設(shè)置地圖顯示區(qū)域 MKCoordinateRegion
  5. 添加大頭針 MKPointAnnotation

代碼如下:

  _mapView = [[MKMapView alloc] initWithFrame:CGRectMake(18, 10, WIDTH-36, 230)];
    _mapView.mapType = MKMapTypeStandard;
    _mapView.delegate = self;
    [self.view addSubview:_mapView];//這里是個(gè)坑,要先添加視圖才能進(jìn)行下面的步驟
    
    //經(jīng)緯度
    CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(_latitude, _longitude);
    //跨度
    MKCoordinateSpan span = MKCoordinateSpanMake(0.01, 0.01);
    //地圖顯示區(qū)域
    MKCoordinateRegion region = MKCoordinateRegionMake(coordinate,span);
    [_mapView setRegion:region animated:YES];
    //大頭針
    MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
    annotation.coordinate = coordinate;
    [_mapView addAnnotation:annotation];

實(shí)現(xiàn)代理方法

#pragma mark - 地圖代理
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{

    MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"annotationView"];
    
    if (annotationView == nil) {
        annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"annotationView"];
    }
    
    annotationView.image = [UIImage imageNamed:@"dingwei_ditu"];
    
    return annotationView;
}

有關(guān)更高級(jí)自定義大頭針和導(dǎo)航線的實(shí)現(xiàn),移步:http://www.itnose.net/detail/6201227.html

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容