开发者

ViewForAnnotation is not being called

开发者 https://www.devze.com 2023-01-18 05:37 出处:网络
I am trying to make a map, where I can see my current location, and see what the street is called. so far, I am able to put a pin on my map, but for some reason, I am not getting the callout.

I am trying to make a map, where I can see my current location, and see what the street is called.

so far, I am able to put a pin on my map, but for some reason, I am not getting the callout. and I have put a NSLog in my viewForAnnotation method, but it is not being called, so i wasn't able to test it.

can someone help me?

-(void)lat:(float)lat lon:(float)lon
{
    CLLocationCoordinate2D location;
    location.latitude = lat;
    location.longitude = lon;
    NSLog(@"Latitude: %f, Longitude: %f",location.latitude, location.longitude);
    //One location is obtained.. just zoom to that location

    MKCoordinateRegion region;
    region.center=location;
    //Set Zoom level using Span
    MKCoordinateSpan span;
    span.latitudeDelta=.005f;
    span.longitudeDelta=.005f;
    region.span=span;
    [map setRegion:region animated:TRUE];

    //MKReverseGeocoder *geocoder=[[MKReverseGeocoder alloc] initWithCoordinate:location];
    //geocoder.delegate=self;
    //[geocoder start];
    if (cPlacemark != nil) {
        [map removeAnnotation:cPlacemark];
    }
    cPlacemark=[[CustomPlacemark alloc] initWithCoordinate:location];
    cPlacemark.title = mPlacemark.thoroughfare;
    cPlacemark.subtitle = mPlacemark.locality;
    [map addAnnotation:cPlacemark];
    [cPlacemark release];

    [mLocationManager stopUpdatingLocation];
}

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
if ([annotation isKindOfClass:[MKUserLocation class]])
    return nil;

// try to dequeue an existing pin view first
if ([annotation isKindOfClass:[CustomPlacemark class]]){
MKPinAnnotationView *pinView=(MKPinAnnotationView *)[map dequeueReusableAnnotationViewWithIdentifier:@"customIdentifier"];

if (!pinView)
{
    // if an existing pin view was not available, create one
    MKPinAnnotationView* cPinAnnoView = [[[MKPinAnnotationView alloc]
                                           initWithAnnotation:annotation reuseIdentifier:@"customIdentifier"] autorelease];
    cPinAnnoView.pinColor = MKPinAnnotationColorPurple;
    cPinAnnoView.animatesDrop = YES;
    cPinAnnoView.canS开发者_运维问答howCallout = YES;
    // Add button
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    [leftButton addTarget:self action:@selector(annotationViewClick:) forControlEvents:UIControlEventTouchUpInside];
    cPinAnnoView.leftCalloutAccessoryView = leftButton;

} else
    {
        pinView.annotation = annotation;
    }
    return pinView; 
}
return nil;

}

Right now I have customized my viewForAnnotation to be like this. But I still can't get a callout from my pin and the pin remains red. But it should be purple of nothing at all


I had the same problem which was not setting the MapView delegate to the File Owner.

  1. Open your nib
  2. Right click on the MapView
  3. Drag the delegate to the File's Owner


I had the same problem, as you mentioned. The delegate had been set to ViewController, but the viewForAnnotation selector was not being called. After some checks, I realized if you do not call addAnotation in the main thread, mapView would not call viewForAnnotation, so following update resolved my problem:

Before:

[_mMapView addAnnotation:marker];

After:

dispatch_async(dispatch_get_main_queue(), ^{
     [_mMapView addAnnotation:marker];
});


In order to get the viewForAnnotation to be called, add mapView.delegate=self; to e.g. the viewDidLoad method:

- (void)viewDidLoad {

    [super viewDidLoad];
    mapView.delegate=self;
}


Could it be that your annotation has been added outside the current view area of the MKMapView?


For storyboard, Ctl drag the MKMapView to the orange circle on the bottom bar of ViewController, and select delegate.

This will solve the problem.


As vatrif mentioned in the comments, you must set your delegate BEFORE adding annotations to your MKMapView object.


Others have already explained, odds are high you have not connected your mapview delegate to your controller. Its the first thing to check


i have been working in ios 9 Mapview related app and I experienced the same problem.

somehow I solved my problem, in my case im resizing the mapview. I added delegate after i resize the mapview. it works now perfectly.!


After having set the delegate for the mapview if still the viewforannotation not getting called then this is something which you have missed - set the self.mapView.showsUserLocation to YES, in interface builder you can tick the shows userLocation option in attributes inspector.

0

精彩评论

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