Get a list of all cities in a country (or state), including latitude and longitude - graphhopper

How can I use GraphHopper to get a list of all cities in a country (or USA state)? For each city, I need a latitude and longitude.
(I presume the city's latitude and longitude would be the official middle of that city.)

That process is called 'geocoding' and not done from GraphHopper. GraphHopper is only about finding the best way between 'raw' coordinates.
Geocoding itself is another research area and also complex ;). You can have a look at nominatim or photon or using the GraphHopper Directions API.
Another more simple solution for your situation could be to get the cities via OSM (parsing xml and find nodes having the tags place=city or town) or using geonames or similar.

Related

Google Place API city search by coordinates

I'm trying to access cities from the google place API. I want to be able to search with coordinates and have it return a city. My goal is for a user to be able to enter in a city and a photo shows up. So after getting the city, I will use the photo reference to get the image.
This is my request
https://maps.googleapis.com/maps/api/place/search/json?location=35.4107,80.8429&radius=100&sensor=true&key=API_KEY
the coordinates entered is a City in North Carolina. However, it does not return a city within North Carolina at all. All I am looking to do is use coordinates inside my request and have it return the city the coordinates are in. I'm relatively new to development, if I have been unclear in any way please point it out so that I may correct myself. Thanks!
I believe reverse geocoding is more suitable for your task than Places API. Places API is designed to search businesses and POIs at first place, localities shouldn't be among the results.
With reverse geocoding you can specify a coordinate and type of result you are looking for. For example, to search the city use the following request
https://maps.googleapis.com/maps/api/geocode/json?latlng=35.4107%2C-80.8429&result_type=locality&key=YOUR_API_KEY
This will return the "Huntersville, NC 28078, USA"

Find city radius

I am working on an app where I need to check if some points are within a city or X km outside of it, so I need to find the city's radius then add the X variable to it. I've been looking on Google Maps and OpenStreetMap and I could not find such info.
Is there any API that provides the city's average width? Or distance between the center and the edge?
Based on the Latitude & Longitude of two given points, you can find the distance between those points.
So fetch the Location (Latitude & Longitude) for the centre of city and the point needed, using Google Maps API's and find out the distance implementing the logic.
Logic is described in below link :
http://www.movable-type.co.uk/scripts/latlong.html
Post calculation of distance, you verify if it is in the required range based on X.
I came across the same problem and here is the best method I found so far,
Using the service Geoapify
you can find the placeid for a city using the geocoding API
We can then create a location filter based on the place id as documented here
we can also use that filter for autocomplete to allow only for places inside the city

Google maps APIs to return place' type based on latitude & longitude

I am trying to build an application in which I need to retrieve location's type based on latitude and longitude. I tried using google's places API but not getting satisfied results.
For eg, if I want to know if the user searches for SFSU, it should be stored as 'university'. I can get its latitude/longitude from geocoding APIs.
https://maps.googleapis.com/maps/api/geocode/json?address=SFSU&key=YOUR_KEY
But from places API, I cannot get the exact type, I mostly get it as street or establishment:
https://maps.googleapis.com/maps/api/geocode/json?latlng=37.7244047,-122.4760196&key=YOUR_KEY
Similarly, I wanted to get cafe for any starbucks or mall/shopping-center for any mall.
Edit:
I got confused between geocode API and places API as geocode API also returns place' type.
I found that places API returns multiple results even if we provide correct latitude and longitude. So if we need the perfect result, we need to provide sort filter with Places API.
https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=(lat),(long)&rankby=distance&key=YOUR_API_KEY
It looks like you are using reverse geocoding, not the Places API.
Request to Places API should be something like:
https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=37.7244047%2C-122.4760196&radius=10&type=university&key=YOUR_API_KEY
This one returns 'San Francisco State University, 1600 Holloway Avenue, San Francisco'
https://developers.google.com/places/web-service/search#PlaceSearchRequests
Hope it helps!

Google Maps Geocoder: Reference Position for Plain Location Names

I'm using the google maps api for geocoding addresses. If I geocode a plain location name (e.g. Munich) the api returns corresponding latitude and longitude.
To which position do these coordinates refer to? Is it the (geographical) center of the location or the position of the administrative headquarter or what else?
They refer to the equator which is latitude=0° and the prime meridian which is longitude=0°. This represents the central point (starting point).
You can look at it as to the Cartesian coordinate system starting from (0,0)=(x,y) and retrieving all the other places by changing the x(long) and y(lat) coordinates. So yes, the answer is geographical centre.
EDIT:
url (video): https://www.youtube.com/watch?v=Spel7vfkpNc starting at 2:43
Basically, companies within the country decide on which lat, long will a city, country when searched appear. For example if you are searching for London the pin point will be assigned to particular coordinates depending on the company decision.

Getting list of cities connected directly woth a city by road

Is there some way to get list of cities connected directly with a city using google maps api?
No, there isn't a way to do that with the Google Maps API. You would have to know which cities you're interested in before hand and use the Distance Matrix.