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

☆樱花仙子☆ 提交于 2019-12-05 03:08:57
Surjeet Rajput

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.

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) 

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

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
                }

            }
        }
for ( GMSAddressComponent * component in place.addressComponents) {
    if ( [component.type  isEqual: @"locality"] )
    {
        NSLog(@"Current city %@ ", component.name);
    }
}
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)
Jose carlos Camacho Alzar

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