4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

iOSのGoogleMapでマーカーをドラッグできる機能を実装する

Last updated at Posted at 2015-09-01

textFieldに入力された住所やキーワードを元にして、地図の初期表示の位置を決定し、その後にマーカーの微調整ができるmapを実装します。
view周りの実装は省略しています。

環境

  • iOS7以降を想定
  • GoogleMapsを使用

実装方法

mapの初期表示をviewDidLoadで設定する

viewDidLoad
# import <CoreLocation/CoreLocation.h>
# import <GoogleMaps/GoogleMaps.h>

- (void)viewDidLoad {
	[super viewDidLoad];
	_mapView.delegate = self; // .hにもdelegateの実装をする宣言を忘れないようにする
	_hogeTextView.delegate = self;

	// mapの初期表示をとりあえず東京駅にする
	GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:35.681382
                                                            longitude:139.766084
                                                                 zoom:17];
    _mapView.camera = camera;
}

textFieldが編集完了したタイミングでgeocoderを使って緯度経度を取得する

viewDidLoad
- (void)textFieldDidEndEditing:(UITextField *)textField
{
	CLGeocoder *geocoder = [CLGeocoder new];
    [geocoder geocodeAddressString:textField.text
                 completionHandler:^(NSArray *placemarks, NSError *error) {
                     CLPlacemark *placeMark = [placemarks objectAtIndex:0];
                     CLLocationCoordinate2D coord = placeMark.location.coordinate;
                     
						_latitude = placeMark.location.latitude;
						_longitude = placeMark.location.longitude;
                     [self createMarkerFromLocation:coord.latitude
                                          longitude:coord.longitude];
                 }];
}

// 指定した緯度経度にマーカーを作成するメソッド
- (void)createMarkerFromLocation:(float)latitude
                       longitude:(float)longitude
{
    GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:latitude
                                                            longitude:longitude
                                                                 zoom:17];
    _mapView.camera = camera;
    
    // マーカーを初期化してから作成
	// 初期化しないとどんどんmarkerが増えていく
    marker.map = nil;
    marker = [[GMSMarker alloc] init];
    marker.position = camera.target;
    marker.appearAnimation = kGMSMarkerAnimationPop;
    marker.map = _mapView;
}

マーカーをドラッグした後の処理

didEndDraggingMarker
// ドラッグし終わったタイミングで呼ばれるdelegateメソッド
- (void)mapView:(GMSMapView *)mapView didEndDraggingMarker:(GMSMarker *)marker
{
	// マーカーを移動した位置の緯度経度を取得
	_latitude = marker.position.latitude;
	_longitude = marker.position.longitude;

	// マーカーを移動した地点を地図の中心にする
    GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:_latitude
                                                            longitude:_longitude
                                                                 zoom:_mapView.camera.zoom];
    _mapView.camera = camera;
}

リンク

Google Maps SDK for iOSの公式ドキュメント
GMS関連のdelegate一覧

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?