问题
I am just a beginning developer with XCode and was wondering if I could get some help. I am trying to display locations on a map. I have the map working and the basic annotations, too. However, I would like annotations for gas stations to be green, for instance. And maybe shopping malls in red. I found a tutorial but it turns all a different color. How would I go about making only certain annotations red and others green? (If you wouldn't mind breaking it down, I am still trying to learn) :)
Thanks!
Here is my code. ViewController.h:
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
@interface ViewController : UIViewController <MKMapViewDelegate>
@property (nonatomic, strong) IBOutlet MKMapView *mapView;
@end
ViewController.m:
#import "ViewController.h"
#import "Annotation.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation (MKPinAnnotationView *)view {
view.pinColor = MKPinAnnotationColorGreen;
}
- (void)viewDidLoad
{
[super viewDidLoad];
//---------- Grocery Stores--------------
//---------------Meijer Richmond,KY Annotation--------------
//Annotation
//Create a coordinate for use with the annotation
CLLocationCoordinate2D meijerRichmondLocation;
meijerRichmondLocation.latitude = 37.76486;
meijerRichmondLocation.longitude = -84.29691;
//Create annotation obj
Annotation *meijerAnnotation = [Annotation alloc];
meijerAnnotation.coordinate = meijerRichmondLocation;
meijerAnnotation.title = @"Meijer Richmond";
meijerAnnotation.subtitle = @" 2013 Lantern Ridge Dr.";
[self.mapView addAnnotation:meijerAnnotation];
//-------------------------------------------------------
//---------------Meijer Reynold Road Lexington, KY Annotation--------------
//Annotation
//Create a coordinate for use with the annotation
CLLocationCoordinate2D meijerLex1Location;
meijerLex1Location.latitude = 37.995744;
meijerLex1Location.longitude = -84.531546;
//Create annotation obj
Annotation *meijerLex1Annotation = [Annotation alloc];
meijerLex1Annotation.coordinate = meijerLex1Location;
meijerLex1Annotation.title = @"Meijer on Reynolds Road";
meijerLex1Annotation.subtitle = @"351 Meijer Way Suite 100";
[self.mapView addAnnotation:meijerLex1Annotation];
//----------------------------------------------------------------------
//---------------Meijer Lexington, KY Annotation-------------------------------
MKPointAnnotation *meijerLex2Annotation = [[MKPointAnnotation alloc] init];
meijerLex2Annotation.title = @"Meijer on Man-O-War";
meijerLex2Annotation.subtitle = @"2155 Paul Jones Way";
meijerLex2Annotation.coordinate = CLLocationCoordinate2DMake(38.0208932,-84.420606);
[self.mapView addAnnotation:meijerLex2Annotation];
//------------ Malls ----------------
//-------------------Mall Annotation---------------------
//Mall Annotation
//Create a coordinate for use with the annotation
CLLocationCoordinate2D fayetteMallLocation;
fayetteMallLocation.latitude = 37.9909;
fayetteMallLocation.longitude = -84.5269;
//Create annotation obj
Annotation *fayetteMallAnnotation = [Annotation alloc];
fayetteMallAnnotation.coordinate = fayetteMallLocation;
fayetteMallAnnotation.title = @"Fayette Mall";
fayetteMallAnnotation.subtitle = @"Lexington's Main Shopping Center.";
//-------------------------------------------------------
[self.mapView addAnnotation:fayetteMallAnnotation];
self.mapView.delegate = self;
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
来源:https://stackoverflow.com/questions/16020996/create-annotations-with-different-colors