开发者

MKMapView Funny Buissness

开发者 https://www.devze.com 2023-02-17 09:48 出处:网络
im trying to create a show on map section 开发者_运维知识库in my app, but for some reason the annotations will not place correctly

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];
}


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);


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号