ios - 谷歌在 swift 中放置自动完成功能并不完美
<p><p>我尝试从 google place 进行“地点自动完成”,我直接从网站复制粘贴代码 <a href="https://developers.google.com/places/ios-api/autocomplete#use_the_fetcher" rel="noreferrer noopener nofollow">google developer</a>我也已经设置了 apikey ,但问题是为什么当我输入一个地方时结果看起来很奇怪,正如你在 <a href="http://i.stack.imgur.com/ixf2l.png" rel="noreferrer noopener nofollow">picture</a> 上看到的那样它似乎有效但并不完美。这是我的代码:</p>
<pre><code>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: ) {
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
}
}
</code></pre>
<p>谁能帮帮我?</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>这是因为 <code>prediction.attributedPrimaryText</code> 是一个属性字符串。试试下面的代码</p>
<pre><code>func didAutocompleteWithPredictions(predictions: ) {
let resultsStr = NSMutableAttributedString()
for prediction in predictions {
resultsStr.appendAttributedString(prediction.attributedPrimaryText)
resultsStr.appendAttributedString(NSAttributedString(string: "\n"))
}
resultText?.attributedText = resultsStr
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - 谷歌在 swift 中放置自动完成功能并不完美,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/36248232/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/36248232/
</a>
</p>
页:
[1]