2013年7月28日日曜日

Google Maps SDK for iOS で現在地をマップ中心に表示し続ける



MapKitよりGoogle Maps SDK for iOS のほうが使いやすいって噂なので使ってみた。
とりあえず現在地表示。

やりかた

1.Google Maps SDK for iOSの導入

ここに書いてる通りやる。とっても親切に書いてる。
http://qiita.com/shu223/items/bfb5ef3e45682c2bb763
・ちょっとだけつまずいたところ
ビルド設定の「Architectures を armv7 にする」なんだけど、
俺のはStandard(armv7,armv7s)ってなってて、ん?って思ったけど、そのままでいけた。

2.コード書く

マップ表示して現在地表示するためにちょっとコード書く。
なんだかインポートのコードの表示がおかしい。。。

ViewController.h
#import 
#import 

@interface ViewController : UIViewController

@end

ViewController.m
#import "ViewController.h"
#import 

@interface ViewController ()

@end

@implementation ViewController

GMSMapView *_mapView;
CLLocationManager *_locationManager;

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // 地図の表示
    GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.86
                                                            longitude:151.20
                                                                 zoom:6];
    _mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
    _mapView.myLocationEnabled = YES;
    _mapView.settings.myLocationButton = YES;

    self.view = _mapView;
    
    _locationManager = [[CLLocationManager alloc] init];
    
    // 位置情報サービスが利用できるかどうかをチェック
    if ([CLLocationManager locationServicesEnabled]) {
        _locationManager.delegate = self;
        // 測位開始
        [_locationManager startUpdatingLocation];
    } else {
        NSLog(@"Location services not available.");
    }

}

// 位置情報更新時
- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation {
    [_mapView animateToLocation : [newLocation coordinate]];
    
    //緯度・経度を出力
    NSLog(@"didUpdateToLocation latitude=%f, longitude=%f",
          [newLocation coordinate].latitude,
          [newLocation coordinate].longitude);
}

// 測位失敗時や、位置情報の利用をユーザーが「不許可」とした場合などに呼ばれる
- (void)locationManager:(CLLocationManager *)manager
       didFailWithError:(NSError *)error{
    NSLog(@"didFailWithError");
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

良い感じに書けたら完成。
左上の再生ボタンを押すとiPhoneのSimulatorが起動して実行される。


今回も皆さんのおかげでとってもいい感じに出来ました。

今回参考にしたサイト Google Maps SDK for iOSの導入
http://qiita.com/shu223/items/bfb5ef3e45682c2bb763

現在地の表示
http://www.atmarkit.co.jp/ait/articles/1104/04/news117_2.html


スポンサーリンク

Related Posts Plugin for WordPress, Blogger...