gpt4 book ai didi

objective-c - 如何将自定义图钉添加到 iPhone MapKit?

转载 作者:太空狗 更新时间:2023-10-30 03:17:40 33 4
gpt4 key购买 nike

我正在 iPhone 上测试 MapKit 框架,非常想将显示位置的标准图钉切换为名为“location.png”的图像。

我如何修改我的代码以允许这样做?

主控

- (void)viewDidLoad
{
[super viewDidLoad];

//
// Set the map center
//
CLLocationCoordinate2D coordinate;
coordinate.latitude = 49.2802;
coordinate.longitude = -123.1182;
mapView.region = MKCoordinateRegionMakeWithDistance(coordinate, 2000, 2000);

//
// Set 10 random locations on the map for testing purposes
//
for(int i = 0; i < 10; i++)
{
CGFloat latDelta = rand()*.035/RAND_MAX -.02;
CGFloat longDelta = rand()*.03/RAND_MAX -.015;

CLLocationCoordinate2D newCoord = { coordinate.latitude + latDelta, coordinate.longitude + longDelta };
MapAnnotation* annotation = [[MapAnnotation alloc] initWithCoordinate:newCoord];
[mapView addAnnotation:annotation];
[annotation release];
}

[mapView setDelegate:self];
}

MapAnnotation.h

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

@interface MapAnnotation : NSObject <MKAnnotation> {
CLLocationCoordinate2D _coordinate;
}

- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate;

@end

MapAnnotation.m

#import "MapAnnotation.h"

@implementation MapAnnotation
@synthesize coordinate = _coordinate;

- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate
{
self = [super init];

if (self != nil)
{
_coordinate = coordinate;
}

return self;
}

@end

谢谢!

最佳答案

我在查看 MapCallouts 的源代码后解决了这个问题

这是我的解决方案:

- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation
{
static NSString *AnnotationViewID = @"annotationViewID";

MKAnnotationView *annotationView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];

if (annotationView == nil)
{
annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID] autorelease];
}

annotationView.image = [UIImage imageNamed:@"location.png"];
annotationView.annotation = annotation;

return annotationView;
}

关于objective-c - 如何将自定义图钉添加到 iPhone MapKit?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2699171/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com