Find Nearby Places with Geoapify API

Explore nearby places using the Geoapify API. This JavaScript code fetches data about various categories of locations, such as schools, parks, supermarkets, and restaurants, within specified radii of a given latitude and longitude. Customize your search preferences and discover the closest places based on categories or proximity. Note that the API key provided in this code is restricted to use within JSFiddle; for unrestricted access, obtain your API key from the Geoapify website.

by Geoapify

HTML

<label for="placesData">Places nearby:</label>

<div>
  <textarea id="placesData" name="placesData" rows="15" cols="50" /></textarea>
</div>

JavaScript

// The API Key provided is restricted to JSFiddle website
// Get your own API Key on https://myprojects.geoapify.com
const myAPIKey = "6dc7fb95a3b246cfa0f3bcef5ce9ed9a";

// define required categories
const placesInfo = [{
    categories: ["populated_place.hamlet", "populated_place.village", "populated_place.town", "populated_place.city"],
    type: 'closest',
    maxRadius: 5000
  },
  {
    categories: ["education.school", "childcare.kindergarten"],
    type: 'closest_by_type',
    maxRadius: 3000
  },
  {
    categories: ["leisure.park", "leisure.playground"],
    type: 'closest_by_type',
    maxRadius: 2000
  },
  {
    categories: ["public_transport.train", "public_transport.light_rail", "public_transport.monorail", "public_transport.subway", "public_transport.bus", "public_transport.tram", "public_transport.ferry"],
    type: 'closest_by_type',
    maxRadius: 2000
  },
  {
    key: "restaurants_1000",
    categories: ["catering.restaurant"],
    type: 'number',
    maxRadius: 1000
  },
  {
    key: "restaurants_5000",
    categories: ["catering.restaurant"],
    type: 'number',
    maxRadius: 5000
  },
  {
    key: "supermarket_1000",
    categories: ["commercial.supermarket"],
    type: 'number',
    maxRadius: 1000
  },
  {
    key: "supermarket_5000",
    categories: ["commercial.supermarket"],
    type: 'number',
    maxRadius: 5000
  }
]

// get property data
getPropertyClosestPlaces(49.2505929, 4.0616133, placesInfo).then(propertyData => {
  console.log(propertyData);
  document.getElementById('placesData').value = JSON.stringify(propertyData, null, "\t");

});


function getPropertyClosestPlaces(lat, lon, placesInfo) {
  const placesPromises = placesInfo.map(info => {

    let placesUrl = `https://api.geoapify.com/v2/places?categories=${info.categories.join(",")}&bias=proximity:${lon},${lat}&filter=circle:${lon},${lat},${info.maxRadius}&apiKey=${myAPIKey}`;

    if (info.type === 'closest') {
      placesUrl += '&limit=1'
    }

    return...