如何在地图上定位公司(mapkit iOS)



>我正在开发一个简单的地图,我想在地图上找到一家公司,例如,公司名称,例如"苹果公司"。

你知道我该怎么做吗?

多谢!

雷加德斯,亚舒

你需要

有一个实现MKAnnotation协议的类。下面是一个示例

@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

以下是实现:

@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

然后,在您的MapView中,您需要添加您的公司位置,如下所示。

CLLocationCoordinate2D coord = [[[CLLocation alloc] initWithLatitude:35.936902 longitude:-79.024953] coordinate];//Here you need to mention your company latitude and longitude
MapPin *pin = [[MapPin alloc] initWithCoordinates:coord placeName:@"Apple Inc" description:@""];
[map addAnnotation:pin];

希望这会有所帮助。

最新更新