0
私は現在地を表示するGoogleマップと、ユーザーがこのマップ上の自動補完候補の場所を検索して選択できるようにしています。私が望む次のことは、ユーザーが自分の現在地から選択してタップするマーカーまでの道順を取得できるようにすることです。私はこれをどのように実装するかに関する提案はありますか?私はGoogleDirectionsAPIをアップロードしましたが、次に混乱しています...特定の場所や座標に対応したコードを見ましたが、設定された目的地がなくても目的地がオートコンプリートで作成されていて欲しいと思います。Googleのオートコンプリート検索の方法Swift
おかげ
オートコンプリートGoogleマップの検索のための私のコードは:
func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) {
let camera = GMSCameraPosition.camera(withLatitude: place.coordinate.latitude, longitude: place.coordinate.longitude, zoom: 15.0)
self.vwGMap.camera = camera
let marker = GMSMarker()
marker.position = CLLocationCoordinate2DMake(place.coordinate.latitude, place.coordinate.longitude)
marker.title = place.name
marker.snippet = place.formattedAddress
marker.map = self.vwGMap
marker.icon = GMSMarker.markerImage(with: UIColor.blue)
marker.tracksViewChanges = true
self.dismiss(animated: true, completion: nil)
print("Place name: ", place.name)
print("Place address: ", place.formattedAddress)
print("Place placeID: ", place.placeID)
print("Place attributions: ", place.attributions)
print("Place Coordinate:", place.coordinate)
//The printing only happens in the terminal
let newPlace = StoredPlace(name: place.name, address: place.formattedAddress!, placeID: place.placeID)
storedPlaces.append(newPlace)
}
func viewController(_ viewcontroller: GMSAutocompleteViewController, didFailAutocompleteWithError error: Error) {
print("ERROR AUTO COMPLETE \(error)")
}
func wasCancelled(_ viewController: GMSAutocompleteViewController) {
self.dismiss(animated: true, completion: nil)
}
@IBAction func searchWithAddress(_ sender: AnyObject) {
let searchWithAddress = GMSAutocompleteViewController()
searchWithAddress.delegate = self
let filter = GMSAutocompleteFilter()
filter.type = .establishment
filter.country = "UK"
self.locationManager.startUpdatingLocation()
self.present(searchWithAddress, animated: true, completion: nil)
}
}
ご回答ありがとうございます。私はすでにGoogleの場所でオートコンプリートウィジェットを使って場所を検索しています。しかし今、私はgooglemapsdirectionsAPIで行われていることを理解する限り、それは正しいですか?病気はオートコンプリートセクションのための私のコードを表示するために私のポストを修正します。現在地から自宅までの道順を得るためにここからどこに行くのですか? –