I have recently started programming in Mathematica and I am currently struggling to find a way to locate the nearest city based on a given latitude. I have looked into GeoPosition and GeoNearest, but I find all these functions a bit confusing.

If someone could point me in the right direction or guide me, I would greatly appreciate it!

  • $\begingroup$ Welcome to Mathematica StackExchange! So if I understand correctly: You are looking at the circle on the Earth with given latitude, and you want to find the city with this latitude? Most likely, there will be many cities that are crossed by this latitude ... So are you sure you want just one of them? The centerpoint (coordinates) of the city are somehow arbitrarily defined. $\endgroup$
    – Domen
    Commented May 1, 2023 at 10:40
  • $\begingroup$ Thank you! Mathematica feels a bit overwhelming from writing Python :) With all these given functions and symbols. But, hoping to find supportive community. $\endgroup$
    – Ammar
    Commented May 1, 2023 at 12:30
  • $\begingroup$ I suggest reading The Wolfram Language: Fast Introduction for Programmers which includes notes for people comming from Python background. $\endgroup$
    – Domen
    Commented May 1, 2023 at 12:39
  • $\begingroup$ @Domen I read that tutorial, with my horror, saw only one YouTube video that used Mathematica and the guy started to write Prepend inside a function and I was like WHAT! And that fast introduction does not involve much with loops and functions in general, so not provided much help, unfortunately! $\endgroup$
    – Ammar
    Commented May 1, 2023 at 12:57
  • $\begingroup$ That is because loops (do, while, for) are very rarerly used in Mathematica. You just have to get used that there are some conceptual differences between the two languages. I strongly suggest you read through the tutorial (also Stephen Wolfram An Elementary Introduction to the Wolfram Language because it will save you a lot of confusion. $\endgroup$
    – Domen
    Commented May 1, 2023 at 13:05

1 Answer 1


You can use GeoNearest and GeoPath.

latitude = 46.06;

cities = GeoNearest["City", GeoPath[{"Parallel", latitude}], {All, 0}, 
 DistanceFunction -> "Center"]
(* {Entity["City", {"Dobrova", "Osrednjeslovenska", "Slovenia"}], 
    Entity["City", {"Ljubljana", "Osrednjeslovenska", "Slovenia"}], 
    Entity["City", {"Premariacco", "FriuliVeneziaGiulia", "Italy"}], 
    ...} *)

(* 11 *)

Show[GeoGraphics[GeoPath[{"Parallel", latitude}]], GeoListPlot[cities]]

enter image description here

However, there are two problems. First, as shown above, there may be multiple cities having the exact same lattitude. Second, if no city is found at the exact lattitude, you will have to repeat the procedure repeatedly by increasing the allowed tolerance (set to 0 in the code above) until a match is found. This is because there seems to be a bug in GeoNearest when used with GeoPath so that results are not given in correct order.

If you want to restrict the search only to capitals, you can first get a list of all of them, then use it inside GeoNearest.

capitals = Map[CountryData[#, "CapitalCity"] &, CountryData["UN"]];
GeoNearest[capitals, GeoPath[{"Parallel", latitude}]]
(* {Entity["City", {"Ljubljana", "Osrednjeslovenska", "Slovenia"}]} *)

Now there are only two such lattitudes with more than one capital city:

Select[GatherBy[EntityValue[capitals, {"Name", "Latitude"}], Last], 
 Length[#] > 1 &] // Grid
(* {Beijing, 39.93°}, {Ankara, 39.93°}
   {Tehran, 35.67°}, {Tokyo, 35.67°} *)
  • $\begingroup$ for a given latitude, I want to find the closest city to it. That means If I use the function or code, I just type the latitude and my code will show the nearest city to that latitude. Can we do that? $\endgroup$
    – Ammar
    Commented May 1, 2023 at 12:29
  • $\begingroup$ Welcome Ammar, here is a hint to get you started in the right direction. Make a function that starts like findCitiesAtLatitude[latitude_, number_]:= Block[{cities=…..},…] $\endgroup$ Commented May 1, 2023 at 12:35
  • $\begingroup$ @Ammar, there is a problem with what you want. For example: both Ljubljana (Slovenia) and Madaras (Hungary) have the same latitude which you can see by evaluating Entity["City", {"Ljubljana", "Osrednjeslovenska", "Slovenia"}]["Latitude"] and Entity["City", {"Madaras", "BacsKiskun", "Hungary"}]["Latitude"], both returning Quantity[46.06, "AngularDegrees"]. How to then choose one of them if you give lattitude 46.06? $\endgroup$
    – Domen
    Commented May 1, 2023 at 12:38
  • $\begingroup$ @Domen Yes, you're right! Can we somehow just limit the function or code to show the nearest capital city of a given latitude? $\endgroup$
    – Ammar
    Commented May 1, 2023 at 12:50
  • $\begingroup$ @CraigCarter Thank you and I actually, thought of that, but, the functions to find the cities nearest to something, I am having a hard time to understand what to use or how to do that. Documentation is OKayish! And don't really have any further materials : ) Trying get to used to myself with mathematica, haha! $\endgroup$
    – Ammar
    Commented May 1, 2023 at 12:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.