自定义当前位置注释图钉未随核心位置更新

发布于 2024-10-20 01:55:45 字数 591 浏览 5 评论 0原文

尝试为当前位置添加自定义图钉,但位置未更新。即使在设置 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; 一切正常,但我丢失了自定义图像。我真的很想让这个发挥作用。任何帮助将不胜感激。

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]];
    }

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

没︽人懂的悲伤 2024-10-27 01:55:45

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

您需要执行的操作是监听手机的位置更新,并自行手动重新定位注释。您可以通过创建 CLLocationManager 实例来完成此操作,并在位置管理器通知其委托更新时删除并替换您的注释:

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

为了重新定位坐标,我有一个类 Placemark,它符合协议 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

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

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

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

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
}

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

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 
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文