地圖架構,地圖框

來源:互聯網
上載者:User

地圖架構,地圖框
使用蘋果內建的地圖架構,需要在項目中載入MapKit.framework(項目—TARGETS--Build Phases--Link Binary With Libraries),並在標頭檔中匯入#import<MapKit/MapKit.h>
一、MKMapView的常用屬性和方法NSArray *annotations;//儲存地圖中的圖釘MKCoordinateRegionregion; //顯示地區MKCoordinateRegion是個結構體,包括兩個屬性typedefstruct {
CLLocationCoordinate2D center;
MKCoordinateSpan span;
} MKCoordinateRegion;
CLLocationCoordinate2D//顯示經緯度MKCoordinateSpan//顯示精度
//設定顯示位置,顯示在螢幕中心點    CLLocationCoordinate2D coord;
    coord.longitude = 113.346196; //經度
    coord.latitude = 23.140563;//緯度
//地圖的顯示精度,數值越小地圖顯示越詳細
    MKCoordinateSpan span;
    span.longitudeDelta = 0.1;
    span.latitudeDelta = 0.1;
//設定顯示地區    [self.mapViewsetRegion:(MKCoordinateRegionMake(coord, span))];
//點擊螢幕上的位置,擷取經緯度
- (IBAction)tapAction:(UITapGestureRecognizer*)sender {
    CGPoint p = [senderlocationInView:self.view];
    //將螢幕上的點轉換為地圖座標
    CLLocationCoordinate2D coord = [self.mapViewconvertPoint:ptoCoordinateFromView:self.view];
    NSLog(@"coord.longitude:%lf coord.latitude:%lf", coord.longitude,coord.latitude);}
二、在地圖上設定圖釘在地圖上顯示圖釘,蘋果是通過遵守一個MapKit架構中MKAnnotation協議來實現的。1、建立一個繼承自NSObject的子類2、在子類中匯入 #import <MapKit/MapKit.h>3、讓子類遵守 MKAnnotation協議,實現協議中的屬性和方法 //必須實現
@property(nonatomic,readonly)CLLocationCoordinate2Dcoordinate;

//選擇實現
// Title and subtitle for use by selection UI.
@property(nonatomic,copy)NSString*title;
@property(nonatomic,copy)NSString*subtitle;
// Called as a result of dragging an annotation view.- (void)setCoordinate:(CLLocationCoordinate2D)newCoordinate;
4、在地圖中添加圖釘//建立一個圖釘XYZAnnotation*ann = [[XYZAnnotationalloc]init];//設定經緯度等屬性[annsetCoordinate:coord];ann.title=@"華師地鐵站";ann.subtitle=@"這裡是華師地鐵站";//往地圖上添加一個圖釘[self.mapViewaddAnnotation:ann];//擷取地圖中的圖釘NSArray*anns = [self.mapViewannotations];//移除地圖中所有的圖釘[self.mapViewremoveAnnotations:anns];
5、自訂圖釘視圖

  • 建立一個繼承自MKAnnotationView的自訂圖釘視圖
@interface XYZAnnotationView :MKAnnotationView

@end
#import"XYZAnnotationView.h"@implementation XYZAnnotationView

- (id)initWithAnnotation:(id<MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithAnnotation:annotationreuseIdentifier:reuseIdentifier];    if (self) {       UIImageView*imageView = [[UIImageViewalloc]initWithFrame:CGRectMake(-15, -30,30,30)]; //點擊螢幕放置圖釘,設定圖釘的frame.origin修正位移        imageView.image = [UIImageimageNamed:@"pink.png"];
        [self addSubview:imageView];
    }
    return self;}@end
  • 當前視圖控制器遵守 MKMapViewDelegate協議;
  • 設定mapView的委派物件為當前視圖控制器(self.mapView.delegate=self或者在Storyboard中拖拽);
  • 實現協議方法 
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
    static NSString *annotationViewId = @"annotationView";
   
    //從隊列中擷取一個annotationView
    XYZAnnotationView *annotationView = (XYZAnnotationView *)[self.mapViewdequeueReusableAnnotationViewWithIdentifier:annotationViewId];
   
    //如果隊列中沒有,則建立一個新的annotationView
    if (!annotationView) {
        annotationView = [[XYZAnnotationViewalloc]initWithAnnotation:annotationreuseIdentifier:annotationViewId];
    }
   
    return annotationView;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.