2014-01-07 150 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天的模擬器中運行,不知道它沒有模擬位置

    相關問題