开发者

MapKit mapView:viewForAnnotation has no effect on pin color

开发者 https://www.devze.com 2023-04-13 06:00 出处:网络
I can\'t seem to change the pin color. I have my view controller extend <MKMapViewDelegate> and implement mapView:viewForAnnotationI\'m close but must be missing something.any help would be app

I can't seem to change the pin color.

I have my view controller extend <MKMapViewDelegate> and implement mapView:viewForAnnotation I'm close but must be missing something. any help would be appreciated.

MainViewController.h

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import "StopAnnotation.h"

#define METERS_PER_MILE 1609.344

@interface MainViewController : UIViewController <MKMapViewDelegate> {

}
@property (weak, nonatomic) IBOutlet MKMapView *mapView;

@end

MapViewController.m

#import "MainViewController.h"

@impl开发者_开发问答ementation MainViewController
@synthesize mapView=_mapView;

- (void)viewWillAppear:(BOOL)animated
{
    CLLocationCoordinate2D zoomLocation;
    zoomLocation.latitude = 43.066667;
    zoomLocation.longitude = -89.4;

    MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, METERS_PER_MILE, METERS_PER_MILE);

    MKCoordinateRegion adjustRegion = [_mapView regionThatFits:viewRegion];

    [_mapView setRegion:adjustRegion animated:YES];
    [_mapView addAnnotation:[[StopAnnotation alloc] initWithCoordinate:zoomLocation]];
}

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
    MKPinAnnotationView *pav = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil];
    pav.pinColor = MKPinAnnotationColorPurple;
    return pav;
}

// the rest of the methods are default, i.e. viewDid* and shouldAutorotate*, etc...

StopAnnotation.h

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

@interface StopAnnotation : NSObject <MKAnnotation> {
    CLLocationCoordinate2D coordinate;
    NSString *title;
    NSString *subtitle;
}
- (id)initWithCoordinate:(CLLocationCoordinate2D)c;
@end

StopAnnotation.m

#import "StopAnnotation.h"

@implementation StopAnnotation
@synthesize coordinate;

- (NSString *)subtitle {
    return @"subtitle";
}

- (NSString *)title {
    return @"title";
}

- (id)initWithCoordinate:(CLLocationCoordinate2D)c {
    coordinate = c;
    NSLog(@"%f,%f", c.latitude, c.longitude);
    return self;
}

@end

I'm doing an exercise & the code was mostly from here

Thanks!!


You must set MainViewController as the delegate of mapView, ie if you are not then the map view might be generating default pins, if you put a breakpoint in

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

does it actually get called?

0

精彩评论

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