MKMapView 有趣的商业
我试图在我的应用程序中的地图部分创建一个显示,但由于某种原因,注释将无法正确放置
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;
}
纬度和经度都有正确的值,我之前使用过此代码,但使用指向对象并且它工作正常,但是当我尝试直接给它值时它失败了
任何人都可以帮助我
*编辑,抱歉我已经有了我的 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];
}
im trying to create a show on map section in my app, but for some reason the annotations will not place correctly
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];
}
}
and in my place annotaion
- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate{
self = [super init];
if (self != nil) {
_coordinate = coordinate;
}
return self;
}
lattitude and longditude both have the right values, i have used this code before but using an array pointing to an object and it works fine , but when i try to give it the values directly it fails
can anyone help me out
*edit , sorry i already have my Viewforannotation just got missed out in the copy
- (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];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的 PlaceAnnotation 需要实现 MKAnnotation 协议。具体来说,方法
或者您可以定义属性并在构造函数中设置它,
这就是 MKMapView 知道在哪里放置注释的方式。
附注
另外,我不确定这是否合理:
可能没问题,但苹果建议:
Your PlaceAnnotation needs to implement the MKAnnotation protocol. Specifically, the method
or you can define the property and set it in your constructor
This is how the MKMapView knows where to place the annotation.
p.s.
Also, I'm not sure how kosher this is:
It's probably fine, but Apple recommend:
您需要实现 MKMapViewDelegate 协议方法
来提供 MKAnnotationView 实例(或子类)以用于显示注释。请参阅 MKMapViewDelegate 协议参考了解详情。
You need to implement the MKMapViewDelegate protocol method
to supply an MKAnnotationView instance (or subclass) to use to display the annotation. Go see the MKMapViewDelegate protocol reference for details.