Easy and fast way to get places by query in googleMaps API

1.2k Views Asked by At

I'm just doing a rebuild of my iOS App in Android, here and there horrible to do. One horrible part is that map thing.

I need to get places around users location by query like "park", "cafe", "bakery" etc. In swift I just used localSearchRequest.naturalLanguageQuery

self.localSearchRequest = MKLocalSearchRequest()
self.localSearchRequest.naturalLanguageQuery = mapSearchQuery
self.localSearchRequest.region = region
self.localSearch = MKLocalSearch(request: self.localSearchRequest)    

for item in localSearchResponse!.mapItems
     let annotation = MKPointAnnotation()
     annotation.coordinate = item.placemark.coordinate
     annotation.title = item.name
     self.mapView.addAnnotation(annotation)
}

Is there a similar way to do the same in Android by using GoogleMaps API? The only way I found was to get them via JSON from https://developers.google.com/places/web-service/search and I'm not even sure if this is for Android applications.

The GooglePlaces API for Android just list all places around a location without a way to filter them or something.

2

There are 2 best solutions below

0
kuemme01 On BEST ANSWER

after long trying I was going with the following solution. Using the GooglePlaces webservice (link in my question):

final String PLACES_API_BASE_URL = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?";
    final String LOCATION_PARAM = "location";
    final String RADIUS_PARAM = "radius";
    final String KEYWORD_PARAM = "keyword";
    final String LANGUAGE_PARAM = "language";
    final String KEY_PARAM = "key";

    mDestinationUri = Uri.parse(PLACES_API_BASE_URL).buildUpon()
            .appendQueryParameter(LOCATION_PARAM, String.valueOf(latitude)+","+String.valueOf(longitude))
            .appendQueryParameter(RADIUS_PARAM, "1000")
            .appendQueryParameter(KEYWORD_PARAM, naturalLanguageQuery)
            .appendQueryParameter(KEY_PARAM, "YOUR_API_KEY")
            .build();

If this will accept from google we will see.

1
Teyam On

For better distinction, usage of the following APIs for Android are as follows:

  • Google Maps Android API to add maps to your Android app. Integrate base maps, 3D buildings, indoor floor plans, Street View and Satellite imagery, custom markers and more.
  • Google Places API for Android to implement device place detection, auto-complete and add information about millions of locations to your app.

And to get places around users location, try Current Place.

You can call PlaceDetectionApi.getCurrentPlace() to find the local business or other place where the device is currently located. You can optionally specify a PlaceFilter to restrict the results to one or more place IDs (up to a maximum of 10), or to select only places that are currently open. If no filter is specified, the results are not filtered.

The following code sample retrieves the list of places where the device is most likely to be located, and logs the name and likelihood for each place.

PendingResult<PlaceLikelihoodBuffer> result = Places.PlaceDetectionApi
    .getCurrentPlace(mGoogleApiClient, null);
result.setResultCallback(new ResultCallback<PlaceLikelihoodBuffer>() {
  @Override
  public void onResult(PlaceLikelihoodBuffer likelyPlaces) {
    for (PlaceLikelihood placeLikelihood : likelyPlaces) {
      Log.i(TAG, String.format("Place '%s' has likelihood: %g",
          placeLikelihood.getPlace().getName(),
          placeLikelihood.getLikelihood()));
    }
    likelyPlaces.release();
  }
});

Please try going through the given documentations for more information such as regarding permissions and usage limits in using this API.