我试图在我的应用中的地图部分创建一个节目,但由于某种原因,注释将无法正确放置
MKMapView *mapView = (MKMapView*)self.view;
CLLocationCoordinate2D coordinate;
coordinate.latitude = 55.065767;
coordinate.longitude = -2.724609;
mapView.region = MKCoordinateRegionMakeWithDistance(coordinate, 500000, 500000);
mapView.delegate = self;
for (int i=0; i<1; i++) {
coordinate.latitude = latitude;
coordinate.longitude = longitude;
CLLocationCoordinate2D newCoord = {coordinate.latitude, coordinate.longitude};
PlaceAnnotation* annotation = [[PlaceAnnotation alloc] initWithCoordinate:newCoord];
//annotation.mTitle = companyTitle;
//annotation.mSubtitle = companyDescription;
[mapView addAnnotation:annotation];
[annotation release];
}
}
并在我的地方注释
- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate{
self = [super init];
if (self != nil) {
_coordinate = coordinate;
}
return self;
}
(p)lattitude和longditude都有正确的值,我之前使用过这段代码,但是使用了一个指向一个对象的数组,它工作正常,但是当我尝试直接给它值时,它就失败了
任何人都可以帮助我
*编辑,抱歉,我的Viewforannotation已经错过了副本
- (MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation
{
MKPinAnnotationView *pinAnnotation = nil;
if(annotation != mV.userLocation)
{
static NSString *defaultPinID = @"myPin";
pinAnnotation = (MKPinAnnotationView *)[mV dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if ( pinAnnotation == nil )
pinAnnotation = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];
pinAnnotation.canShowCallout = YES;
//instatiate a detail-disclosure button and set it to appear on right side of annotation
}
return pinAnnotation;
[pinAnnotation release];
}
答案 0 :(得分:1)
您需要实现MKMapViewDelegate协议方法
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
提供用于显示注释的MKAnnotationView实例(或子类)。有关详细信息,请参阅MKMapViewDelegate protocol reference。
答案 1 :(得分:1)
您的PlaceAnnotation需要实现MKAnnotation协议。具体来说,方法
- (CLLocationCoordinate)coordinate;
或者您可以定义属性并在构造函数中设置它
@property (assign) CLLocationCoordinate coordinate
这就是MKMapView如何知道注释的放置位置。
P.S。 另外,我不确定这是多么犹豫:
CLLocationCoordinate2D newCoord = {coordinate.latitude, coordinate.longitude};
这可能很好,但Apple推荐:
CLLocationCoordinate2D newCoord = CLLocationCoordinate2DMake(coordinate.latitude, coordinate.longitude);