3-3. 地図上にピンアイコンを立てる
「ローカルサーチAPI」を利用する準備ができましたので、次は地図にピンアイコンを表示させるための準備をします。
まず、ViewController.hでピンアイコンの情報を保存しておくための変数を宣言します。
@interface ViewController : UIViewController<YMKMapViewDelegate, CLLocationManagerDelegate, YolpLocalSearchDelegate>
{
YMKMapView *map; //YMKMapViewインスタンス用ポインタ
CLLocationManager *locationMgr; //ロケーションマネージャ
CLLocationCoordinate2D userLocation; //ユーザの位置(緯度経度)
YolpLocalSearch *localSearch; // ローカルサーチAPIアクセスクラス
NSMutableArray *annotations; //ピンアイコンオブジェクト保存用
NSMutableArray *annotationDatas; //ピンアイコンのデータ用
int count; //ピンアイコンのカウンタ
}
@end
- (void)dealloc
{
[map release];
[locationMgr release];
[annotations release]; //新たに設定したインスタンス変数の解放
[annotationDatas release];
[super dealloc];
}
ピンアイコンを表示させるために、YMKAnnotationプロトコルに準じたクラスを新たに作成します。今回は、MyAnnotationという名前で作成しましょう。
地図にアイコンを表示させる方法は、下記URLのチュートリアルでも解説しています。こちらも併せて参照してください。
#import <YMapKit/YMapKit.h>
#import <Foundation/Foundation.h>
@interface MyAnnotation : NSObject <YMKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString *annotationTitle;
NSString *annotationSubtitle;
}
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
@property (nonatomic, retain) NSString *annotationTitle;
@property (nonatomic, retain) NSString *annotationSubtitle;
- (id)initWithLocationCoordinate:(CLLocationCoordinate2D) coord
title:(NSString *)annTitle subtitle:(NSString *)annSubtitle;
- (NSString *)title;
- (NSString *)subtitle;
@end
#import <YMapKit/YMapKit.h>
#import "MyAnnotation.h"
@implementation MyAnnotation
@synthesize coordinate;
@synthesize annotationTitle;
@synthesize annotationSubtitle;
- (id)initWithLocationCoordinate:(CLLocationCoordinate2D) coord
title:(NSString *)annTitle subtitle:(NSString *)annSubtitle {
if (self=[super init]) {
coordinate.latitude = coord.latitude;
coordinate.longitude = coord.longitude;
annotationTitle = annTitle;
annotationSubtitle = annSubtitle;
}
return self;
}
- (NSString *)title
{
return annotationTitle;
}
- (NSString *)subtitle
{
return annotationSubtitle;
}
@end
次に、ViewController.mに、ピンアイコンを表示させる処理を実装します。
YolpLocalSearchのデリゲートメソッドで、JSONに変換したAPIのレスポンスをパースし、ピンアイコンのオブジェクトを生成します。
- (void) didFinishWithYolpLocalSearch:(YolpLocalSearch *)station
{
if (nil == annotations) {
// ピンアイコンの情報保存用の変数を生成
annotations = [[NSMutableArray alloc] init];
annotationDatas = [[NSMutableArray alloc] init];
} else if (0 < [annotations count]) {
// ピンアイコンオブジェクトをクリアする
[map removeAnnotations:annotations];
[annotations removeAllObjects];
}
count = 0;
for (NSDictionary* feature in [station.result objectForKey:@"Feature"]) {
NSString *title = [[NSString alloc] initWithString:[feature objectForKey:@"Name"]];
NSString *subtitle = [[NSString alloc] initWithString:[[feature objectForKey:@"Property"] objectForKey:@"Address"]];
// タイトルは alloc init しないとMapSDK内部で解放されてしまうので注意
NSString *sPoint = [[feature objectForKey:@"Geometry"] objectForKey:@"Coordinates"];
NSArray *aryPoint = [sPoint componentsSeparatedByString:@","];
CLLocationCoordinate2D point = CLLocationCoordinate2DMake([[aryPoint objectAtIndex:1] floatValue], [[aryPoint objectAtIndex:0] floatValue]);
//MyAnnotationの初期化
MyAnnotation* myAnnotation = [[MyAnnotation alloc] initWithLocationCoordinate:point title:title subtitle:subtitle];
//AnnotationをYMKMapViewに追加
[map addAnnotation:myAnnotation];
[annotations addObject:myAnnotation];
[annotationDatas addObject:feature];
count++;
[myAnnotation release];
}
}
- (void) didFailWithYolpLocalSearch:(YolpLocalSearch *)station
{
// ローカルサーチAPIの結果取得に失敗した場合
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"エラー" message:@"ローカルサーチAPI検索エラー" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
ピンアイコンをタップしたとき表示される吹き出しにボタンを追加したいので、吹き出しのイベント処理を実装します。
詳しくは、下記のチュートリアルでも解説しています。
// 吹き出しのViewのカスタマイズ
- (YMKAnnotationView*)mapView:(YMKMapView *)mapView viewForAnnotation:(MyAnnotation*)annotation{
//追加されたAnnotationがMyAnnotationか確認
if( [annotation isKindOfClass:[MyAnnotation class]] ){
//YMKPinAnnotationViewオブジェクトを作成
YMKPinAnnotationView *pin = [[[YMKPinAnnotationView alloc] initWithAnnotation: annotation reuseIdentifier: @"Pin"] autorelease];
//吹き出しに表示するボタンを追加
UIButton *rightAccessoryButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[rightAccessoryButton setFrame:CGRectMake(0, 0, 40, 20)];
[rightAccessoryButton setTitle:@"GO!" forState:UIControlStateNormal];
rightAccessoryButton.tag = count;
[pin setRightCalloutAccessoryView:rightAccessoryButton];
return pin;
}
return nil;
}
ここまでで、ピンアイコンを表示する準備は完了です。
次は、ローカルサーチの検索結果からピンアイコンを表示させます。
3-4. ローカルサーチ検索の実行
実際にローカルサーチを検索するメソッドを呼び出すのは、ViewController,.mです。
現在地の取得が完了したタイミングで、YolpLocalSearchクラスを初期化してsearchPointByLocationメソッドを呼び出します。
-(void)locationManager:(CLLocationManager*)manager didUpdateToLocation:(CLLocation*)newLocation fromLocation:(CLLocation *)oldLocation
{
// 現在位置取得が完了したら呼ばれる
userLocation = newLocation.coordinate;
//開発用設定
userLocation.latitude = 35.6657214;
userLocation.longitude = 139.7310058;
map.region = YMKCoordinateRegionMake(userLocation, YMKCoordinateSpanMake(0.005, 0.005));
// 測位終了
[locationMgr stopUpdatingLocation];
// 緯度経度を使ってYolpLocalSearchクラスを初期化
localSearch = [[YolpLocalSearch alloc] initWithLat:userLocation.latitude Lon:userLocation.longitude];
localSearch.delegate = self;
// ローカルサーチ検索を実行
[localSearch searchPointByLocation];
}
ここまで実装し、シミュレータを起動させると、現在地周辺のコンビニにピンアイコンが表示されているのが確認できます。
アイコンをタップすると、吹き出しの内部に店舗名と住所が表示されます。

