Displaying Points on a Map
Learn how to display points on a map to specify a geographical location through coordinates.
We'll cover the following
Displaying maps and exploring them is pretty entertaining. But as any modern cartographer can tell you, there’s a whole new level of cool to be attained by adding to a map.
With that in mind, our next task is to add some simple detail in the form of points that show the location of cities.
To do this, we will load in a CSV file with data that identifies our cities and includes latitude and longitude details. Our file is called cities.csv
and looks like this:
code,city,country,lat,lon
ZNZ,ZANZIBAR,TANZANIA,-6.13,39.31
TYO,TOKYO,JAPAN,35.68,139.76
AKL,AUCKLAND,NEW ZEALAND,-36.85,174.78
BKK,BANGKOK,THAILAND,13.75,100.48
DEL,DELHI,INDIA,29.01,77.38
SIN,SINGAPORE,SINGAPOR,1.36,103.75
BSB,BRASILIA,BRAZIL,-15.67,-47.43
RIO,RIO DE JANEIRO,BRAZIL,-22.90,-43.24
YTO,TORONTO,CANADA,43.64,-79.40
IPC,EASTER ISLAND,CHILE,-27.11,-109.36
SEA,SEATTLE,USA,47.61,-122.33
While we’re only going to use the latitude and longitude for our current work, the additional details could just as easily be used for labeling or tooltips.
We need to place our code carefully in this case because while you might have some flexibility in getting the right result with a locally hosted version of a map, there is a possibility that with a version hosted in the outside world, you could strike trouble.
The code
The code to load the cities should be placed inside the function that is loading the world map as indicated below:
Get hands-on with 1400+ tech skills courses.