Google Maps iOS SDK,获取用户的当前位置

Sof*_*eda 13 objective-c ios google-maps-sdk-ios

对于我的iOS应用程序(iOS7内置),我需要在应用程序加载时显示用户的当前位置Google Maps iOS SDK.我正在使用.我正在关注 谷歌地图 但我无法理解.如果你走过这条路,请帮忙.

Max*_*lle 40

忘记我之前的回答.如果您使用本机MapKit.framework,它可以很好地工作.

事实上,GoogleMaps for iOS可以为您完成所有工作.您不必直接使用CoreLocation.

你唯一要做的就是添加yourMapView.myLocationEnabled = YES;,框架将做所有事情.(除了你位置上的地图中心).

我做了什么:我只是按照以下文档的步骤.我得到了一张以悉尼为中心的地图但是如果我缩小并移动到我的位置(如果你使用真实的设备,否则使用模拟器工具以Apple的位置为中心),我可以看到我的位置上的蓝点.

现在,如果您想要更新地图以跟随您的位置,您可以复制MyLocationViewController.m框架目录中包含的Google示例.他们只是在myLocation属性上添加一个观察者来更新相机属性:

@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
Run Code Online (Sandbox Code Playgroud)

通过我给你的文档和框架中包含的样本,你应该能够做你想做的事.


Max*_*lle 9

似乎Google Maps iOS SDK无法访问设备位置.所以你必须使用CLLocationManagerof 来检索位置iOS.

首先,添加CoreLocation.framework到您的项目:

  • 进去 Project Navigator
  • 选择您的项目
  • 单击选项卡 Build Phases
  • 添加CoreLocation.frameworkLink Binary with Libraries

然后,您需要做的就是遵循Apple文档的基本示例.

  • 创建一个CLLocationManager可能在你的ViewDidLoad:

    if (nil == locationManager)
        locationManager = [[CLLocationManager alloc] init];
    
    locationManager.delegate = self;
    //Configure Accuracy depending on your needs, default is kCLLocationAccuracyBest
    locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
    
    // Set a movement threshold for new events.
    locationManager.distanceFilter = 500; // meters
    
    [locationManager startUpdatingLocation];
    
    Run Code Online (Sandbox Code Playgroud)

随着CLLocationManagerDelegate位置更新,每次,您可以在更新用户的位置Google Maps:

- (void)locationManager:(CLLocationManager *)manager
      didUpdateLocations:(NSArray *)locations {
    // If it's a relatively recent event, turn off updates to save power.
   CLLocation* location = [locations lastObject];
   NSDate* eventDate = location.timestamp;
   NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
   if (abs(howRecent) < 15.0) {
      // Update your marker on your map using location.coordinate.latitude
      //and location.coordinate.longitude); 
   }
}
Run Code Online (Sandbox Code Playgroud)