地图上的图钉着色

发布于 2024-09-19 03:53:19 字数 2991 浏览 2 评论 0原文

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

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

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

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

问候, 斯蒂芬

I'm trying to mark a starting pin and ending pin on my map with different colours. Maybe the starting pin in green and the ending pin in red.

My code below, reads the latitude / longitude coordinates from core data and loops around placing a red pin for each object found in core data.

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

From what I've read and some examples I found when googling the following code is called for each pin on the map. Its not placing any green pins on my map. Maybe I should passing coordinates to this method and allowing it to place the pin on the map instead of the code above.

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

Any help or a push in the right direction (excuse the pun) is very much appreciated.

Regards,
Stephen

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吃兔兔 2024-09-26 03:53:19
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; 
}

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

MKPinAnnotationView *annView = nil;
...

You do not create annotation view here, just operate with nil object (that works fine but does nothing) and return nil - so naturally pin is not display (as it does not exists).

Correct method will look like:

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

Here you try to reuse annotation view if possible (similar way you do with cells in table view), create and setup new one if not

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文