Next Previous

Places API Search Nearby Places

342

Nearby search is part of the Google Places API Library.

Nearby Search request allows us to search for different types of places such as restaurants, grocery stores, etc close by to the user’s location.

✅Get User Location using HTML5 Geolocation in the form of Latitude and Longitude.

 navigator.geolocation.getCurrentPosition(
     position => {
         console.log(position.coords.latitude);
         console.log(position.coords.longitude);
     },
     error => {
         console.log(error.message);
     })

✅ Then, make an HTTP request to Nearby Search Library.

Base URL

https://maps.googleapis.com/maps/api/place/nearbysearch/

Required URL Parameter

namedescription
JSON or XMLoutput format (json recommended)

Required Query Parameters

namedescription
keyYour API Key from Google Cloud Platform. Find how to get it here
locationThe value must be specified in the format of latitude, longitude
radiusSpecify in meters. i.e. 5000 = 5KM
typeThis could be a restaurant, bar, etc. full list here

Optional Query Parameters

namevaluedescription
rankbydistanceThis will sort places in asending order by their distance. When using rankby, radius option can’t be used.

Here is a sample URL with the required parameters:

https://maps.googleapis.com/maps/api/place/nearbysearch/json?key=[YOURAPIKEY]&location=-33.8670522,151.1957362&radius=5000&type=restaurant

The returned JSON object will look like this:

Next Previous