如何在swift中的google place picker api中将国家/地区与地址分开

0 xcode google-maps ios google-places-api swift

我正在开发应用程序,我正在使用谷歌地方选择器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);
            }
        }
Run Code Online (Sandbox Code Playgroud)

在这里,我正在获得包括国家,州和城市在内的完整地址.

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

任何帮助,将不胜感激.

小智 7

请检查以下代码,以便在swift中的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)")
                    }
                }
            }
        }
    })
Run Code Online (Sandbox Code Playgroud)