imagePin.png

前回の続き:

[iPhone] Map Kitで遊ぼう:地図を出して現在位置にピンを立てる
http://blog.s21g.com/articles/1596

参考:

Drawing polyines or routes on a MKMapView (as an MKAnnotationView) – Part 2
http://spitzkoff.com/craig/?p=108

###MKAnnotationを実装するクラスを作成
MyAnnotation.h
rails>>
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

@interface MyAnnotation : NSObject {
CLLocationCoordinate2D _coordinate;
}

@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;

-(id)initWithCoordinate:(CLLocationCoordinate2D) coordinate;

@end
<<--

MyAnnotation.m
rails>>
#import "MyAnnotation.h"

@implementation MyAnnotation

@synthesize coordinate=_coordinate;

-(id)initWithCoordinate:(CLLocationCoordinate2D) c{
_coordinate=c;
return self;
}

@end
<<--

###ViewControllerでMKMapViewDelegateを実装
ポイントとしては

  • .hでMKMapViewDelegateを実装宣言
  • _mapView.delegate = self;
  • -(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id )annotationを実装
  • MyAnnotationなら画像を設定する:
    if([annotation isKindOfClass:[MyAnnotation class]]) {
    annotationView.image = [UIImage imageNamed:@"some.png"];

本当はMKAnnotationViewを派生して、MyAnnotationViewを作るのがベターなんですけど省略しています。

MapTutorialViewController.h
rails>>
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>

@interface MapTutorialViewController : UIViewController {
MKMapView *_mapView;
}
@end
<<--

MapTutorialViewController.m
rails>>
#pragma mark MKMapViewDelegate

  • (MKAnnotationView *)mapView:(MKMapView )mapView viewForAnnotation:(id )annotation{
    MKAnnotationView annotationView;
    NSString
    identifier = @"Pin";
    annotationView = (MKPinAnnotationView
    )[_mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
    if(nil == annotationView) {
    annotationView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier] autorelease];
    }
    if([annotation isKindOfClass:[MyAnnotation class]]) {
    annotationView.image = [UIImage imageNamed:@"some.png"]; //地図に表示したい画像を設定
    }
    return annotationView;
    }

  • (void)addSomeAnnotations {
    //a location
    double latitude = 37.331688999999997;
    double longitude = -122.030731;
    CLLocationCoordinate2D _location;
    _location.latitude = latitude;
    _location.longitude = longitude;

    CLLocationCoordinate2D location1;
    MyAnnotation *annotation;

    location1.latitude = _location.latitude+0.1;
    location1.longitude = _location.longitude+0.1;
    annotation =[[MyAnnotation alloc] initWithCoordinate:location1];
    [_mapView addAnnotation:annotation];

    location1.latitude = _location.latitude+0.5;
    location1.longitude = _location.longitude+0.5;
    annotation =[[MyAnnotation alloc] initWithCoordinate:location1];
    [_mapView addAnnotation:annotation];

}

  • (void)viewDidLoad {
    [super viewDidLoad];

    _mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
    _mapView.showsUserLocation=TRUE;
    _mapView.delegate = self;
    [self.view addSubview:_mapView];

    [self addSomeAnnotations];

}

<<--

posted by satoko satoko on Wed 9 Sep 2009 at 02:22 with 0 comments
Contents rssrss
NSAssertやNSLogを無効にする:-DNS_BLOCK_ASSERTIONS=1とNS_BLOCK_ASSERTIONS
GDBでview.frameを表示させる
Core Animationについてのリンク集
[iPhone] new BSDライセンスについてライセンス引用のメモ
Implementing a Container View Controller in UIViewController Class Reference
UITableView用のNSIndexPathの作り方
Xcodeのテンプレート、View-based ApplicationとWindow-based Applicationの違い
CopyOnSelectのLion対応
Xcode4のCode Snippetsを別のPCに移動する+gitで管理する
[iPhone] Singleton: iOS4以降はGCDのdispatch_onceを使う
Tags
iphone開発gitIBInterfaceBuilderiOSSIMBLSnippetsterminaltwitterxcodeXcode4
Comments rssrss
https://skyprofi.ru/stati/natyazhnoj-potolok-na-balkone/ https://skyprofi.ru/stati/natyazhnoj-potolok-na-balkone/: Эксклюзивный https://skyprofi.ru/tenevye-natyazhny... 04/06 19:38
https://stosastudio.ru/catalog/kuhni-lounge/ https://stosastudio.ru/catalog/kuhni-lounge/: Фабрика Tessarolo https://stosastudio.ru/vybiraem-... 04/06 08:14
KingofSmack KingofSmack: Here also good reads for this mobile application d... 05/10 18:02
satoko satoko: stackoverflowでも同じエラーを挙げている人がいたので、1.3でアップロードしたよってコメ... 12/13 03:36
ujihisa ujihisa: :%s/blog/glob/g 07/28 16:41
satoko satoko: しゅが〜様 返事が遅くなって申し訳ありません。また、投稿百景ご購入ありがとうございます。 『po... 10/11 14:05
しゅが~ しゅが~: こんにちは。投稿百景を発売日翌日から利用しています。本当にいいAppを作っていただきました。罫線絵文... 10/08 00:38
satoko satoko: 情報ありがとうございます!コミットを再利用するの部分、文章が不明瞭ですね。ちょっと修正します。教えて... 09/25 14:32