How to extract street, city, etc. from GMSPlace Address Components

后端 未结 10 1899
栀梦
栀梦 2021-02-20 02:51

I\'m using Google Places API for iOS and can successfully retrieve nearby places and present the address as a string. What I\'m trying to do is extract address components such

相关标签:
10条回答
  • 2021-02-20 03:35

    I will extend @Ramis answer, in any case you want to check for multiple types if one of them exist, e.x

    addressComponents?.valueFor(placeTypes: kGMSPlaceTypePostalTown, kGMSPlaceTypeLocality)
    

    implementation

    extension Array where Element == GMSAddressComponent {
        func valueFor(placeTypes: String..., shortName: Bool = false) -> String? {
            let array = self as NSArray
            let result = array
                .compactMap { $0 as? GMSAddressComponent }
                .first(where: {
                    placeTypes.contains($0.types.first(where: { placeTypes.contains($0) }) ?? "")
                })
            return shortName ? result?.shortName : result?.name
        }
    }
    
    0 讨论(0)
  • 2021-02-20 03:35
    var keys = [String]()
        place.addressComponents?.forEach{keys.append($0.type)}
        var values = [String]()
        place.addressComponents?.forEach({ (component) in
            keys.forEach{ component.type == $0 ? values.append(component.name): nil}
    
             print("All component type \(component.type)")
    
           if component.type == "locality"{
                print("This is city name \(component.name)")
            }
        })
        print(values)
    
    0 讨论(0)
  • 2021-02-20 03:37

    I could not find this anywhere for Objective-C, for those who follow behind me:

    for (int i = 0; i < [place.addressComponents count]; i++)
    {
        NSLog(@"name %@ = type %@", place.addressComponents[i].name, place.addressComponents[i].type);
    }
    
    0 讨论(0)
  • 2021-02-20 03:37

    I found this, I hope I help you

      let arrays : NSArray = place.addressComponents! as NSArray
            for i in 0..<arrays.count {
    
                let dics : GMSAddressComponent = arrays[i] as! GMSAddressComponent
                let str : NSString = dics.type as NSString
    
                if (str == "country") {
                    print("Country: \(dics.name)")
                    self.pais = dics.name
                }
    
                else if (str == "administrative_area_level_2") {
                    print("City: \(dics.name)")
                    self.ciudad = dics.name
                }
    
    0 讨论(0)
  • 2021-02-20 03:39
    for ( GMSAddressComponent * component in place.addressComponents) {
        if ( [component.type  isEqual: @"locality"] )
        {
            NSLog(@"Current city %@ ", component.name);
        }
    }
    
    0 讨论(0)
  • 2021-02-20 03:40

    For the latest Google Places API (July 2019) this is what could help you. Actually, Google now putting a couple of types for each element. So the "type" is now deprecated and "types" is new filed.

    You can do something like this:

     for addressComponent in (self.mySelectedPlace?.addressComponents)! {
                for type in (addressComponent.types){
    
                    switch(type){
                        case "country":
                            var country = addressComponent.name
    
                        case "postal_code":
                             var postCode = addressComponent.name
    
                    default:
                        break
                    }
    
                }
            }
    
    0 讨论(0)
提交回复
热议问题