歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> iOS地圖的注釋(Annotation)

iOS地圖的注釋(Annotation)

日期:2017/3/1 10:34:10   编辑:Linux編程

1. 添加到map view的子視圖不會隨地圖的移動而移動,map view會固定其子視圖的位置。如果要添加隨著地圖移動的子視圖,可以使用annotations和overlays。annotation用來顯示由一個經緯度定義的位置,而overlay則是由多個點所定義或者包含了許多連續的圖形。


2.在地圖上顯示annotation,需要提供兩個對象

  • annotation object)
  • annotation view.)

注釋對象通常是一些小的數據對象,保存了地圖的坐標和一些相關信息。

Map Kit提供了一些標准的注釋視圖,你也可以使用自定義的注釋視圖。但是不能將注釋視圖直接添加到map view,而是使用map view的代理對象來提供。


3.添加注釋的具體步驟

  1. 定義一個注釋對象annotation object :
    • 使用MKPointAnnotation類來實現一個簡單的注釋,這類注釋可以顯示標題和副標題。
    • 自定義一個遵守MKAnnotation協議的對象,這類注釋可以存儲任何類型數據
  2. 定義一個注釋視圖annotation view來顯示數據:
    • 如果注釋可以由一張靜態圖片表示,則創建一個MKAnnotationView類的實例,然後將圖像賦值給image屬性
    • 如果你想使用標准的pin annotation,創建一個MKPinAnnotationView類的實例
    • 如果靜態圖像不夠表示你的注釋,那麼創建一個MKAnnotationView的子類
  3. Implement the mapView:viewForAnnotation: method in your map view delegate. Your implementation of this method should dequeue an existing annotation view if one exists or create a new one. If your application supports multiple types of annotations, you must include logic in this method to create a view of the appropriate type for the provided annotation object. 在map view的代理對象中實現mapView:viewForAnnotation:方法,使用已經存在的注釋視圖或者新創建一個。如果你的程序支持多種不同的注釋,那麼應該根據不同的注釋提供不同的視圖
  4. 方法添加你的注釋對象到map view


4.自定義注釋對象

注釋對象遵守MKAnnotation協議,如果你之想要將一個標題和一個坐標相聯系,那麼可以直接使用MKPointAnnotation作為注釋對象。如果還想要顯示其他信息,就需要自定義一個注釋對象

[plain]
  1. <span >Listing 5-1 Creating a simple annotation object
  2. @@interface MyCustomAnnotation : NSObject <MKAnnotation> {
  3. CLLocationCoordinate2D coordinate;
  4. }
  5. @property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
  6. - (id)initWithLocation:(CLLocationCoordinate2D)coord;
  7. // Other methods and properties.
  8. @end </span>

[plain]
  1. <span >@implementation MyCustomAnnotation
  2. @synthesize coordinate;
  3. - (id)initWithLocation:(CLLocationCoordinate2D)coord {
  4. self = [super init];
  5. if (self) {
  6. coordinate = coord;
  7. }
  8. return self;
  9. }
  10. @end </span>

5.使用標准注釋視圖

MKAnnotationView類定義了注釋視圖的一些基本特性。MKPinAnnotationView類是MKAnnotationView的子類,用來顯示系統標准的注釋視圖(pin view)

創建一個MKAnnotationView的實例,設置image屬性。當此annotation顯示在地圖上時,該圖像顯示在相應的坐標位置( If you do not want the image to be centered on the map coordinate, you can use the centerOffset property to move the center point horizontally and vertically in any direction. )。

[plain]
  1. <span >MKAnnotationView* aView = [[[MKAnnotationView alloc] initWithAnnotation:annotation
  2. reuseIdentifier:@"MyCustomAnnotation"] autorelease];
  3. aView.image = [UIImage imageNamed:@"myimage.png"];
  4. aView.centerOffset = CGPointMake(10, -20); </span>
在代理的mapView:viewForAnnotation:方法中創建標准注釋視圖


6.使用自定義注釋視圖

[plain]
  1. <span >#import <UIKit/UIKit.h>
  2. #import <MapKit/MapKit.h>
  3. @interface MyCustomAnnotationView : MKAnnotationView
  4. {
  5. // Custom data members
  6. }
  7. // Custom properties and methods.
  8. @end </span>

[plain]
  1. <span >Listing 5-5 Initializing a custom annotation view
  2. - (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier
  3. {
  4. self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
  5. if (self)
  6. {
  7. // Set the frame size to the appropriate values.
  8. CGRect myFrame = self.frame;
  9. myFrame.size.width = 40;
  10. myFrame.size.height = 40;
  11. self.frame = myFrame;
  12. // The opaque property is YES by default. Setting it to
  13. // NO allows map content to show through any unrendered
  14. // parts of your view.
  15. self.opaque = NO;
  16. }
  17. return self;
  18. } </span>

6.在代理對象中創建注釋視圖


[plain]
  1. <span ><span >Listing 5-6 Creating annotation views
  2. - (MKAnnotationView *)mapView:(MKMapView *)mapView
  3. viewForAnnotation:(id <MKAnnotation>)annotation
  4. {
  5. // If it's the user location, just return nil.
  6. if ([annotation isKindOfClass:[MKUserLocation class]])
  7. return nil;
  8. // Handle any custom annotations.
  9. if ([annotation isKindOfClass:[MyCustomAnnotation class]])
  10. {
  11. // Try to dequeue an existing pin view first.
  12. MKPinAnnotationView* pinView = (MKPinAnnotationView*)[mapView
  13. dequeueReusableAnnotationViewWithIdentifier:@"CustomPinAnnotationView"];
  14. if (!pinView)
  15. {
  16. // If an existing pin view was not available, create one.
  17. pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation
  18. reuseIdentifier:@"CustomPinAnnotation"]
  19. autorelease];
  20. pinView.pinColor = MKPinAnnotationColorRed;
  21. pinView.animatesDrop = YES;
  22. pinView.canShowCallout = YES;
  23. // Add a detail disclosure button to the callout.
  24. UIButton* rightButton = [UIButton buttonWithType:
  25. UIButtonTypeDetailDisclosure];
  26. [rightButton addTarget:self action:@selector(myShowDetailsMethod:)
  27. forControlEvents:UIControlEventTouchUpInside];
  28. pinView.rightCalloutAccessoryView = rightButton;
  29. }
  30. else
  31. pinView.annotation = annotation;
  32. return pinView;
  33. }
  34. return nil;
  35. } </span></span>

當map view需要顯示注釋時候,會調用代理的mapView:viewForAnnotation:方法,如果你不實現此方法或者總是返回nil,map view會使用默認的注釋視圖,即pin annotation view。在你創建新的視圖之前,檢查是否已經存在此類視圖dequeueReusableAnnotationViewWithIdentifier: ,類似於tableview的cell實現


7.管理地圖的注釋對象

為了避免注釋視圖的重疊,在方法mapView:regionWillChangeAnimated: andmapView:regionDidChangeAnimated: 中,根據需要來添加或者減少


8.讓注釋視圖可拖動

  • 在注釋對象中,實現setCoordinate:方法來更新注釋的坐標。
  • 創建注釋視圖時,設置draggable屬性為YES
Copyright © Linux教程網 All Rights Reserved