This section will cover the implementation of the map and its dependencies. User-centric documentation for the map can be found here.
The company map used to be an static ArcGIS map embedded within the HempDB page. Now the map shows active companies within the company
table using their latitude and longitude fields.
The map in its current state was implemented in these PRs if you’d like to see the code: #171, #179.
The map and markers are displayed using LeafletJS, and the heatmap functionality uses Leaflet.markercluster.
Latitudes and longitudes (and other information) of each company are gathered in the map()
view and sent to the map.html
template using the json_script
template tag. From there, rendering is done on the frontend using JavaScript by parsing the company data in the <script>
tag.
To obtain the latitude and longitude of each company, we use the Geocoder Python library. This acts as a wrapper around the ArcGIS Geocoding API. See the geocode_location()
helper function in views.py
to see how this is done in detail.
Since we can’t query all 5,000+ company latitudes/longitudes each time someone visits the map, we store them in the database. Prior to this, each company just had a country (required), and an address (optional). We now obtain the company’s latitude and longitude from these avaiable attributes.
Creating a company
Editing a company
In summary, a user manually touching latitude/longitude will always take precedence. If they do not, we will call the geocoding API to obtain the coordinates.
You may notice when visiting the map page for the first time in awhile, it takes ~5 seconds to load, but subsequent refreshes of the page are faster. This is because we cache the queried company data from the database.
Signals in signals.py
are used to invalidate the cached data when the applicable models are created, edited, or deleted so the map isn’t showing dated information.
To do this, we use Redis, which stores data in-memory for better performance. More details about this can be seen in the map()
view, and Django’s caching documentation can be found here.