ICT:Drupal - View - Custom text with URL to openstreetmap
How to use a text field in a view to link to maps (OpenStreetMap + OpenTopoMap)
In the view, make a Global: Custom text field. This field should be located behind (down) other fields you want to use the fields content of.
Look carefully to the replacement patterns; they tell you what machine fields are available as parameters.
Here is an example of the view PLACES:
{{ edit_node }} == Link to edit Content
{{ title }} ==
{{ field_pl_name }} ==
{{ field_pl_type }} ==
{{ field_pl_address }} ==
{{ field_pl_parent_place }} ==
{{ field_pl_coordinates }} ==
{{ nothing }} == Custom text
We are using the geofield approach with coordinates Latitude, Longitude as a couple in the machine field. The field contains HTML code which needs to be stripped by the "clean" function first before the coordinates can be used separately.
Map providers used
The platform uses two external map viewers:
| Provider | Used For | Reason |
|---|---|---|
| OpenTopoMap | Continent, Country, Region, Province, City, Village, Square, Street | Clean, readable, calm map style; ideal for borders and terrain; no marker needed |
| OpenStreetMap.org | Point | Supports marker display via URL parameters |
This hybrid approach keeps the system simple and readable while still allowing precise point-level markers.
URL formats
OpenStreetMap (with marker support)
In the example of going with this field to look to the map at OpenStreetMap, you should first study what OpenStreetMap requests as an URL in order to show a map centered around a point given by coordinates, and this with a certain zoom level.
Basic centered map:
https://www.openstreetmap.org/#map=<zoom>/<latitude>/<longitude>
If you also want to display a marker at the coordinates spot then it becomes:
https://www.openstreetmap.org/?mlat=<latitude>&mlon=<longitude>#map=<zoom>/<latitude>/<longitude>
This form is used only for the Point type, where a marker is essential.
OpenTopoMap (no marker support)
OpenTopoMap uses a slightly different URL format:
https://opentopomap.org/#map=<zoom>/<latitude>/<longitude>
Example:
https://opentopomap.org/#map=6/50.85/4.35
Notes:
- OpenTopoMap does not support markers.
- It may rewrite the URL after loading (rounding coordinates). This is normal behavior.
Final Twig code (hybrid solution)
If we on top of this want to use the TYPE field (Continent, Country, City,...) to set the zoom level, and use OpenTopoMap for all types except Point (where we still want a marker via OpenStreetMap), the final code is like this:
{% set clean = field_pl_coordinates|striptags|trim %}
{% if clean %}
{% set parts = clean|split(',') %}
{% set lat = parts[0]|trim %}
{% set lon = parts[1]|trim %}
{% if field_pl_type matches '/Continent/' %}
{% set zoom = 3 %}
{% elseif field_pl_type matches '/Country/' %}
{% set zoom = 5 %}
{% elseif field_pl_type matches '/Region/' %}
{% set zoom = 7 %}
{% elseif field_pl_type matches '/Province/' %}
{% set zoom = 7 %}
{% elseif field_pl_type matches '/City/' %}
{% set zoom = 10 %}
{% elseif field_pl_type matches '/Village/' %}
{% set zoom = 10 %}
{% elseif field_pl_type matches '/Square/' %}
{% set zoom = 16 %}
{% elseif field_pl_type matches '/Street/' %}
{% set zoom = 18 %}
{% elseif field_pl_type matches '/Point/' %}
{% set zoom = 18 %}
{% else %}
{% set zoom = 10 %} {# default #}
{% endif %}
{# Use OpenStreetMap only for Point (marker needed) #}
{% if field_pl_type matches '/Point/' %}
<a href="https://www.openstreetmap.org/?mlat={{ lat }}&mlon={{ lon }}#map={{ zoom }}/{{ lat }}/{{ lon }}" target="_blank">🌐</a>
{% else %}
<a href="https://opentopomap.org/#map={{ zoom }}/{{ lat }}/{{ lon }}" target="_blank">🌐</a>
{% endif %}
{% endif %}
Zoom levels to be used
OpenStreetMap and OpenTopoMap use fixed zoom levels to define how much detail a map shows, from global views to building-level precision.
| Zoom | Visible Area | Meters per pixel (approx.) | Typical Use |
|---|---|---|---|
| 0 | Whole Earth | 156412 | Global overview |
| 1 | Continents | 78206 | Very broad context |
| 2 | Large regions | 39103 | Continental regions |
| 3 | Countries | 19551 | National maps |
| 4 | Large provinces | 9776 | Regional overview |
| 5 | Regions / states | 4888 | Macro-regional planning |
| 6 | Large metropolitan areas | 2444 | Wide urban areas |
| 7 | Cities | 1222 | City-level context |
| 8 | Large towns | 610 | Town overview |
| 9 | Towns | 305 | Local town context |
| 10 | Urban areas | 152 | City districts |
| 11 | Neighborhoods | 76 | Neighborhood-level detail |
| 12 | Local areas | 38 | Local context |
| 13 | Streets | 19 | Street-level detail |
| 14 | Street blocks | 9.5 | Street blocks, POIs |
| 15 | Buildings | 4.8 | Building footprints |
| 16 | Building details | 2.4 | Entrances, precise locations |
| 17 | Entrances | 1.2 | Very fine detail |
| 18 | Very fine detail | 0.6 | Door-level precision |
| 19 | Ultra-detailed | 0.3 | High-precision mapping (if available) |
These are suggestions of course, and personal adjustment is your choice.