自定义当前位置注释图钉未随核心位置更新 [英] Custom curent location Annotation pin not updating with core location

查看:78
本文介绍了自定义当前位置注释图钉未随核心位置更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试为当前位置添加自定义图钉,但是该位置不会更新.即使设置了setShowsUserLocation = YES;

Trying to add a custom pin for current location, However the location does not update. Even after setting 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;,则一切正常,但是丢失了自定义图像.我真的很想让它工作.任何帮助将不胜感激.

However, if I set to return nil; everything works fine, but I lose the custom image. I really want to get this to work. Any help would be greatly appreciated.

推荐答案

您已经看到setShowsUserLocation标志仅使用默认的蓝色气泡显示当前位置.

As you've seen the setShowsUserLocation flag only shows the current location using the default blue bubble.

您在这里需要做的事情是侦听手机中的位置更新信息,然后自己手动重新定位注释.您可以通过创建CLLocationManager实例来做到这一点,并在位置管理器通知其委托进行更新时删除并替换您的注释:

What you need to do here listen for location updates from the phone and manually reposition your annotation yourself. You can do this by creating a CLLocationManager instance and remove and replace your annotation whenever the Location Manager notifies its delegate of an update:

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

要重新放置坐标,我有一个类Placemark,它符合协议MKAnnotation:

To reposition the coordinates, I have a class, Placemark, that conforms to the protocol MKAnnotation:

//--- .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

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

Then in my mapview controller I place the annotation with:

- (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 
} 

这篇关于自定义当前位置注释图钉未随核心位置更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆