我正在尝试用不同的颜色标记我的地图上的起始针和结束针。也许绿色的起始引脚和红色的结束引脚。
下面的代码,从核心数据读取纬度/经度坐标,并为核心数据中的每个对象放置一个红色针脚。
MapPin.h
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
@interface MapPin : NSObject <MKAnnotation>{
CLLocationCoordinate2D coordinate;
}
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
-(id)initwithCoordinates:(CLLocationCoordinate2D)location;
@end
MapPin.m
#import "MapPin.h"
@implementation MapPin
@synthesize coordinate;
-(id)initwithCoordinates:(CLLocationCoordinate2D)location
{
self = [super init];
if (self != nil) {
coordinate = location;
}
return self;
}
-(void)dealloc{
[super dealloc];
}
@end
MapViewController.m
-(void)addAnnotations
{
if (![sharedMapID isEqualToString:@""]){
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"WayPoint" inManagedObjectContext:managedObjectContext];
[request setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"waypoint_map_id contains[cd] %@", sharedMapID];
[request setPredicate:predicate];
NSError *error = nil;
NSArray *results = [managedObjectContext executeFetchRequest:request error:&error];
// Check for errors from the FetchRequest
if (nil == results || nil != error)
NSLog(@"Error getting results : %@", error);
listArray = [results mutableCopy];
[request release];
// Loop through the array, get the coordinates and display on the map.
for (int i = 0; i < [listArray count]; i++)
{
NSString *strXCoordinate = [[listArray objectAtIndex: i] valueForKey:@"waypoint_x"];
NSString *strYCoordinate = [[listArray objectAtIndex: i] valueForKey:@"waypoint_y"];
double dblXCoordinate = [strXCoordinate doubleValue];
double dblYCoordinate = [strYCoordinate doubleValue];
CLLocationCoordinate2D coordinate = {dblYCoordinate, dblXCoordinate};
MapPin *pin = [[MapPin alloc]initwithCoordinates:coordinate];
[self.mapView addAnnotation:pin];
[pin release];
}
[listArray release];
}
}
从我所阅读的内容和我在谷歌搜索下面的代码中找到的一些例子,为地图上的每个引脚调用。它没有在我的地图上放置任何绿色别针。也许我应该将坐标传递给这个方法并允许它将引脚放在地图上而不是上面的代码。
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
MKPinAnnotationView *annView = nil;
annView.pinColor = MKPinAnnotationColorGreen;
annView.animatesDrop = TRUE;
annView.canShowCallout = YES;
annView.calloutOffset = CGPointMake(-5, 5);
return annView;
}
非常感谢任何帮助或推动正确的方向(借口双关语)。
此致 斯蒂芬
答案 0 :(得分:0)
MKPinAnnotationView *annView = nil;
...
你不在这里创建注释视图,只使用nil对象(它工作正常但什么都不做)并返回nil - 所以自然pin不显示(因为它不存在)。
正确的方法如下:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
MKPinAnnotationView *annView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@"SomeID"];
if (!annView){
annView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"SomeID"] autorelease];
annView.pinColor = MKPinAnnotationColorGreen;
annView.animatesDrop = TRUE;
annView.canShowCallout = YES;
annView.calloutOffset = CGPointMake(-5, 5);
}
annView.annotation = annotation;
return annView;
}
在这里,如果可能,尝试重用注释视图(与表视图中的单元格类似),如果不是,则创建并设置新注释视图