Jos*_*the 2 objective-c mapkit mkpinannotationview ios
我有以下文件:
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
Run Code Online (Sandbox Code Playgroud)
annotation.m:
#import "annotation.h"
@implementation annotation
@synthesize coordinate, title, subtitle;
@end
Run Code Online (Sandbox Code Playgroud)
在主代码中,它在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];
Run Code Online (Sandbox Code Playgroud)
我之前看过的事情就是说我需要使用MKAnnotationView而不是MKPinAnnotationView你可以看到我也没有使用它,是否可以使用自定义图像作为屏幕上掉落的引脚.
您(a)确保将您的视图控制器定义delegate为适合您的视图控制器MKMapView; (b)实施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;
}
Run Code Online (Sandbox Code Playgroud)
顺便说一句,在上面的例子中,我将你的annotation类名改为CustomAnnotation.annotation对于一个类来说,这是一个可怕的名称,因为它(a)它不遵循大写首字母的类命名约定; 和(b)它与变量名相同annotation,许多MKMapViewDelegate方法默认使用它们.
| 归档时间: |
|
| 查看次数: |
14514 次 |
| 最近记录: |