2016-12-13 28 views
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) 

} 

    } 

答えて

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() 
} 
+0

ご回答ありがとうございます。私はすでにGoogleの場所でオートコンプリートウィジェットを使って場所を検索しています。しかし今、私はgooglemapsdirectionsAPIで行われていることを理解する限り、それは正しいですか?病気はオートコンプリートセクションのための私のコードを表示するために私のポストを修正します。現在地から自宅までの道順を得るためにここからどこに行くのですか? –

関連する問題