0

G'day 所有,

我正在尝试更改 MKPointAnnotations 中引脚的颜色。我尝试遵循的大多数代码示例都太复杂了,我无法遵循。我想出了这个:

//
//  ViewController.m
//  PlayingWithMaps
//
//  Created by custom on 22/09/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotationPoint
{
static NSString *annotationIdentifier = @"annotationIdentifier";

MKPinAnnotationView *pinView = [[MKPinAnnotationView alloc]initWithAnnotation:annotationPoint reuseIdentifier:annotationIdentifier];

pinView.pinColor = MKPinAnnotationColorPurple;

return pinView;
}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

// First, choose what type of map
//mapView.mapType = MKMapTypeHybrid;
mapView.mapType = MKMapTypeStandard;


// Second, where is the centre of the map
CLLocationCoordinate2D centreCoord = {.latitude =  -37.123456, .longitude =  145.123456};
MKCoordinateSpan mapSpan = {.latitudeDelta =  0.001, .longitudeDelta =  0.001};
MKCoordinateRegion region = {centreCoord, mapSpan};

[mapView setRegion:region];

// Now lets make a single annotation.
CLLocationCoordinate2D annotationCoordinate;

annotationCoordinate.latitude = -37.781650;
annotationCoordinate.longitude = 145.076116;

MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc]init];
annotationPoint.coordinate = annotationCoordinate;
annotationPoint.title = @"My point of interest";
annotationPoint.subtitle = @"Location";

[mapView addAnnotation:annotationPoint];

// Read a set of points from files into arrays
NSString *fileString = [NSString stringWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"latitudes" ofType:@"txt"] encoding:NSUTF8StringEncoding error:nil];
NSMutableArray *latitudeStringsArray = [NSMutableArray arrayWithArray:[fileString componentsSeparatedByString:@"\n"]];

fileString = [NSString stringWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"longitudes" ofType:@"txt"] encoding:NSUTF8StringEncoding error:nil];
NSMutableArray *longitudeStringsArray = [NSMutableArray arrayWithArray:[fileString componentsSeparatedByString:@"\n"]];

// Now lets put them onto the map
int i; // Loop control
for (i=0; i<25; i++) 
{
    MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc]init];
    annotationCoordinate.latitude = [[latitudeStringsArray objectAtIndex:i]floatValue];
    annotationCoordinate.longitude = [[longitudeStringsArray objectAtIndex:i]floatValue];
    annotationPoint.coordinate = annotationCoordinate;
    annotationPoint.title = [NSString stringWithFormat:@"Point %d",i];
    annotationPoint.subtitle = [NSString stringWithFormat:@"%f, %f",[[latitudeStringsArray objectAtIndex:i]floatValue],[[longitudeStringsArray objectAtIndex:i]floatValue]];
    [mapView addAnnotation:annotationPoint];
}

}

- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}

@end

这些点出现在正确的位置,但它们是默认的红色,而不是我试图实现的紫色。从我所做的阅读中我的理解是,只要创建注释点但没有发生,就应该调用 (MKAnnotationView) 方法。

我知道这是我缺少的一些基本的东西,但我不知道是什么。

非常感谢所有帮助。

干杯,

4

1 回答 1

2

如果它没有被调用,那么你还没有将你的 ViewController 设置为地图视图的委托。

于 2012-09-25T03:19:09.910 回答