iphone - 将自定义注释添加到 map 时使用

标签 iphone ios objective-c mapkit

将自定义注释类添加到 map View 时,我无法访问它。我的自定义类工作正常,但当我将其添加到 map 时,我不确定如何通过此委托(delegate)访问自定义注释:

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation

我尝试在网上查找,但没有找到任何东西。任何帮助都会很棒。

它的名字是这样的:

CLLocationCoordinate2D coords = CLLocationCoordinate2DMake(shops.latitude, shops.longitude);
    AnnotationForId *shop = [[AnnotationForId alloc] initWithCoordinate:coords];
    //[[CLLocationCoordinate2DMake(shops.latitude, shops.longtitude)]];
    shop.title = shops.name;
    shop.subtitle = @"Coffee Shop";
    shop.shopId = shops.id;
    [map addAnnotation:shop];

最佳答案

这是如何创建自定义 AnnotationView 的简单示例。

创建自定义AnnotationView:

#import <MapKit/MapKit.h>

@interface AnnotationView : MKPlacemark

@property (nonatomic, readwrite, assign) CLLocationCoordinate2D coordinate;

@property (nonatomic, strong) NSString *title;
@property (nonatomic, strong) NSString *subtitle;

// you can put here any controllers that you want. (such like UIImage, UIView,...etc)

@end

并在.m文件

#import "AnnotationView.h"

@implementation AnnotationView

- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate addressDictionary:(NSDictionary *)addressDictionary
{
    if ((self = [super initWithCoordinate:coordinate addressDictionary:addressDictionary]))
    {
        self.coordinate = coordinate;
    }
    return self;
}

@end

//使用注释 在相关.m 文件中添加#import "AnnotationView.h":

CLLocationCoordinate2D pCoordinate ;
pCoordinate.latitude = LatValue;
pCoordinate.longitude = LanValue;

// Create Obj Of  AnnotationView class  

AnnotationView *annotation = [[AnnotationView alloc] initWithCoordinate:pCoordinate addressDictionary:nil] ;

    annotation.title = @"I m Here";
    annotation.subtitle = @"This is Sub Tiitle";

[self.mapView addAnnotation:annotation];

关于iphone - 将自定义注释添加到 map 时使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15684080/

相关文章:

iphone - 蓝牙应用的最佳平台

iphone - 一旦 ios 媒体库接管,是否可以插入您自己的音频?

android - android中Artoolkitplus获取MarkerId的方法

ios - 以编程方式在 iPhone 上调用紧急电话

objective-c - 检查方法是否存在

iphone - 如何检测 iOS 购买?

ios - 在 xcode 中跟踪僵尸对象

ios - 更改 MFMailComposeViewControllers 状态栏样式

ios - 不使用 Xcode 的 Flutter 项目中 iOS 的文件夹引用

ios - n 个 View Controller 或 n 个单元格