【问题标题】:Custom curent location Annotation pin not updating with core location自定义当前位置注释引脚未随核心位置更新
【发布时间】:2011-03-03 03:08:02
【问题描述】:

尝试为当前位置添加自定义图钉,但位置未更新。即使设置了setShowsUserLocation = YES;

- (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier {

    self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
    if ([[annotation title] isEqualToString:@"Current Location"]) {
        self.image = [UIImage imageNamed:[NSString stringWithFormat:@"cursor_%i.png", [[Session instance].current_option cursorValue]+1]];
    }

但是,如果我设置为 return nil; 一切正常,但我会丢失自定义图像。我真的很想让这个工作。任何帮助将不胜感激。

【问题讨论】:

    标签: objective-c mapkit core-location mkannotation mkannotationview


    【解决方案1】:

    如您所见,setShowsUserLocation 标志仅使用默认的蓝色气泡显示当前位置。

    您需要在此处执行的操作是从手机监听位置更新并自己手动重新定位您的注释。您可以通过创建 CLLocationManager 实例并在位置管理器通知其代表更新时删除和替换您的注释来做到这一点:

    - (void)locationManager:(CLLocationManager *)manager
        didUpdateToLocation:(CLLocation *)newLocation
               fromLocation:(CLLocation *)oldLocation
    {
      // update annotation position here
    }
    

    为了重新定位坐标,我有一个符合 MKAnnotation 协议的 Placemark 类:

    //--- .h ---
    
    #import <Foundation/Foundation.h>
    #import <MapKit/MapKit.h>
    
    @interface Placemark : NSObject <MKAnnotation> {
    }
    
    @property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
    @property (nonatomic, retain) NSString *strSubtitle;
    @property (nonatomic, retain) NSString *strTitle;
    
    -(id)initWithCoordinate:(CLLocationCoordinate2D) coordinate;
    - (NSString *)subtitle;
    - (NSString *)title;
    
    @end
    
    //--- .m ---
    
    @implementation Placemark
    
    @synthesize coordinate;
    @synthesize strSubtitle;
    @synthesize strTitle;
    
    - (NSString *)subtitle{
        return self.strSubtitle;
    }
    - (NSString *)title{
        return self.strTitle;
    }
    
    -(id)initWithCoordinate:(CLLocationCoordinate2D) c {
        self.coordinate = c;
        [super init];
        return self;
    }
    
    @end
    

    然后在我的地图视图控制器中,我将注释放置在:

    - (void) setPlacemarkWithTitle:(NSString *) title andSubtitle:(NSString *) subtitle forLocation: (CLLocationCoordinate2D) location {
    
        //remove pins already there...
        NSArray *pins = [mapView annotations];
    
        for (int i = 0; i<[pins count]; i++) {
          [mapView removeAnnotation:[pins objectAtIndex:i]];
        }
    
        Placemark *placemark=[[Placemark alloc] initWithCoordinate:location];
        placemark.strTitle = title;
        placemark.strSubtitle = subtitle;
        [mapView addAnnotation:placemark];  
        [self setSpan]; //a custom method that ensures the map is centered on the annotation 
    } 
    

    【讨论】:

    • 我尝试重新绘制注释,但它重新定位了点。不太确定如何在注释上设置新位置?
    • 我已经从早期的应用程序中提取了我使用的代码,并在上面的答案中更新了它。它是不久前写的(iOS 3.0!),但它应该仍然是金色的。希望有帮助!
    • yup 帮了很多忙,我不得不用自定义的 MKAnnotation 完全替换 MKUserLocation。我的其他观点已经到位,只是没有考虑将其作为一个整体替换。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-23
    • 2017-04-15
    • 1970-01-01
    • 1970-01-01
    • 2013-04-17
    • 1970-01-01
    相关资源
    最近更新 更多