2

次のファイルがあります。

注釈.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

注釈.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いますが、ご覧のとおり、どちらも使用していません。画面にドロップされたピンにカスタム画像を使用することは可能ですか.

4

3 に答える 3

5

あなたは(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;
}

ところで、上記の例では、annotationクラスの名前を に変更しましたCustomAnnotationannotationクラスの恐ろしい名前です。(a) 最初の文字を大文字にするクラス命名規則に従っていないためです。annotation(b)多くのMKMapViewDelegateメソッドがデフォルトで使用する変数名 と同じです。

参考文献

于 2013-04-14T18:09:46.897 に答える
0

のカスタム イメージを確実に使用できます。 iOS MapKit カスタム ピンMKAnnotationViewを参照してください。

于 2013-04-14T17:35:09.503 に答える