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

2024-03-07

For my iOS应用程序(内置iOS7),我需要在应用程序加载时显示用户的当前位置。我正在使用Google Maps iOS SDK。我正在关注这个谷歌地图 https://developers.google.com/maps/documentation/ios/但我无法弄清楚。如果您走过这条路,请帮忙。


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

事实上,iOS 版 GoogleMaps 会为您完成所有工作。您不必直接使用 CoreLocation。

您唯一需要做的就是添加yourMapView.myLocationEnabled = YES;框架将完成一切。 (除了将地图置于您所在位置的中心)。

我所做的:我只是按照以下步骤操作文档 https://developers.google.com/maps/documentation/ios/start。我得到了一张以悉尼为中心的地图,但如果我缩小并移动到我的位置(如果您使用真实设备,否则使用模拟器工具以苹果的位置为中心),我可以看到我位置上的蓝点。

现在,如果您想更新地图以跟随您的位置,您可以复制 Google 示例MyLocationViewController.m包含在框架目录中。他们只是在上面添加了一个观察者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

通过我给您的文档和框架中包含的示例,您应该能够做您想做的事情。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Google Maps iOS SDK,获取用户的当前位置 的相关文章

随机推荐