首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Google directions for autocompleted

Google directions for autocompleted
EN

Stack Overflow用户
提问于 2016-12-14 00:21:32
回答 1查看 810关注 0票数 0

我正在使用谷歌地图,它显示了当前的位置,以及用户能够搜索和选择从自动完成的建议在这张地图上的地点。我想要的下一件事是允许用户获得从他们当前位置到他们选择并点击的标记的方向。对我如何实现这一点有什么建议吗?我已经上传了GoogleDirectionsAPI,但我不知道下一步要做什么……我已经看到了容纳特定地点或坐标的代码,而我不希望有一个设定的目的地,而是无论目的地是由自动完成创建的。

谢谢

我的自动补全google地图搜索代码:

代码语言:javascript
复制
   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)

}

  }
EN

回答 1

Stack Overflow用户

发布于 2016-12-14 03:28:13

我将建议您实现用于搜索位置的google api。

代码语言:javascript
复制
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()
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41125809

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档