2017-12-22 8 views
-1

Instagramのような私の現在の場所に最も近い場所を取得する方法はありますか?私はCoreLocationを使って現在の場所を以下のコードで探します。今、このCLLocationインフォメーションクラスを使用して最も近い場所を取得する方法はありますか?または、他のどのオプションをこの機能を実装する必要がありますか?どんな提案も非常に役に立ちます。CoreLocationに最も近い場所を取得

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]){ 
    CLGeocoder().reverseGeocodeLocation(manager.location!, completionHandler: { (placemarks, error) -> Void in 
     if (error != nil){ 
      print("LoginView locationManager Error: \(error!.localizedDescription)"); 
      return 
     } 

     if (placemarks!.count > 0){ 
      let placemark = placemarks?[0] 

      let latitude = String(format: "%f", (placemark?.location?.coordinate.latitude)!) 
      let longitude = String(format: "%f", (placemark?.location?.coordinate.longitude)!) 
      guard let local = placemark?.locality else { return } 
      let uLocation = UserLocation(locality: local, longitude: longitude, latitude: latitude) 
      self.userLocations.append(uLocation) 
      self.locationContainer.setupData(locations: self.userLocations, delegate: self) 
      self.locationManager.stopUpdatingLocation() 
     } 
    }) 
    locationManager.stopUpdatingLocation() 
} 
+0

クエストを受ける前に、適切な研究開発を行う必要があります。ところで、あなたはGoogleの場所apiでそれを行うことができます。 –

答えて

0
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
    CLGeocoder().reverseGeocodeLocation(manager.location!, completionHandler: { (placemarks, error) -> Void in 
     if (error != nil){ 
      print("LoginView locationManager Error: \(error!.localizedDescription)"); 
      return 
     } 

     if (placemarks!.count > 0){ 
      let placemark = placemarks?[0] 

      let latitude = String(format: "%f", (placemark?.location?.coordinate.latitude)!) 
      let longitude = String(format: "%f", (placemark?.location?.coordinate.longitude)!) 
      self.addressDecoder.reverseGeocodeLocation(manager.location!, completionHandler: { 
       [unowned self] (result, error) in 
       guard let address = result else { return } 
       let nearbyName = address[0].name ?? address[0].locality 
       let uLocation = UserLocation(locality: local, longitude: longitude, latitude: latitude) 
       self.userLocations.append(uLocation) 
      }) 
      let uLocation = UserLocation(locality: local, longitude: longitude, latitude: latitude) 
      self.userLocations.append(uLocation) 
      self.locationContainer.setupData(locations: self.userLocations, delegate: self) 
      self.locationManager.stopUpdatingLocation() 
     } else { 
     } 
    }) 
    locationManager.stopUpdatingLocation() 
} 

私はアドレスが見つからないあなたが今それを持っているように、その後、地域で行く場合に??を使用。補完ブロック内でresultを使用して再生し、いずれかがあなたに魅力的である場合に備えて、利用可能な他のオプションを確認します。

よろしくお願いいたします。

+0

@ umutg4dこれを試しましたか? –

関連する問題