Find closest big city froma given lat/lng

504 views
Skip to first unread message

Panacea

unread,
Apr 11, 2017, 12:57:11 AM4/11/17
to GeoNames
Hi,
  I am a new user to Geonames and very impressed by this API. I have a mobile app which gives me the lat/lng for the user, I want to look up the closest large city atleast within US/Canada maybe 
world wide. I came across this post geoname post for finding city
and the reply posted suggested I use a bounding box with the API api.geonames.org/citiesJSON?  This service returns a list of cities and placenames in the bounding box, ordered by relevancy (capital/population). This is exactly what I want but I dont understand the bounding box parameters? Can someone please tell me how I can calculate these for a given lat/lng
to get the closest large city. If for example im in a suburb of Atlanta, norcross with the following coordinates

Norcross
lat: 33.9, 
lng: -84.2 

The closest city would be Atlanta, how can I dynamically get the bounding box parameters from a lat/lng? I would really appreciate your help since i couldnt find this info
Thanks in Advance

Farhan

Barry Hunter

unread,
Apr 11, 2017, 7:47:50 AM4/11/17
to geonames
The easiest would probably just add a fixed size. Say +/- 0.5 degrees. (so south is say lat-0.5, north is lat+0.5) 

... this would give you in effect the 'biggest city within 0.5 degrees'. rather than the just geographically nearest place (which may be tiny!), or even having to decide what types of places you want. 

The complication is perhaps if you get nothing useful within 0.5 degrees (ie no cities, or just small places) - may have to repeat the call, using a radius of say 1.0 degree to actually get results. 



--
You received this message because you are subscribed to the Google Groups "GeoNames" group.
To unsubscribe from this group and stop receiving emails from it, send an email to geonames+unsubscribe@googlegroups.com.
To post to this group, send email to geon...@googlegroups.com.
Visit this group at https://groups.google.com/group/geonames.
For more options, visit https://groups.google.com/d/optout.

Panacea

unread,
Apr 12, 2017, 12:38:24 AM4/12/17
to GeoNames
Thanks so much for replying Bary, so I tried your suggestion when i do the API call, what should the east/west parameters be? When i use the following API call


setting from 0 to 4 for north south like north =+4 and south =-4 gets me no results back

and when i set north =+5 and south =-5 or bigger I get the following error

"ERROR: canceling statement due to statement timeout"

Any idea what am i doing wrong here? Please let me know, thanks!!


 
On Tuesday, April 11, 2017 at 7:47:50 AM UTC-4, barryhunter wrote:
The easiest would probably just add a fixed size. Say +/- 0.5 degrees. (so south is say lat-0.5, north is lat+0.5) 

... this would give you in effect the 'biggest city within 0.5 degrees'. rather than the just geographically nearest place (which may be tiny!), or even having to decide what types of places you want. 

The complication is perhaps if you get nothing useful within 0.5 degrees (ie no cities, or just small places) - may have to repeat the call, using a radius of say 1.0 degree to actually get results. 


On 11 April 2017 at 03:54, Panacea <panaceade...@gmail.com> wrote:
Hi,
  I am a new user to Geonames and very impressed by this API. I have a mobile app which gives me the lat/lng for the user, I want to look up the closest large city atleast within US/Canada maybe 
world wide. I came across this post geoname post for finding city
and the reply posted suggested I use a bounding box with the API api.geonames.org/citiesJSON?  This service returns a list of cities and placenames in the bounding box, ordered by relevancy (capital/population). This is exactly what I want but I dont understand the bounding box parameters? Can someone please tell me how I can calculate these for a given lat/lng
to get the closest large city. If for example im in a suburb of Atlanta, norcross with the following coordinates

Norcross
lat: 33.9, 
lng: -84.2 

The closest city would be Atlanta, how can I dynamically get the bounding box parameters from a lat/lng? I would really appreciate your help since i couldnt find this info
Thanks in Advance

Farhan

--
You received this message because you are subscribed to the Google Groups "GeoNames" group.
To unsubscribe from this group and stop receiving emails from it, send an email to geonames+u...@googlegroups.com.

Urs Klaeger

unread,
Apr 14, 2017, 2:07:20 AM4/14/17
to GeoNames
Hi Farhan

Before asking GeoNames you must prepare the bounding box.

 

You are near Atlanta at lat 33.9° and long -84.2°

To consider a bounding box of say 30 km ‘around’ this point, you must add or subtract these 30 km from your position.

 

One degree of latitude is about 111 km or 60 nautical miles (about… the earth is not a sphere)

Add and subtract to your latitude in degrees 30km / 111km = 0.27°

Northern limit will be 33.9° + 0.27° = 34.17°

And southern limit  33.9° - 0.27° = 33.63°

 

One degree of longitude is 111km too but only on the equator… and 0km at the poles.

You must multiply the 111km with the cosine of your latitude

111km * Cosine(33.9°) = 111km * 0.83 = 92.1km

Add and subtract to your longitude in degrees 30km / 92.1km= 0.33°

Eastern limit will be -84.2° + 0.33° = -83.87°

And western limit -84.2° - 0.33° = -84.53°

 

Then you can ask GeoNames >>>

http://api.geonames.org/citiesJSON?north=34.17&south=33.63&east=-83.87&west=-84.53 &username=demo

 

Indeed, you may transform the kilometers into your preferred miles…

 
Reply all
Reply to author
Forward
0 new messages