Home > OS >  Getting places nearby for a city city and not for a specific latlng
Getting places nearby for a city city and not for a specific latlng

Time:12-21

Is there a way to get all the towns around a specific city (radius of 10km) without using a single pair of latlng? I'd like to get all the towns around the city polygon in this radius (let's say there's a town in 8 km on the north, I can't just take the city center latlng and use it as it further than 10 km from this point).

How would you do it?

Thanks, J.

I tried to use plcae_id instead of latlng but it id not work.

CodePudding user response:

with the google map API try this: (10km distance) https://maps.googleapis.com/maps/api/place/autocomplete/json?input=**CITY_NAME**&radius=10000&types=(cities)&key=YOUR_API_KEY

You can take a look at this repository: https://github.com/drolbr/Overpass-API

(edit) example request with a python script:

import requests

api_key = "API_KEY"
query = "[out:json];area[name='Istanbul'][admin_level=2]->.a;(node[place=town](area.a);way[place=town](area.a);relation[place=town](area.a));out body;>;out skel qt;"

response = requests.get(
    "https://api.openstreetmap.org/api/interpreter",
    params={
        "data": query,
        "key": api_key
    }
)

data = response.json()

CodePudding user response:

Using the Places API, you can request a nearby search of x radius from a center latitude/longitude literal up to 50,000 meters.

For example, using JavaScript, we fetch the nearby search API and use (cities) as the type parameter to use the collection of cities including towns, etc. See https://developers.google.com/maps/documentation/places/web-service/supported_types for all the supported types.

GET https://maps.googleapis.com/maps/api/place/nearbysearch/json?key=API_KEY&location=-33.8670522,151.1957362&radius=8000&type=(cities)

const API_KEY = "API_KEY";

async function getNearbySearch(center, radius, type) {
  try {
    const response = await fetch(`https://maps.googleapis.com/maps/api/place/nearbysearch/json?key=${API_KEY}&location=${center.latitude},${center.longitude}&radius=${radius}&type=${type}`);
    const result = await response.json();
    
    return result;
  }
  catch(error) {
    console.error(error);
    
    return null;
  }
};

getNearbySearch({ latitude: -33.8670522, longitude: 151.1957362 }, 8000, "(cities)").then((nearbyCities) => {
  console.log(nearbyCities);
});

The response would look like: https://developers.google.com/maps/documentation/places/web-service/search-nearby#nearby-search-responses

Because of a bug with the (cities) type collection, we can instead query locality and administrative_area_level_3 to combine the cities manually instead.

const API_KEY = "API_KEY";

async function getNearbySearch(center, radius, type) {
  try {
    const response = await fetch(`https://maps.googleapis.com/maps/api/place/nearbysearch/json?key=${API_KEY}&location=${center.latitude},${center.longitude}&radius=${radius}&type=${type}`);
    const result = await response.json();
    
    return result;
  }
  catch(error) {
    console.error(error);
    
    return null;
  }
};

async function getNearbyCityCollectionSearch(center, radius) {
  const localityList = await getNearbySearch(center, radius, "locality");
  const administrativeAreaLevel3List = await getNearbySearch(center, radius, "administrative_area_level_3");
  
  return localityList.results.concat(administrativeAreaLevel3List.results);
};

getNearbyCityCollectionSearch({ latitude: -33.8670522, longitude: 151.1957362 }, 8000).then((nearbyCities) => {
  console.log(nearbyCities);
});

See this for the Places API documentation: https://developers.google.com/maps/documentation/places/web-service/search-nearby

  • Related