MapKit自定义引脚注释

时间:2013-04-14 17:32:07

标签: ios objective-c mapkit mkpinannotationview

我有以下文件:

annotation.h:

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

@interface annotation : NSObject <MKAnnotation>

@property(nonatomic, assign) CLLocationCoordinate2D coordinate;
@property(nonatomic, copy) NSString *title;
@property(nonatomic, copy) NSString *subtitle;


@end

annotation.m:

#import "annotation.h"

@implementation annotation

@synthesize coordinate, title, subtitle;

@end

在主要代码中,它在其他地方找到了NSURL

[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[spinner stopAnimating];

// json parsing
results = [NSJSONSerialization JSONObjectWithData:data options:nil error:nil];

NSMutableArray * locations = [[NSMutableArray alloc] init];
CLLocationCoordinate2D location;
annotation * myAnn;

NSArray *pins = mapView.annotations;

if ([pins count])
{
    [mapView removeAnnotations:pins];
}

/* loop through the array, each key in the array has a JSON String with format:
 * title <- string
 * strap <- string
 * id <- int
 * date <- date
 * lat <- floating point double
 * long <- floating point double
 * link <- string

 */
int i;

for (i = 0; i < [results count]; i++) {
    //NSLog(@"Result: %i = %@", i, results[i]);
    //NSLog(@"%@",[[results objectAtIndex:i] objectForKey:@"long"]);

    myAnn = [[annotation alloc] init];
    location.latitude = (double)[[[results objectAtIndex:i] objectForKey:@"lat"] doubleValue];
    location.longitude = (double)[[[results objectAtIndex:i] objectForKey:@"long"] doubleValue];
    myAnn.coordinate = location;
    myAnn.title = [[results objectAtIndex:i] objectForKey:@"title"];
    myAnn.subtitle = [[results objectAtIndex:i] objectForKey:@"strap"];

    [locations addObject:myAnn];

    //NSLog(@"%i", [[results objectAtIndex:i] objectForKey:@"lat"]);
}

[self.mapView addAnnotations:locations];

我之前看过的事情就是说我需要使用MKAnnotationView而不是MKPinAnnotationView但是你可以看到我也没有使用它,是否可以使用自定义图像对于放在屏幕上的引脚。

3 个答案:

答案 0 :(得分:5)

您(a)确保将您的视图控制器定义为delegate的{​​{1}}; (b)实施MKMapView,例如:

viewForAnnotation

顺便说一下,在上面的示例中,我将- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation { if ([annotation isKindOfClass:[MKUserLocation class]]) { return nil; } if ([annotation isKindOfClass:[CustomAnnotation class]]) { static NSString * const identifier = @"MyCustomAnnotation"; MKAnnotationView* annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:identifier]; if (annotationView) { annotationView.annotation = annotation; } else { annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier]; } // set your annotationView properties annotationView.image = [UIImage imageNamed:@"Your image here"]; annotationView.canShowCallout = YES; // if you add QuartzCore to your project, you can set shadows for your image, too // // [annotationView.layer setShadowColor:[UIColor blackColor].CGColor]; // [annotationView.layer setShadowOpacity:1.0f]; // [annotationView.layer setShadowRadius:5.0f]; // [annotationView.layer setShadowOffset:CGSizeMake(0, 0)]; // [annotationView setBackgroundColor:[UIColor whiteColor]]; return annotationView; } return nil; } 课程的名称更改为annotationCustomAnnotation是一个类的可怕名称,因为(a)它不遵循大写首字母的类命名约定; (b)它与变量名annotation相同,默认情况下会使用许多annotation方法。

参考

答案 1 :(得分:0)

您当然可以使用MKAnnotationView的自定义图片,请参阅iOS MapKit custom pins

答案 2 :(得分:0)

MKAnnotationPinViewMKAnnotationView都是UIView的子类,因此会允许自定义视图。