`
re_reference
  • 浏览: 233552 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

MapKit之在地图上加pin(转)

阅读更多
在MKMapView中加pin其实就是加入MKAnnonation, 可以加入服和MKAnnonation协议的pin,下面展示一下方法。

1.首先创建一个服和MKAnnonation协议的委托类
@interface AnnotationDelegate : NSObject <MKAnnotation> {  
    CLLocationCoordinate2D coordinate;  
}  
    
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;  
    
- (id) initWithCoordinate:(CLLocationCoordinate2D)coord;  
    
@end  
    
@implementation AnnotationDelegate  
    
@synthesize coordinate;  
    
- (id) initWithCoordinate:(CLLocationCoordinate2D)coord  
{  
    coordinate.latitude = coord.latitude;  
    coordinate.longitude = coord.longitude;  
    return self;  
}  
    
@end  

2. 实例化该委托对像,加入到MKMapView中。
view plaincopy to clipboardprint?
AnnotationDelegate * annotationDelegate = [[[AnnotationDelegate alloc] initWithCoordinate:coordinate] autorelease];  
[self._mapView addAnnotation:annotationDelegate];  

另一处介绍
#import <Foundation/Foundation.h>
#import <MapKit/MKAnnotation.h>
@interface DisplayMap : NSObject
<MKAnnotation>{
    CLLocationCoordinate2D coordinate;
    NSString *title;
    NSString *subtitle;
}
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
@end
 
 
#import "DisplayMap.h"
@implementation DisplayMap
@synthesize coordinate,title,subtitle;
-(void)dealloc{
    [title release];
    [super dealloc];
}
@end

修改viewDidLoad方法
- (void)viewDidLoad {
    [super viewDidLoad];
    //mapView.showsUserLocation=YES;
    self.mapView.delegate=self;
    CLLocationManager *locationManager = [[CLLocationManager alloc] init];//创建位置管理器
    locationManager.delegate=self;//设置代理
    locationManager.desiredAccuracy=kCLLocationAccuracyBest;//指定需要的精度级别
    locationManager.distanceFilter=1000.0f;//设置距离筛选器
    [locationManager startUpdatingLocation];//启动位置管理器
  
    MKCoordinateRegion theRegion = { {0.0, 0.0 }, { 0.0, 0.0 } };
    theRegion.center=[[locationManager location] coordinate];
    [locationManager release];
    [mapView setZoomEnabled:YES];
    [mapView setScrollEnabled:YES];
    theRegion.span.longitudeDelta = 0.01f;
    theRegion.span.latitudeDelta = 0.01f;
    [mapView setRegion:theRegion animated:YES];
    DisplayMap *ann = [[DisplayMap alloc] init];
    ann.title = @"欧陆经典";
    ann.subtitle = @"vsp";
    //地点名字
    ann.coordinate = theRegion.center;
    [mapView addAnnotation:ann];
}
- (MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation
{
    MKPinAnnotationView *pinView = nil;
    if(annotation != mapView.userLocation)
    {
        static NSString *defaultPinID = @"com.invasivecode.pin";
        pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
        if ( pinView == nil ) pinView = [[[MKPinAnnotationView alloc]
                                          initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];
        pinView.pinColor = MKPinAnnotationColorRed;
        pinView.canShowCallout = YES;
        pinView.animatesDrop = YES;
    }
    else {
        [mapView.userLocation setTitle:@"欧陆经典"];
    [mapView.userLocation setSubtitle:@"vsp"];
    }
    return pinView;
}

注意:无论是自定义的MKAnnotationView还是标准的,一旦addAnnotation to MapView,如何更新它在地图上的位置呢?更新MKAnnotation protocol中的coordinate可以做到吗? 如果是手动更新位置是不可以让它在地图上移动的。请看官网文档 ,其中有一段描述:
“Important: When you implement the coordinate property in your class, it is recommended that you synthesize its creation. If you choose to implement the methods for this property yourself, or if you manually modify the variable underlying that property in other parts of your class after the annotation has been added to the map, be sure to send out key-value observing (KVO) notifications when you do. Map Kit uses KVO notifications to detect changes to the coordinate, title, and subtitle properties of your annotations and make any needed changes to the map display. If you do not send out KVO notifications, the position of your annotations may not be updated properly on the map.For more information about how to implement KVO-compliant accessor methods, see Key-Value Observing Programming Guide.”


手动更新后必须用KVO的方式通知系统,不然系统是不知道更新位置的。如何通知道呢,其实NSObject中有这样的方法willChangeValueForKey and didChangeValueForKey。如你写了个方法更新pin位置,如下:
view sourceprint?-(void)UpdateCoord:(CLLocationCoordinate2D)newCoord   
{   
        [self willChangeValueForKey:@"coordinate"];   
        coordinate = newCoord;   
        [self didChangeValueForKey:@"coordinate"];   
}

IOS4中更新更简单,只需要调用- (void)setCoordinate:(CLLocationCoordinate2D)newCoordinate;这个方法就可以自动更新了。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics