2013-06-12 63 views
9

現在、私のプロジェクトでGoogle Maps APIを使用しています。私はデフォルトのカメラ/ズームをユーザーの場所に設定しようとしています。私はGoogle Maps API:現在地の座標を取得するiOS

NSLog(@"%f, %f", myLocation.coordinate.latitude, myLocation.coordinate.longitude); 

を行うときには、0を返す0、それが現在の位置座標を与えないため、

@implementation ViewController{ 

GMSMapView *mapView_; 

} 
@synthesize currentLatitude,currentLongitude; 

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 
mapView_.settings.myLocationButton = YES; 
mapView_.myLocationEnabled = YES; 


} 
- (void)loadView{ 

CLLocation *myLocation = mapView_.myLocation; 

GMSMarker *marker = [[GMSMarker alloc] init]; 
marker.position = CLLocationCoordinate2DMake(myLocation.coordinate.latitude, myLocation.coordinate.longitude); 
marker.title = @"Current Location"; 
marker.map = mapView_; 
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:myLocation.coordinate.latitude 
                 longitude:myLocation.coordinate.longitude 
                  zoom:6]; 
mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera]; 

self.view = mapView_; 
    NSLog(@"%f, %f", myLocation.coordinate.latitude, myLocation.coordinate.longitude); 

} 

しかし、それは動作しません:私はこれを行います。ユーザーの座標を正しく取得するにはどうすればよいですか?

+0

この行が何をしていますか? CLLocation * myLocation = mapView_.myLocation; 1. CoreLocation Frameworkを追加します。 2. CLLocationManagerのオブジェクトを作成します。 CLLocationManager * locationManager = [[CLLocationManager alloc] init]; 3. [locationManager startUpdatingLocation]; CLLocation * location = [locationManager location]; CLLocationCoordinate2D座標= [位置座標]。 –

+0

Googleマップにはユーザーの所在地を取得する機能がありますが、入手方法がわかりません – Alexyuiop

+0

参照:http:// stackoverflow。com/questions/16331767/how-to-get-current-location-in-google-map-sdk-in-iphone ホープが役に立ちます –

答えて

10

の.h

#import <CoreLocation/CoreLocation.h> 
@property(nonatomic,retain) CLLocationManager *locationManager; 

.Mはhereに答え。

+0

リンクをクリックすると、どのlocationManagerが表示されますか。 – DrDev

+1

独自の 'viewDidLoad'の実装で' [super viewDidLoad]; 'を呼び出す必要があります –

+0

初期化後、デリゲートを設定する必要があります。それ以外の場合、動作しません。 Swiftでは: 'locationManager.delegate = self'あなたは' CLLocationManagerDelegate'デリゲートに準拠し、 'func locationManager(manager:CLLocationManager、didUpdateLocations locations:[CLLocation]){...}'を実装する必要があります。 'self.locationManager.stopUpdatingLocation()' – oyalhi

6

アプリが最初に起動したときに、GPSデバイスが現在の場所にロックされるまで(開始されたばかり)、通常これが初めての場合は、しばらく時間がかかりますアプリケーションが実行されているため、ユーザーはまだアプリケーションにその場所へのアクセスを許可するプロンプトに応答していません。また、mapView.myLocationは、マップビューが作成されたばかりのときに常に空(nilまたは座標0,0)であるようです。

したがって、ユーザーの場所がわかるまで待ってから、カメラの位置を更新する必要があります。

Puneetの提案によると、how to get current location in google map sdk in iphoneのコードを使用している可能性がありますが、サンプルコードにはロケーションマネージャの設定の詳細がありません(ロケーションマネージャのデリゲートの設定など)。あなたのために働きません。

別のオプションは、ここで説明するように、mapView.myLocationにKVOを使用することができますabout positioning myself,some problems

ところで、あなたのサンプルコードで、あなたがmapViewを作成する前に、mapView.myLocationにアクセスしている、そしてその場所は常にとにかくゼロになります。

- (NSString *)deviceLocation 
{ 
NSString *theLocation = [NSString stringWithFormat:@"latitude: %f longitude: %f", locationManager.location.coordinate.latitude, locationManager.location.coordinate.longitude]; 
return theLocation; 
} 

- (void)viewDidLoad 
{ 
    locationManager = [[CLLocationManager alloc] init]; 
    locationManager.distanceFilter = kCLDistanceFilterNone; 
    locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m 
    [locationManager startUpdatingLocation]; 
} 

2

iOS用の新しいGoogleMap SDKデモをダウンロードしました。ここで私はソースコードから、 "現在の場所"がKVO経由でどのように達成されたかを見てきました。

#if !defined(__has_feature) || !__has_feature(objc_arc) 
#error "This file requires ARC support." 
#endif 

#import "SDKDemos/Samples/MyLocationViewController.h" 

#import <GoogleMaps/GoogleMaps.h> 

@implementation MyLocationViewController { 
    GMSMapView *mapView_; 
    BOOL firstLocationUpdate_; 
} 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.868 
                  longitude:151.2086 
                   zoom:12]; 

    mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera]; 
    mapView_.settings.compassButton = YES; 
    mapView_.settings.myLocationButton = YES; 

    // Listen to the myLocation property of GMSMapView. 
    [mapView_ addObserver:self 
      forKeyPath:@"myLocation" 
       options:NSKeyValueObservingOptionNew 
       context:NULL]; 

    self.view = mapView_; 

    // Ask for My Location data after the map has already been added to the UI. 
    dispatch_async(dispatch_get_main_queue(), ^{ 
    mapView_.myLocationEnabled = YES; 
    }); 
} 

- (void)dealloc { 
    [mapView_ removeObserver:self 
       forKeyPath:@"myLocation" 
        context:NULL]; 
} 

#pragma mark - KVO updates 

- (void)observeValueForKeyPath:(NSString *)keyPath 
         ofObject:(id)object 
         change:(NSDictionary *)change 
         context:(void *)context { 
    if (!firstLocationUpdate_) { 
    // If the first location update has not yet been recieved, then jump to that 
    // location. 
    firstLocationUpdate_ = YES; 
    CLLocation *location = [change objectForKey:NSKeyValueChangeNewKey]; 
    mapView_.camera = [GMSCameraPosition cameraWithTarget:location.coordinate 
                zoom:14]; 
    } 
} 

@end 

ご希望の場合はお手数ですが、

+0

このメソッドは非常に長いものであり、CLLocation Managerを使用する方が良いでしょう。 –

1

念のために誰もがスウィフト3でDrDevの優れた答えを望んでいる:

var locationManager: CLLocationManager? 

func deviceLocation() -> String { 
    let theLocation: String = "latitude: \(locationManager.location.coordinate.latitude) longitude: \(locationManager.location.coordinate.longitude)" 
    return theLocation 
} 

func viewDidLoad() { 
    locationManager = CLLocationManager() 
    locationManager.distanceFilter = kCLDistanceFilterNone 
    locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters 
    // 100 m 
    locationManager.startUpdatingLocation() 
} 
関連する問題