如何在mapView中显示注释

如何在mapView中显示注释

本文介绍了如何在mapView中显示注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于MKAnnotations的问题。我想添加到我的mapView注释来绘制它们与典型的Pin。我该怎么做?我已经实现了坐标获取器,因为坐标是只读的,并且我给出了该方法中坐标的值。如果我像这样添加注释:
[self.mapView addAnnotations:self.annotations];
这不够吗?因为在我的mapView中没有出现引脚,并且我不知道是否必须调用某个方法,或者我必须调用setter作为坐标(但我认为自己的mapView调用Annotation类的getter给每个注释赋予这个坐标)。我的mapView.annotations有注释,但它们不出现在地图中。



谢谢

解决方案

你需要做的只是:

pre $ code PinObject * pin = [[PinObject alloc] init];
[pin setCoordinate:CLLocationCoordinate2DMake(double latitude,double longitude)];
[map addAnnotation:pin];

在PinObject中,您需要扩展MKAnnotation并且.h文件看起来像:

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

@interface PinObject:NSObject< MKAnnotation> {
}

@property(非原子,只读)CLLocationCoordinate2D坐标;
- (void)setCoordinate:(CLLocationCoordinate2D)newCoordinate;
@end

您还需要将MapKit框架和CoreLocation框架添加到您的项目中



在你的PinObject.m文件中,你需要实现这个方法

   - (void)setCoordinate:(CLLocationCoordinate2D)newCoordinate {
coordinate = newCoordinate;
}

我希望我能帮到你。


I got a question about the MKAnnotations. I want to add to my mapView annotations to draw them with the typical Pin. How can I do it? I've implemented the coordinate getter because coordinate is readonly, and I gave value to the coordinates in that method. If I add the annotations like this:[self.mapView addAnnotations:self.annotations];Isn't it enough? Because in my mapView don't appear the pins, and I don't know if a have to call some method, or I have to call a setter for the coordinate (but i thought that the own mapView call the getter of the Annotation class giving that coordinate to each Annotation). My mapView.annotations has annotations but they dont appear in the map.

Thanks

解决方案

All you need to do is

PinObject* pin = [[PinObject alloc] init];
[pin setCoordinate:CLLocationCoordinate2DMake(double latitude, double longitude)];
[map addAnnotation:pin];

In the PinObject you need to extends MKAnnotation and your .h file will look like:

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

@interface PinObject : NSObject <MKAnnotation>{
}

@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
- (void)setCoordinate:(CLLocationCoordinate2D)newCoordinate ;
@end

You also need to add the MapKit framework and the CoreLocation framework to your project

In your PinObject.m file you need to implement the method

- (void)setCoordinate:(CLLocationCoordinate2D)newCoordinate {
    coordinate = newCoordinate;
}

I hope I helped you.

这篇关于如何在mapView中显示注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 09:09