【问题标题】:Colouring Pins on a Map地图上的着色图钉
【发布时间】:2010-09-16 13:46:17
【问题描述】:

我正在尝试用不同颜色在我的地图上标记起始图钉和结束图钉。也许是绿色的起始引脚和红色的结束引脚。

我的代码在下面,从核心数据中读取纬度/经度坐标,并循环为核心数据中找到的每个对象放置一个红色引脚。

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; 
}

非常感谢任何帮助或推动正确方向(请原谅双关语)。

问候, 斯蒂芬

【问题讨论】:

    标签: iphone mapkit


    【解决方案1】:
    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; 
    }
    

    如果可能,您在这里尝试重用注释视图(与表格视图中的单元格类似的方式),如果没有,则创建并设置新的

    【讨论】:

    • 弗拉基米尔,感谢您的回复。我只是有点困惑应该用什么代替@“SomeID”。我正在阅读关于 dequeueReusableAnnotationViewWithIdentifier 的 Apple 文档,但仍然有点困惑。
    • 您可以为此使用任何字符串 - 只需确保使用创建时相同的 id 将视图出列。您还可以使用不同的 ID 创建和出列不同类型的注释视图(例如,您有地铁站和公共汽车站的视图 - 您可以使用不同的 id 创建和出列它们,然后为其设置适当的注释)
    猜你喜欢
    • 2014-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多