MKAnnotation,简单的例子 [英] MKAnnotation, simple example

查看:82
本文介绍了MKAnnotation,简单的例子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有 MKAnnotation 的简单示例项目?我不知道在 addAnnotation 中要传递什么,因为它需要一些 id< Annotation>
开发人员站点上的示例都是数组或解析器左右,我只需要一个非常简单的示例来首先了解整个事情是如何工作的。

Is there a simple example project for MKAnnotation? I don't know what to pass at "addAnnotation", as it wants some "id<Annotation>". The examples at the developer site are all with arrays or parsers or so, I just need a very easy example to first understand, how the whole thing works.

提前致谢..

编辑:

我设置了一个班级 Pin 。在.h中写道

I set up a class Pin. In the .h it writes

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

@interface Pin : NSObject <MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString *title;
NSString *subTitle;
}

@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
@property (nonatomic, readonly) NSString *title;
@property (nonatomic, readonly) NSString *subTitle;

- (id)initWithCoordinates:(CLLocationCoordinate2D)location placeName:(NSString *)placeName description:(NSString *)description;
@end

根据onnoweb的回答。

according to what onnoweb answered.

在Pin.m中,我根据不同的例子实现了这样的结构:

In the Pin.m, I implemented like this, according to different examples:

#import "Pin.h"

@implementation Pin

@synthesize title, subTitle;

- (CLLocationCoordinate2D) coordinate {
CLLocationCoordinate2D coords;
coords.latitude = coordinate.latitude; //i want it that way
coords.longitude =  7.1352260; //this way it works

return coords;

- (NSString *) title {
return @"Thats the title";
}

- (NSString *) subtitle {
return @"Thats the sub";
}

- (id)initWithCoordinates:(CLLocationCoordinate2D)location placeName:(NSString *)placeName description:(NSString *)description {

return self;
}

- (void) dealloc {
[super dealloc];
}

@end

所以如果我设置坐标如评论所指出的那样,它是有效的。但我希望能够像第一条评论一样动态设置值。我尝试了各种各样的方法,但都没有成功。因为你没有指定 - (CLLocationCoordinate2D)坐标我试图合成坐标,但是就像它赢了也没办法。

So if I set the coordinates by hand, as pointed in the comment, it works. But I'd like to be able to set the value dynamically like in the first comment. I tried various ways, but none worked out. As you don't specify a - (CLLocationCoordinate2D) coordinate I tried to just synthesize coordinate, but like that it won't work either.

你能告诉我你的 MapPin.m 吗?

编辑2:

我设置 mapCenter 之类的

- (void)viewWillAppear:(BOOL)animated {
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.delegate = self;
[self.locationManager startUpdatingLocation];

CLLocation *curPos = locationManager.location;
location = curPos.coordinate;

CLLocationCoordinate2D mapCenter;
mapCenter.latitude =  location.latitude;
mapCenter.longitude = location.longitude;

MKCoordinateSpan mapSpan;
mapSpan.latitudeDelta = 0.005;
mapSpan.longitudeDelta = 0.005;

MKCoordinateRegion mapRegion;
mapRegion.center = mapCenter;
mapRegion.span = mapSpan;

mapKitView.region = mapRegion;
mapKitView.mapType = MKMapTypeStandard;
mapKitView.showsUserLocation=TRUE;
}

有什么问题吗?

推荐答案

您需要有一个实现 MKAnnotation 协议的类。以下是我的一个项目的片段:

You need to have a class implementing the MKAnnotation protocol. Here is a snippet from one of my projects:

@interface MapPin : NSObject<MKAnnotation> {
    CLLocationCoordinate2D coordinate;
    NSString *title;
    NSString *subtitle;
}

@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
@property (nonatomic, readonly) NSString *title;
@property (nonatomic, readonly) NSString *subtitle;

- (id)initWithCoordinates:(CLLocationCoordinate2D)location placeName:(NSString *)placeName description:(NSString *)description;

@end

然后,在您创建地图的地方,您将调用:

Then, where you create the map you'll call:

pin = [[MapPin alloc] initWithCoordinates:[track startCoordinates] placeName:@"Start" description:@""];
[map addAnnotation:pin];

编辑:

以下是实施:

@implementation MapPin

@synthesize coordinate;
@synthesize title;
@synthesize subtitle;

- (id)initWithCoordinates:(CLLocationCoordinate2D)location placeName:placeName description:description {
    self = [super init];
    if (self != nil) {
        coordinate = location;
        title = placeName;
        [title retain];
        subtitle = description;
        [subtitle retain];
    }
    return self;
}

- (void)dealloc {
    [title release];
    [subtitle release];
    [super dealloc];
}


@end

希望这个帮助,
Onno。

Hope this helps, Onno.

这篇关于MKAnnotation,简单的例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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