2017-02-10 4 views
0

住所をGoogleマップに検索してジオコードするたびに、検索を2回実行するまで地図にマーカーが追加されません。誰が私にそれがなぜあるか教えてもらえますか? 基本的には、アドレスを検索し、その名前をサードパーティのジオコーダに入力し、マップ上に作成したグローバルマーカーを変更します。これは、マップ上に一度にマーカーが1つしかないためです。しかし何らかの理由で、最初にアドレスを検索するときにマーカーを置くことはありません。別のアドレスを検索する必要があります。変わったことは、同じアドレスである必要はないということです。別のアドレスを入力すると、マーカーのタイトルが変更され、元の場所に配置されます。Swiftのジオコーディングマーカー

マイコード:

extension EventCreatorVC: GMSAutocompleteResultsViewControllerDelegate { 
func resultsController(_ resultsController: GMSAutocompleteResultsViewController, 
         didAutocompleteWith place: GMSPlace) { 
    searchController?.isActive = false 
    // Do something with the selected place. 
    print("Place name: \(place.name)") 
    print("Place address: \(place.formattedAddress)") 
    print("Place attributions: \(place.attributions)") 

    Nominatim.getLocation(fromAddress: place.name, completion: {(error, location) -> Void in 

     let latitude = (location!.latitude as NSString).doubleValue 
     let longitude = (location!.longitude as NSString).doubleValue 

     self.locationOfMarker = CLLocationCoordinate2D(latitude: latitude, longitude: longitude) 

    }) 

    markerPlace(locationOfMarker: locationOfMarker, name: place.name) 

} 

func markerPlace(locationOfMarker: CLLocationCoordinate2D, name: String) { 
    marker.position = locationOfMarker 
    marker.title = name 


} 

func resultsController(_ resultsController: GMSAutocompleteResultsViewController, 
         didFailAutocompleteWithError error: Error){ 
    // TODO: handle the error. 
    print("Error: ", error.localizedDescription) 
} 

// Turn the network activity indicator on and off again. 
func didRequestAutocompletePredictions(forResultsController resultsController: GMSAutocompleteResultsViewController) { 
    UIApplication.shared.isNetworkActivityIndicatorVisible = true 
} 

func didUpdateAutocompletePredictions(forResultsController resultsController: GMSAutocompleteResultsViewController) { 
    UIApplication.shared.isNetworkActivityIndicatorVisible = false 
} 
} 
+0

完了マーカー部分にマーカーラインを挿入してください。ブロックごとに、結果が見つかると完了部分に戻ります。だからデバッグモードで試してみてください。マーカスラインが最初に走ってから完了部分に行くと思います。 –

+0

どういう意味なのか分かりません。どこに行を置くべきですか? – Willstarr

+0

私の答えを確認 –

答えて

1

今、あなたの問題は終わっ取得に時間がかかる、あなたのcompeletionの一部のようなものです。だから私はあなたのコードにこの変更を遅らせるようにします

Nominatim.getLocation(fromAddress: place.name, completion: {(error, location) -> Void in 

       let latitude = (location!.latitude as NSString).doubleValue 
       let longitude = (location!.longitude as NSString).doubleValue 

       self.locationOfMarker = CLLocationCoordinate2D(latitude: latitude, longitude: longitude) 
       let delayTime = DispatchTime.now() + Double(Int64(0.5 * Double(NSEC_PER_SEC)))/Double(NSEC_PER_SEC) 
       DispatchQueue.main.asyncAfter(deadline: delayTime) 
       { 
        self.markerPlace(locationOfMarker: self.locationOfMarker, name: "") 
        self.mapView.animate(toLocation: self.locationOfMarker) 
       } 
     }) 

方法を変更して問題を解決できることを確認してください。
ご協力いただきありがとうございます

+0

ありがとうございました!非常に興味深い解決策 – Willstarr

+0

あなたは私の答えを受け入れるでしょうか?してください –

+0

私はハングアウトで招待状を送信、私たちはあなたの仕事のためにチャットする私の招待を受け入れてください –