2014-01-07 151 views

回答

8

看来Google Maps iOS SDK无法访问设备位置。 因此,您必须使用iOSCLLocationManager来检索位置。

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

  • 围棋在Project Navigator
  • 选择项目
  • 单击该选项卡上Build Phases
  • 添加CoreLocation.frameworkLink Binary with Libraries

然后你只需要做的是遵循Apple documentation的基本例子。

  • 可能在你ViewDidLoad创建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); 
    } 
} 
+0

非常感谢。还有一件事需要知道,我可以在这里使用谷歌GMSMapView来呈现位置?或者我必须使用MKMapView? – Sofeda

+0

是的,您需要使用Google GMSMapView和GMSMarker。 MKMapView只有在你使用本地iOS MapKit的情况下。 –

+0

请Maxime帮助使用GMSMapView。 – Sofeda

2

在任何iOS设备上,通过Core Location获取用户的位置。具体而言,您需要CLLocation类(和CLLocationManager)。

+0

这是唯一的方法吗?由于我使用Google Maps iOS SDK,因此无法从此处获取它?当我需要搜索时,我会做什么取决于用户当前的位置? – Sofeda

+0

@SMi这是正确的方法。浏览Google Maps SDK文档,似乎没有办法提取当前位置。这并不让我感到意外,Google的工程师往往很聪明,没有充分的理由就不会重新发明轮子。核心位置并不难用(请看Maxime的答案)。 – JeremyP

+0

非常感谢。但是当我在Google地图上看到我的currentLocation时,它非常具体。我只想要详细的位置地址。当你向我指出这是正确的方式,所以我将以这种方式... – Sofeda

38

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

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

您唯一需要做的就是添加yourMapView.myLocationEnabled = YES;,框架将完成所有工作。 (除了在你的位置上放置地图)。

我做了什么:我只是按照以下步骤documentation。我收到了一张以悉尼为中心的地图,但如果我缩小并移动到我的位置(如果您使用的是真实设备,否则使用模拟器工具将焦点集中在Apple的位置),我可以看到我的立场上的蓝色点。

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

随着我给你的样品包括你应该能够做你想做的框架的文档。

+0

是的,我看到它了。当这个例子开始时,它会加载到悉尼,然后捏住我可以看到我的位置。我首先需要用当前的loaction加载蓝点。我无法捕获mapView_后面的信息。 settings.myLocationButton.If我可以得到我的当前位置的JSON它会让我分裂! – Sofeda

+0

这正是上面的代码('MyLocationViewController.m')。当我在iPhone上启动应用程序时,我将地图集中在我的位置上。要使用当前位置创建JSON,可能应该将代码放在由观察者触发的方法中。在那里,你可以用你的坐标建立一个JSON,并用你的想法做出你想要的。 –

+0

哇!让我使用它。你尝试谷歌地图搜索?在哪里我把一个字符串,并获得相关的JSONs? – Sofeda

-2

获得位置的方法有很多... 我用这个方法,它在所有的情况下工作。 Google为您提供了所有与json格式相关的反馈,并向您介绍了如何处理这些数据。

有些步骤可以在您的项目中加载谷歌地图。

  1. 从这个链接https://developers.google.com/places/ios-api/ 标志找到的API密钥与您的谷歌帐户,并添加您的项目,并创建一个IOS关键。 然后在项目中使用这个

  2. 使需要谷歌地图的所有API

一,谷歌地图的SDK为iOS B-GoogleMap的方向API C-”“javasripts API D-选择器API E-地方API为iOS ˚F距离矩阵API

中的appdelegate方法

...

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 

    [GMSServices provideAPIKey:@"xxxxxxxx4rilCeZeUhPORXWNJVpUoxxxxxxxx"]; 

    return YES; 
} 
  • 添加在您的项目 所有需要的库和框架,如果谷歌地图不工作就意味着你必须用谷歌地图添加所需的框架 所有最好的发挥
  • 3

    的Xcode 8.1,夫特3,谷歌地图的iOS 2.1

    一步配方步骤:

    1)添加密钥为字符串的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.)等待授权d启用位置:

    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!") 
    
         } 
    
        } 
    

    这就是它!

    0

    当前位置将不会显示在模拟器......连一个真正的设备,并给它一个尝试 我花了2天的模拟器中运行,不知道它没有模拟位置

    相关问题