Pyae Phyoe Shein
Pyae Phyoe Shein

Reputation: 13797

How to get Google Place Details value in iOs

I'm now got stuck that I cannot retrieve how to fetch value from below json of Google Place Detail API.

I want to get all data from "types" node.

{
    "address_components" =     (
                {
            "long_name" = "Bridge Street";
            "short_name" = "Bridge St";
            types =             (
                route
            );
        },
                {
            "long_name" = London;
            "short_name" = London;
            types =             (
                locality,
                political
            );
        },
                {
            "long_name" = London;
            "short_name" = London;
            types =             (
                "postal_town"
            );
        },
                {
            "long_name" = "Greater London";
            "short_name" = "Greater London";
            types =             (
                "administrative_area_level_2",
                political
            );
        },
                {
            "long_name" = "United Kingdom";
            "short_name" = GB;
            types =             (
                country,
                political
            );
        },
                {
            "long_name" = "SW1A 2LW";
            "short_name" = "SW1A 2LW";
            types =             (
                "postal_code"
            );
        }
    );
    vicinity = London;
}

Upvotes: 0

Views: 1036

Answers (2)

Milan Rathod
Milan Rathod

Reputation: 336

You can use google places api. Use place id and use below code to get place details value.

-(id)getAddressFromPlaceId:(NSString*)placeId
{

NSString *placeRequest;

if ([AppDelegate getInstance].isDetailsLimitReached)
{
    placeRequest = [[NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/details/json?placeid=%@&key=%@&language=en",placeId,[Utility getInstance].arrGooglkey[tempc]] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

}
else
{
    placeRequest = [[NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/details/json?placeid=%@&language=en",placeId] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
}

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:placeRequest]cachePolicy:NSURLRequestUseProtocolCachePolicy
                                     timeoutInterval:20.0];
NSError *error;
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];
if (error)
{
    return nil;
}
else
{
    NSDictionary *resData = [NSJSONSerialization JSONObjectWithData:response options:0 error:&error];
    if (error)
    {
        return nil;
    }
    else
    {
        if ([resData[@"status"] isEqualToString:@"OK"] || [[resData objectForKey:@"status"] isEqualToString:@"ZERO_RESULTS"]) {

            return [resData[@"result"] mutableCopy];
        }
        else
        {
            if ([AppDelegate getInstance].isDetailsLimitReached)
            {
                return nil;
            }
            [AppDelegate getInstance].isDetailsLimitReached = TRUE;
            return [self getAddressFromPlaceId:placeId];
        }
    }

}
return nil;

}

Upvotes: 0

Mihir Mehta
Mihir Mehta

Reputation: 13833

Why not integrate Google places API itself rather than parsing yourself.

It's simple and easy to retrieve places

here is the link

https://developers.google.com/places/ios-api/

Upvotes: 2

Related Questions