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

对于我的iOS应用程序(在iOS7iOS7 ),我需要在应用程序加载时显示用户的当前位置。我正在使用Google Maps iOS SDK 。 我正在关注这个谷歌地图,但我无法弄清楚。 请帮助,如果你通过path。

看来Google Maps iOS SDK无法访问设备位置。 所以你必须使用iOS CLLocationManager检索位置。

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

  • 进入Project Navigator
  • select你的项目
  • 点击选项卡Build Phases
  • Link Binary with Libraries添加CoreLocation.framework

那么你所要做的就是遵循苹果文档的基本例子。

  • 可能在您的ViewDidLoad创build一个CLLocationManager

     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]; 

每次更新职位时,都可以通过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); } } 

忘记我以前的答案。 如果你使用本地的MapKit.framework,效果会很好。

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

你唯一要做的就是添加你的yourMapView.myLocationEnabled = YES; 框架将尽一切。 (除了在你的位置中心的地图)。

我所做的:我只是遵循以下文档的步骤。 而且我有一张以悉尼为中心的地图,但是如果我缩小并移动到我的位置(如果您使用真实的设备,否则使用模拟器工具来集中在苹果的位置),我可以看到我的位置上的蓝色点。

现在,如果你想更新地图来跟随你的位置,你可以复制包含在框架目录中的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 

随着我给你的文档和框架中包含的样本,你应该能够做你想要的。

Xcode 8.1,Swift 3,Google Maps iOS 2.1

一步一步的食谱:

1.)将密钥string添加到Info.plist(作为源代码打开):

 <key>NSLocationWhenInUseUsageDescription</key> <string>This app needs your location to function properly</string> 

2.)将CLLocationManagerDelegate添加到您的视图控制器类:

 class MapViewController: UIViewController, CLLocationManagerDelegate { ... } 

3.)将CLLocationManager添加到您的类中:

 var locationManager = CLLocationManager() var didFindMyLocation = false 

4.)请求权限:

 override func viewDidLoad() { super.viewDidLoad() locationManager.delegate = self locationManager.requestWhenInUseAuthorization() ... } 

5.)等待授权并启用位置:

 func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) { if status == CLAuthorizationStatus.authorizedWhenInUse { yourMapView.isMyLocationEnabled = true } } 

6.)添加可观察的位置变化:

 override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) { if !didFindMyLocation { let myLocation: CLLocation = change![NSKeyValueChangeKey.newKey] as! CLLocation // do whatever you want here with the location yourMapView.camera = GMSCameraPosition.camera(withTarget: myLocation.coordinate, zoom: 10.0) yourMapView.settings.myLocationButton = true didFindMyLocation = true print("found location!") } } 

而已!

在任何iOS设备上,使用Core Location获取用户的位置。 具体来说,你需要CLLocation类(和CLLocationManager)。

委托方法didTapMyLocationButton是不是方法?

https://developers.google.com/maps/documentation/ios/reference/protocol_g_m_s_map_view_delegate-p#ac0e0171b811e839d9021800ca9fd33f4

 - (BOOL)didTapMyLocationButtonForMapView:(GMSMapView *)mapView { return YES; } 

你可以通过获取位置

 (lldb) po mapView.myLocation <+37.33243033,-122.03088128> +/- 386.93m (speed -1.00 mps / course -1.00) @ 5/19/14, 6:22:28 PM Moscow Standard Time 

当前的位置不会显示在模拟器上…连接一个真实的设备,并试一试我花了2天在模拟器上运行,不知道它不模拟位置

有很多方法…我使用这种方法,它适用于所有情况。 谷歌给你所有与json格式的响应和它在你如何处理这些数据的一切。

有些步骤是在你的项目中加载谷歌地图。

  1. find这个链接的API键https://developers.google.com/places/ios-api/login您的谷歌帐户,并添加您的项目,并创build一个ios的关键&#x3002; 然后在你的项目中使用这个

  2. 启用谷歌地图所需的所有API

a-googlemaps sdk for ios b-googlemap direction api c-“”javasripts api d-picker api e-位置api for ios f距离matrixapi

在appdelegate方法…

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [GMSServices provideAPIKey:@"xxxxxxxx4rilCeZeUhPORXWNJVpUoxxxxxxxx"]; return YES; } 
  1. 在你的项目中添加所有需要的库和框架,如果谷歌地图不工作,这意味着你必须添加所需的框架与谷歌地图所有最好的发挥