我想在 iPhone/iPad 应用程序中叠加开始和结束图像。我有起点和终点纬度和经度值,想在起点和终点之间绘制叠加层,并将起点图像放在起点上,将终点图像放在终点上。
我已经用谷歌搜索了,但我发现 MapKit 获取了一张图像并将其设置在起点和终点,找不到关于第二张图像的任何帮助。
喜欢
annotationView.image=[UIImage imageNamed:@"parkingIcon.png"];
它只为起点和终点设置一张图像。但我想为这两个点放置不同的图像。
请帮忙。
最佳答案
我明白了……感谢所有试图帮助我的人。完整的解决方案是
创建一个类
@interface MyAnnotationClass : NSObject <MKAnnotation> {
NSString *_name;
NSString *_description;
CLLocationCoordinate2D _coordinate;
}
@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSString *description;
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
-(id) initWithCoordinate:(CLLocationCoordinate2D) coordinate;
ViewDidLoad 方法代码:
mapView.delegate = self;
//Initialize annotation
MyAnnotationClass *commuterLotAnnotation=[[MyAnnotationClass alloc] initWithCoordinate:CLLocationCoordinate2DMake( 39.047752, -76.850388)];
commuterLotAnnotation.name = @"1";
MyAnnotationClass *overflowLotAnnotation=[[MyAnnotationClass alloc] initWithCoordinate:CLLocationCoordinate2DMake( 39.047958, -76.852520)];
overflowLotAnnotation.name = @"2";
//Add them to array
self.myAnnotations=[NSArray arrayWithObjects:commuterLotAnnotation, overflowLotAnnotation, nil];
//Release the annotations now that they've been added to the array
[commuterLotAnnotation release];
[overflowLotAnnotation release];
//add array of annotations to map
[mapView addAnnotations:_myAnnotations];
viewForAnnotation 代码:
-(MKAnnotationView *)mapView:(MKMapView *)MapView viewForAnnotation:(id<MKAnnotation>)annotation{
static NSString *parkingAnnotationIdentifier=@"ParkingAnnotationIdentifier";
if([annotation isKindOfClass:[MyAnnotationClass class]]){
//Try to get an unused annotation, similar to uitableviewcells
MKAnnotationView *annotationView=[MapView dequeueReusableAnnotationViewWithIdentifier:parkingAnnotationIdentifier];
//If one isn't available, create a new one
if(!annotationView){
annotationView=[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:parkingAnnotationIdentifier];
if([((MyAnnotationClass *)annotation).name isEqualToString: @"1"]){
annotationView.image=[UIImage imageNamed:@"passenger.png"];
}
else if([((MyAnnotationClass *)annotation).name isEqualToString: @"2"]){
annotationView.image=[UIImage imageNamed:@"place.png"];
}
}
return annotationView;
}
return nil;
}
这就是您可以为 MapKit 上的每个点添加单独图像的方法。
关于iphone:如何为 MapKit 上的每个针点显示不同的图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10585124/