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

亡梦爱人 提交于 2019-12-12 09:37:57

问题


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 as city to store in a database.

The documentation indicates a GMSPlace has an addressComponents property (an array), but I can't seem to figure how to use this property.

The code below provides sets the entire address to the text of a label, but I can't get beyond that:

Edit ---- added code that shows how I'm trying to access Address Components

venueLabel.isHidden = false
            venueLabel.text = selectedPlace?.name
            addressLabel.isHidden = false
            addressLabel.text = selectedPlace?.formattedAddress
            let addressComponents = selectedPlace?.addressComponents
            for component in addressComponents! {
                let city = component["city"] //Error Type GMSPaceAddressComponent has no subscript members
                print(city)
            }

回答1:


A safe Swift 4.2 solution:

let name = place.addressComponents?.first(where: { $0.type == "city" })?.name

selectedPlace.addressComponents is a array of GMSAddressComponent which have 2 properties type and name. In you case it will be like

    for component in addressComponents! {
    if component.type == "city" {
    print(component.name)
    }
   }

GMSAddressComponent is a class not a dictionary or array that's you are getting this error.

Additional component types can be referred from the link.




回答2:


Swift 4

    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(values) 



回答3:


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);
}



回答4:


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
                }

            }
        }



回答5:


for ( GMSAddressComponent * component in place.addressComponents) {
    if ( [component.type  isEqual: @"locality"] )
    {
        NSLog(@"Current city %@ ", component.name);
    }
}



回答6:


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)



回答7:


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
            }


来源:https://stackoverflow.com/questions/44112982/how-to-extract-street-city-etc-from-gmsplace-address-components

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!