MKMapView 有趣的商业

发布于 2024-10-22 04:28:09 字数 1817 浏览 0 评论 0原文

我试图在我的应用程序中的地图部分创建一个显示,但由于某种原因,注释将无法正确放置

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冰葑 2024-10-29 04:28:09

您的 PlaceAnnotation 需要实现 MKAnnotation 协议。具体来说,方法

- (CLLocationCoordinate)coordinate;

或者您可以定义属性并在构造函数中设置它,

@property (assign) CLLocationCoordinate coordinate

这就是 MKMapView 知道在哪里放置注释的方式。

附注
另外,我不确定这是否合理:

CLLocationCoordinate2D newCoord = {coordinate.latitude, coordinate.longitude};

可能没问题,但苹果建议:

CLLocationCoordinate2D newCoord = CLLocationCoordinate2DMake(coordinate.latitude, coordinate.longitude);

Your PlaceAnnotation needs to implement the MKAnnotation protocol. Specifically, the method

- (CLLocationCoordinate)coordinate;

or you can define the property and set it in your constructor

@property (assign) CLLocationCoordinate coordinate

This is how the MKMapView knows where to place the annotation.

p.s.
Also, I'm not sure how kosher this is:

CLLocationCoordinate2D newCoord = {coordinate.latitude, coordinate.longitude};

It's probably fine, but Apple recommend:

CLLocationCoordinate2D newCoord = CLLocationCoordinate2DMake(coordinate.latitude, coordinate.longitude);
萌梦深 2024-10-29 04:28:09

您需要实现 MKMapViewDelegate 协议方法

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation

来提供 MKAnnotationView 实例(或子类)以用于显示注释。请参阅 MKMapViewDelegate 协议参考了解详情。

You need to implement the MKMapViewDelegate protocol method

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation

to supply an MKAnnotationView instance (or subclass) to use to display the annotation. Go see the MKMapViewDelegate protocol reference for details.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文