2016-03-27 20 views
2

私はgoogle placeから "place autocomplete"を作成しようとしましたが、ここでウェブサイトからコードを直接コピーしてコピーします。google developerと私は既にapiキーを設定しています。あなたがpictureで見ることができるように、私が場所を入力すると結果はとても奇妙に見えますが、それは完璧ではないようです。ここに私のコードです:Google place autocomplete in swiftは完全には機能しません

import UIKit 
import GoogleMaps 

class FetcherSampleViewController: UIViewController { 

    var textField: UITextField? 
    var resultText: UITextView? 
    var fetcher: GMSAutocompleteFetcher? 

    override func viewDidLoad() { 
    super.viewDidLoad() 

    self.view.backgroundColor = UIColor.whiteColor() 
    self.edgesForExtendedLayout = .None 

    // Set bounds to inner-west Sydney Australia. 
    let neBoundsCorner = CLLocationCoordinate2D(latitude: -33.843366, 
     longitude: 151.134002) 
    let swBoundsCorner = CLLocationCoordinate2D(latitude: -33.875725, 
     longitude: 151.200349) 
    let bounds = GMSCoordinateBounds(coordinate: neBoundsCorner, 
     coordinate: swBoundsCorner) 

    // Set up the autocomplete filter. 
    let filter = GMSAutocompleteFilter() 
    filter.type = .Establishment 

    // Create the fetcher. 
    fetcher = GMSAutocompleteFetcher(bounds: bounds, filter: filter) 
    fetcher?.delegate = self 

    textField = UITextField(frame: CGRect(x: 5.0, y: 0, 
     width: self.view.bounds.size.width - 5.0, height: 44.0)) 
    textField?.autoresizingMask = .FlexibleWidth 
    textField?.addTarget(self, action: "textFieldDidChange:", 
     forControlEvents: .EditingChanged) 

    resultText = UITextView(frame: CGRect(x: 0, y: 45.0, 
     width: self.view.bounds.size.width, 
     height: self.view.bounds.size.height - 45.0)) 
    resultText?.backgroundColor = UIColor(white: 0.95, alpha: 1.0) 
    resultText?.text = "No Results" 
    resultText?.editable = false 

    self.view.addSubview(textField!) 
    self.view.addSubview(resultText!) 
    } 

    func textFieldDidChange(textField: UITextField) { 
    fetcher?.sourceTextHasChanged(textField.text!) 
    } 

} 

extension FetcherSampleViewController: GMSAutocompleteFetcherDelegate { 
    func didAutocompleteWithPredictions(predictions: [GMSAutocompletePrediction]) { 
    let resultsStr = NSMutableString() 
    for prediction in predictions { 
     resultsStr.appendFormat("%@\n", prediction.attributedPrimaryText) 
    } 

    resultText?.text = resultsStr as String 
    } 

    func didFailAutocompleteWithError(error: NSError) { 
    resultText?.text = error.localizedDescription 
    } 
} 

誰でも助けてくれますか?

答えて

1

これは、prediction.attributedPrimaryTextが属性付き文字列であるためです。次のコードを試してください

func didAutocompleteWithPredictions(predictions: [GMSAutocompletePrediction]) { 
    let resultsStr = NSMutableAttributedString() 
    for prediction in predictions { 
     resultsStr.appendAttributedString(prediction.attributedPrimaryText) 
     resultsStr.appendAttributedString(NSAttributedString(string: "\n")) 
    } 
    resultText?.attributedText = resultsStr 
} 
+0

ありがとうございます。 –

関連する問題