9

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

@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);

}

しかし、それはうまくいきません。

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

0、0 を返し、現在の位置座標は提供しません。ユーザーの座標を正しく取得するにはどうすればよいですか?

4

4 に答える 4

6

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

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

1 つの方法は、Puneet によって提案されているように、iPhone の Google マップ SDK で現在の位置を取得する方法のコードを使用することですが、そこにあるサンプル コードには、ロケーション マネージャーの設定の詳細 (ロケーション マネージャーのデリゲートの設定など) が欠落していることに注意してください。それがあなたにとってうまくいかなかった理由かもしれません。

mapView.myLocationここで説明されているように、別のオプションとして KVO on を使用することもできます:自分自身の配置について、いくつかの問題

ちなみに、サンプルコードでは、mapView.myLocationを作成する前にアクセスしmapViewているため、とにかく場所は常に nil になります。

于 2013-06-12T07:31:17.563 に答える
2

iOS 用の新しい GoogleMap SDK Demo をダウンロードしました。これは、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

それがあなたを助けることを願っています。

于 2015-08-31T13:41:14.550 に答える