Google Places API results not showing on map?

414 views Asked by At

So I have the Google Places API working (entering URL in browser displays results), yet none of the results are displayed on the actual map. Why is this? I am using the web key not the iOS which I know is a common error so its not that.

Code:

-(void) queryGooglePlaces: (NSString *) googleType {


    NSString* url = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=REMOVED FOR STACKOVERFLOW&radius=5000&types=atm&sensor=true&key=REMOVEDFORSTACKOVERFLOW"];

    //Formulate the string as a URL object.
    NSURL *googleRequestURL=[NSURL URLWithString:url];

    // Retrieve the results of the URL.
    dispatch_async(kBgQueue, ^{
        NSData* data = [NSData dataWithContentsOfURL: googleRequestURL];
        [self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:YES];

    });
}



-(void)fetchedData:(NSData *)responseData {
    //parse out the json data
    NSError* error;
    NSDictionary* json = [NSJSONSerialization
                          JSONObjectWithData:responseData

                          options:kNilOptions
                          error:&error];

    //The results from Google will be an array obtained from the NSDictionary object with the key "results".
    NSArray* places = [json objectForKey:@"results"];

    //Write out the data to the console.
    NSLog(@"Google Data: %@", places);
}
1

There are 1 answers

3
viggio24 On

The Google Places API can only be used to query for places in the huge Google database. However the map doesn't show all these places (there are millions of them) and the fact that you successfully query one of them doesn't mean it should be added in the map. So the solution for you is once you get the place coordinates you add a marker.

The fact that after a query on the Google Maps App you can see a marker with a representation of the place you queried is a feature of the Google Maps App which is built on top of the Google Places API + Google Map SDK and that after a query adds its own marker.

So the algorithm is: 1) do the search (Google Places API) 2) get the place coordinates and types (a place can have more than one type; the list is provided as a link in the GMSPlace class reference: https://developers.google.com/places/supported_types ) 3) add the right marker (depending on the type) on the right coordinate.

As a final note you will notice how the Google Map will still show some places even if you didn't look for: this is a Google choice, probably to show what are considered relevant places. But at the end you shouldn't think that what you see in the Google Maps App will be what you could see with the Google Maps SDK map: one is built on top of the other but it is difficult to spot at first glance where the information comes from (certainly Google used a coherent graphics between the app and the SDK!). The only for you to see this difference is to compare side by side a portion of the same map on the Google Maps app and the your app using the Google Maps SDK.