gpt4 book ai didi

ios - 如何在 google place picker api 中 swift 将国家与地址分开

转载 作者:行者123 更新时间:2023-11-28 09:36:32 24 4
gpt4 key购买 nike

我正在开发使用 google place picker API 获取地址的应用程序。

placePicker.pickPlaceWithCallback { (place: GMSPlace?, error: NSError?) -> Void in

if let error = error {
print("Error occurred: \(error.localizedDescription)")
return
}
// 3
if let place = place {
let coordinates = CLLocationCoordinate2DMake(place.coordinate.latitude, place.coordinate.longitude)
let marker = GMSMarker(position: coordinates)
marker.title = place.name
marker.map = self.googleMapView
self.googleMapView.animateToLocation(coordinates)
self.delegate?.userAddress(place.formattedAddress! )
self.navigationController!.popViewControllerAnimated(true)

} else {
Print(@"Place name %@", place.name);
Print(@"Place address %@", place.formattedAddress);
Print(@"Place placeID %@", place.placeID);
Print(@"Place attributions %@", place.attributions);
}
}

在这里我得到了完整的地址,包括国家、州和城市。

我想将国家/地区和城市与地址分开我没有找到任何解决方案。

如有任何帮助,我们将不胜感激。

最佳答案

在 google place picker api 中快速检查以下代码以获取与地址不同的国家/地区。它对我有用!!!

    placesClient?.currentPlaceWithCallback({
(placeLikelihoodList: GMSPlaceLikelihoodList?, error: NSError?) -> Void in
if let error = error {
print("Pick Place error: \(error.localizedDescription)")
return
}
if let placeLikelihoodList = placeLikelihoodList {
let place = placeLikelihoodList.likelihoods.first?.place
if let place = place {

self.mapView.removeAnnotations(self.mapView.annotations)

let span = MKCoordinateSpanMake(0.05, 0.05)
let region = MKCoordinateRegion(center: locValue, span: span)
self.mapView.setRegion(region, animated: true)

let annotation = MKPointAnnotation()
annotation.coordinate = locValue
annotation.title = place.name
annotation.subtitle = place.formattedAddress!.componentsSeparatedByString(", ").joinWithSeparator(",")
self.mapView.addAnnotation(annotation)

let arrays : NSArray = place.addressComponents!;
for i in 0..<arrays.count {

let dics : GMSAddressComponent = arrays[i] as! GMSAddressComponent
let str : NSString = 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)")
}
}
}
}
})

关于ios - 如何在 google place picker api 中 swift 将国家与地址分开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38800739/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com