我正在使用谷歌地图,它显示了当前的位置,以及用户能够搜索和选择从自动完成的建议在这张地图上的地点。我想要的下一件事是允许用户获得从他们当前位置到他们选择并点击的标记的方向。对我如何实现这一点有什么建议吗?我已经上传了GoogleDirectionsAPI,但我不知道下一步要做什么……我已经看到了容纳特定地点或坐标的代码,而我不希望有一个设定的目的地,而是无论目的地是由自动完成创建的。
谢谢
我的自动补全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)
}
}发布于 2016-12-14 03:28:13
我将建议您实现用于搜索位置的google api。
func hitForPlace(searchText:String)
{
arrPlaceList.removeAll()
viewLine.hidden = false
let url:NSURL = NSURL(string: "https://maps.googleapis.com/maps/api/place/autocomplete/json?input=\(searchText)&sensor=true&key=\(googleMapApiKey)")!
let task = NSURLSession.sharedSession().dataTaskWithURL(url) { (data, response, error) -> Void in
do {
if data != nil{
let dic = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableLeaves) as! NSDictionary
print_debug(dic)
for var count = 0;count<dic["predictions"]?.count;count = count+1
{
self.arrPlaceList.append(dic["predictions"]![count]["description"] as! String)
}
dispatch_async(dispatch_get_main_queue(), {
self.placeTableShow(true)
self.tablePlace.reloadData()
});
}
}catch {
print("Error")
}
}
task.resume()
}https://stackoverflow.com/questions/41125809
复制相似问题