2016-02-22 51 views
0

我想要使用CLLocationManager返回的座標來查看它們是否是任何本地業務的近似匹配 - 例如,如果我偶然站在星巴克例如,我想我的應用程序建議,根據我的座標,我站在星巴克。如果我在星巴克外20米處,但它是我所站的最近的地址,我希望它能提出'星巴克'。從座標使用CLLocation和/或Mapkit尋找企業名稱信息

我可以返回CLPlacemarks沒問題,但它們包含的信息不包含任何地方的商業名稱。例如,我在英國,因此倫敦格洛斯特路上的南多的地址爲'170 Gloucester Road,London,SW7 4ST'。我可以返回倫敦格羅斯特路170號SW7 4ST在CLPlacemark info沒問題,但沒有提到Nando's。

我在各種帖子中看到信息可能位於名稱屬性或感興趣的區域,甚至是格式化的地址屬性中,但是我在倫敦市中心的廣泛測試表明情況並非如此,在英國率。即使在美國,「蘋果公司」也只在其地址格式化的地址屬性中被提及,但是在1無限循環之前。

我嘗試通過使用CLLocation獲取用戶座標,然後將它們用作編程MapKit搜索的基礎來實現此目的 - 這只是再次返回地址,而不是業務名稱。

在完成了Apple Maps應用程序的一些最終用戶測試後,我對此很感興趣,但如果在本地搜索一般情況下,它似乎會返回相當不完整的結果。 '酒吧','餐廳'。有些地方是包含的,有些則不是,有時結果包含相當遙遠的地方,儘管我知道附近有更多的餐館。

我快速總結出實現我想要的唯一方法就是使用Google Places API,這是我希望避免的一些原因,尤其是我在iOS編程方面缺乏經驗(在Swift btw中...)

如果任何人都可以明確地建議在Mapkit中本地無法執行此操作,並且我需要使用Google Places API,那麼至少我知道並且可以繼續使用它 - 但是如果有一種方法,我會非常感激,知道它是什麼...

在此先感謝!

回答

0

我也遇到類似的問題與MapKit,並在一段時間後發現,谷歌Places API的很多這類任務的最好檢查一下這個link找到關於如何使用谷歌的地方獲取當前位置的示例代碼

基本上,你需要

- Install Google places SDK

-Import谷歌地圖上您的課

import GoogleMaps 

- 添加上一流的地方客戶

var placesClient: GMSPlacesClient? 

-init您的地方客戶可能對您的viewDidLoad()

placesClient = GMSPlacesClient() 

- 和在你需要它來搜索當前位置

添加以下代碼
placesClient!.currentPlaceWithCallback({ (placeLikelihoods, error) -> Void in 
    if error != nil { 
    print("Current Place error: \(error.localizedDescription)") 
    return 
    } 

    for likelihood in placeLikelihoods.likelihoods { 
    if let likelihood = likelihood as? GMSPlaceLikelihood { 
     let place = likelihood.place 
     print("Current Place name \(place.name) at likelihood \(likelihood.likelihood)") 
     print("Current Place address \(place.formattedAddress)") 
     print("Current Place attributions \(place.attributions)") 
     print("Current PlaceID \(place.placeID)") 
    } 
    } 
}) 

Notes about the likelihood values:

The likelihood provides a relative probability of the place being the best match within the list of returned places for a single request. You can't compare likelihoods across different requests. The value of the likelihood will be between 0 and 1.0. The sum of the likelihoods in a returned array of GMSPlaceLikelihood objects is always less than or equal to 1.0. Note that the sum isn't necessarily 1.0. For example, to represent a 55% likelihood that the correct place is Place A, and a 35% likelihood that it's Place B, the likelihood array has two members: Place A with a likelihood of 0.55 and Place B with a likelihood of 0.35.

您還可以使用GMSPlacePicker,這可以幫助很多,如果你想你的機會用戶有機會選擇他們當前的位置。

+0

嘿弗朗西斯科 - 非常感謝你您的幫助;這是我尋找的解決方案。我正在運行Xcode 7.2,並且我發現我必須稍微更改一下語法以使其起作用 –

+0

謝謝我更新了語法以使用swift 2.0和Xcode 7.2 –

0

更進一步Francisco的非常有幫助的答案,這是我必須對currentPlaceWithCallback做出的調整 - 我刪除了:GMSPlaceLikelihoodList!這給了以下錯誤:無法轉換類型GMSPlaceLikelihoodList預期參數類型的值....

這是糾正代碼:

placesClient!.currentPlaceWithCallback({ (placeLikelihoods, error) -> Void in 
     if error != nil { 
      print("Current Place error: \(error!.localizedDescription)") 
      return 
     } 

     for likelihood in placeLikelihoods!.likelihoods { 
      if let likelihood = likelihood as? GMSPlaceLikelihood { 
       let place = likelihood.place 
       print("Current Place name \(place.name) at likelihood \(likelihood.likelihood)") 
       print("Current Place address \(place.formattedAddress)") 
       print("Current Place attributions \(place.attributions)") 
       print("Current PlaceID \(place.placeID)") 


      } 
     } 

})

+0

已更新我的答案,以便將這些更改包含在語法中 –