我正在使用谷歌地图显示当前位置以及用户能够在此地图上搜索和选择自动填充建议位置。我想要的另一件事是允许用户从他们当前的位置获取他们选择的标记并点击。有关我如何实现这一点的任何建议?我已经上传了GoogleDirectionsAPI,但我很困惑接下来...我已经看到了适合特定地点或坐标的代码,而我希望那里不是设定的目的地,但无论目的地是由自动完成创建的。
由于
我的自动填充谷歌地图搜索代码:
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)
}
}
答案 0 :(得分:0)
我建议你实施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()
}