Search code examples
iosclplacemark

How to get the map_region from CLPlaceMark


I have to take out the map_region and type from CLPlaceMark but I couldn't find a property which will give the map_region values in CLPlaceMark Class.

 {
        placeData =     {
            component =         (
                            {
                    "cache_control" = UNCACHEABLE;
                    "start_index" = 0;
                    status = "STATUS_SUCCESS";
                    type = "COMPONENT_TYPE_HOURS";
                    "values_available" = 0;
                },
                            {
                    "cache_control" = UNCACHEABLE;
                    "start_index" = 0;
                    status = "STATUS_SUCCESS";
                    type = "COMPONENT_TYPE_RATING";
                    "values_available" = 0;
                },
                            {
                    "cache_control" = UNCACHEABLE;
                    "start_index" = 0;
                    status = "STATUS_SUCCESS";
                    type = "COMPONENT_TYPE_FLYOVER";
                    "values_available" = 0;
                },
                            {
                    "cache_control" = UNCACHEABLE;
                    "start_index" = 0;
                    status = "STATUS_SUCCESS";
                    type = "COMPONENT_TYPE_BOUNDS";
                    value =                 (
                                            {
                            bounds =                         {
                                "map_region" =                             {
                                    eastLng = "-73.832921";
                                    northLat = "40.739434";
                                    southLat = "40.550334";
                                    westLng = "-74.056687";
                                };
                            };
                        }
                    );
                    "values_available" = 1;
                },
                            {
                    "cache_control" = UNCACHEABLE;
                    "start_index" = 0;
                    status = "STATUS_SUCCESS";
                    type = "COMPONENT_TYPE_ROAD_ACCESS_INFO";
                    "values_available" = 0;
                },
                            {
                    "cache_control" = UNCACHEABLE;
                    "start_index" = 0;
                    status = "STATUS_SUCCESS";
                    type = "COMPONENT_TYPE_PLACE_INFO";
                    value =                 (
                                            {
                            "place_info" =                         {
                                center =                             {
                                    lat = "40.692529";
                                    lng = "-73.990996";
                                };
                                timezone =                             {
                                    identifier = "America/New_York";
                                };
                            };
                        }
                    );
                    "values_available" = 1;
                },
                            {
                    "cache_control" = UNCACHEABLE;
                    "start_index" = 0;
                    status = "STATUS_SUCCESS";
                    type = "COMPONENT_TYPE_ENTITY";
                    value =                 (
                                            {
                            entity =                         {
                                "is_disputed" = 0;
                                name =                             (
                                                                    {
                                        locale = "en_US";
                                        "string_value" = Brooklyn;
                                    }
                                );
                                type = "SUB_LOCALITY";
                            };
                        }
                    );
                    "values_available" = 1;
                },
                            {
                    "cache_control" = UNCACHEABLE;
                    "start_index" = 0;
                    status = "STATUS_SUCCESS";
                    type = "COMPONENT_TYPE_ADDRESS";
                    value =                 (
                                            {
                            address =                         {
                                "localized_address" =                             (
                                                                    {
                                        address =                                     {
                                            formattedAddressLine =                                         (
                                                "Brooklyn, NY",
                                                "United States"
                                            );
                                            structuredAddress =                                         {
                                                administrativeArea = "New York";
                                                administrativeAreaCode = NY;
                                                areaOfInterest =                                             (
                                                    "Long Island"
                                                );
                                                country = "United States";
                                                countryCode = US;
                                                geoId =                                             (
                                                );
                                                locality = Brooklyn;
                                                subAdministrativeArea = Kings;
                                            };
                                        };
                                        locale = "en_US";
                                    }
                                );
                            };
                        }
                    );
                    "values_available" = 1;
                },
                            {
                    "cache_control" = UNCACHEABLE;
                    "start_index" = 0;
                    status = "STATUS_SUCCESS";
                    type = "COMPONENT_TYPE_AMENITIES";
                    "values_available" = 0;
                },
                            {
                    "cache_control" = UNCACHEABLE;
                    "start_index" = 0;
                    status = "STATUS_SUCCESS";
                    type = "COMPONENT_TYPE_STYLE_ATTRIBUTES";
                    "values_available" = 0;
                },
                            {
                    "cache_control" = UNCACHEABLE;
                    "start_index" = 0;
                    status = "STATUS_SUCCESS";
                    type = "COMPONENT_TYPE_BUSINESS_CLAIM";
                    "values_available" = 0;
                }
            );
            "result_provider_id" = 6489;
            status = "STATUS_SUCCESS";
        };
    }

Is there any way to read the values from CLPlaceMark class ?


Solution

  • CLPlacemark does not have any properties to access it's underlaying storage data (private GEOMapItemStorage). There is however a way how to access these data although it's a hacky way and you should use it very carefully.

    When you try to print your CLPlacemark instance console logs also GEOMapItemStorage so theoretically you can access this string and convert it into a dictionary. Tricky part is that it's a raw string and you have to process it little bit. I am posting an extension to CLPlacemark that should does the job and returns [String: AnyObject]?.

    extension CLPlacemark {
    
        enum GeoMapItemStorageError: ErrorType {
            case InvalidFormat
            case DataEncoding
            case Serialization(underlaying: ErrorType)
        }
    
        func extractGeoMapItemStorage() throws -> [String: AnyObject]? {
    
            let description = self.description as NSString
            let indexOfBracket = description.rangeOfString("{", options: NSStringCompareOptions.init(rawValue: 0))
    
            guard indexOfBracket.location != NSNotFound else {
                throw GeoMapItemStorageError.InvalidFormat
            }
    
            let dictString = description.substringFromIndex(indexOfBracket.location)
    
            guard let dictStringData = dictString.dataUsingEncoding(NSUTF8StringEncoding) else {
                throw GeoMapItemStorageError.DataEncoding
            }
    
            do {
                return try NSPropertyListSerialization.propertyListWithData(
                    dictStringData,
                    options: NSPropertyListReadOptions.Immutable,
                    format: nil
                    ) as? [String: AnyObject]
            }
            catch (let error){
                throw GeoMapItemStorageError.Serialization(underlaying: error)
            }
    
        }
    }