【发布时间】:2019-01-10 09:58:12
【问题描述】:
更新 - 找到了解决方案,如果有人在寻找相同的解决方案,这里是示例项目。这将从坐标和附近位置获取城市、国家、邮政编码和 LocalAdress(subLocality)。 https://github.com/SwiftGuides/Google_Place_Picker
我在我的项目中实现了 google place picker API,以便我可以移动选择器并获取附近的位置以及我的选择器所在街道的确切位置。
我想要地点选择器自定义位置的地址,例如(未命名的道路,Panchkula,印度),而不是(37°19'1464"N 122°01'74.724"W)。
在android google place picker API中,当我们单击“选择此地址”以获取自定义位置时,它会显示一个弹出窗口并以字符串格式设置位置(未命名的道路,Panchkula,印度)而不是(37°19'1464“N 122°01'74.724"W)
我想要这样的东西
这是当前状态的图像
请帮忙!!
import UIKit
import GooglePlaces
import GooglePlacePicker
import GoogleMaps
class ViewController: UIViewController,GMSPlacePickerViewControllerDelegate {
@IBOutlet weak var addressLabel: UILabel!
@IBOutlet weak var placeNameLabel: UILabel!
@IBOutlet weak var coordinatesLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
// To receive the results from the place picker 'self' will need to conform to
// GMSPlacePickerViewControllerDelegate and implement this code.
func placePicker(_ viewController: GMSPlacePickerViewController, didPick place: GMSPlace) {
// Dismiss the place picker, as it cannot dismiss itself.
viewController.dismiss(animated: true, completion: nil)
print("Place name \(place.name)")
print("PostalCode\(place.addressComponents)")
print("Place address \(place.formattedAddress)")
print("Place attributions \(place.attributions)")
placeNameLabel.text = place.formattedAddress
//Get address Seperated like city,country,postalcode
let arrays : Array = place.addressComponents!
for i in 0..<arrays.count {
let dics : GMSAddressComponent = arrays[i]
let str : String = dics.type
if (str == "country") {
print("Country: \(dics.name)")
}
else if (str == "administrative_area_level_1") {
print("State: \(dics.name)")
}
else if (str == "administrative_area_level_2") {
print("City: \(dics.name)")
}
else if (str == "postal_code"){
print("PostalCode:\(dics.name)")
addressLabel.text = dics.name // this is only to get postalcode of the selected nearby location
}
}
}
func placePickerDidCancel(_ viewController: GMSPlacePickerViewController) {
// Dismiss the place picker, as it cannot dismiss itself.
viewController.dismiss(animated: true, completion: nil)
print("No place selected")
}
@IBAction func pickPlaceButton(_ sender: Any) {
let config = GMSPlacePickerConfig(viewport: nil)
let placePicker = GMSPlacePickerViewController(config: config)
//This delegate has to be called here to proper working of cancle and selected location feature , it is not mentioned in the official documentation
placePicker.delegate = self
present(placePicker, animated: true, completion: nil)
}
}
【问题讨论】:
标签: swift google-maps google-places-api