Allow more complex URLs in the map URL #11157

Open
opened 2025-12-29 21:41:06 +01:00 by adam · 2 comments
Owner

Originally created by @joernheissler on GitHub (May 10, 2025).

NetBox version

v4.3.0

Feature type

Change to existing functionality

Proposed functionality

Hello,

I'd like to suggest a small improvement for the GPS Coordinate / MAPS_URL feature.
Currently NetBox renders the site/device coordinate as "{MAPS_URL}{lat},{lon}" when I click the map button.
My favorite online map is OpenStreetMap and with above scheme all I can do is set:

MAPS_URL = "https://www.openstreetmap.org/search?query="

which results in a link like: https://www.openstreetmap.org/search?query=48.858258,2.294498

OSM doesn't know if that is lat,lon or lon,lat. And it doesn't show a marker, doesn't allow me to set a different layer, zoom level, etc.

Instead I'd like to get a link like
https://www.openstreetmap.org/?mlat=48.858258&mlon=2.294498#map=16/48.858258/2.294498&layers=T

To implement this, the configured URL would be formatted, e.g.:

MAPS_URL_COORD = "https://www.openstreetmap.org/?mlat={lat}&mlon={lon}#map=16/{lat}/{lon}&layers=T"

The formatting could be done with a property in the Site/Device classes:

@property
def maps_url_coord(self) -> str:
  fmt = ConfigItem("MAPS_URL_COORD")()
  return fmt.format(lat=self.latitude, lon=self.longitude)

Alternatively, the same could be achived with a user specified jinja template.

For compatibility with the current MAPS_URL and the "map address" feature, a new configuration option might be needed, hence I used MAPS_URL_COORD above.

The templates in device.html and site.html need to be aware of the new config option. If it's not set (default), MAPS_URL is used:

{% if config.MAPS_URL or config.MAPS_URL_COORD %}
  <a href="{% if config.MAPS_URL_COORD %}{{ object.maps_url_coord }}{% else %}{{ config.MAPS_URL }}{{ object.latitude|unlocalize }},{{ object.longitude|unlocalize }}{% endif %}" target="_blank" class="btn btn-primary btn-sm d-print-none">
    <i class="mdi mdi-map-marker"></i> {% trans "Map" %}
  </a>
{% endif %}

That template doesn't look too nice anymore, so rendering MAPS_URL could be done in the same python function.

Use case

Better user experience when opening a map by allowing more flexibility in the map url.

Database changes

No database changes.

External dependencies

No new dependencies.

Originally created by @joernheissler on GitHub (May 10, 2025). ### NetBox version v4.3.0 ### Feature type Change to existing functionality ### Proposed functionality Hello, I'd like to suggest a small improvement for the GPS Coordinate / MAPS_URL feature. Currently NetBox renders the site/device coordinate as "{MAPS_URL}{lat},{lon}" when I click the map button. My favorite online map is OpenStreetMap and with above scheme all I can do is set: ``` MAPS_URL = "https://www.openstreetmap.org/search?query=" ``` which results in a link like: https://www.openstreetmap.org/search?query=48.858258,2.294498 OSM doesn't know if that is lat,lon or lon,lat. And it doesn't show a marker, doesn't allow me to set a different layer, zoom level, etc. Instead I'd like to get a link like https://www.openstreetmap.org/?mlat=48.858258&mlon=2.294498#map=16/48.858258/2.294498&layers=T To implement this, the configured URL would be formatted, e.g.: ``` MAPS_URL_COORD = "https://www.openstreetmap.org/?mlat={lat}&mlon={lon}#map=16/{lat}/{lon}&layers=T" ``` The formatting could be done with a property in the Site/Device classes: ```python @property def maps_url_coord(self) -> str: fmt = ConfigItem("MAPS_URL_COORD")() return fmt.format(lat=self.latitude, lon=self.longitude) ``` Alternatively, the same could be achived with a user specified jinja template. For compatibility with the current `MAPS_URL` and the "map address" feature, a new configuration option might be needed, hence I used `MAPS_URL_COORD` above. The templates in `device.html` and `site.html` need to be aware of the new config option. If it's not set (default), `MAPS_URL` is used: ``` {% if config.MAPS_URL or config.MAPS_URL_COORD %} <a href="{% if config.MAPS_URL_COORD %}{{ object.maps_url_coord }}{% else %}{{ config.MAPS_URL }}{{ object.latitude|unlocalize }},{{ object.longitude|unlocalize }}{% endif %}" target="_blank" class="btn btn-primary btn-sm d-print-none"> <i class="mdi mdi-map-marker"></i> {% trans "Map" %} </a> {% endif %} ``` That template doesn't look too nice anymore, so rendering MAPS_URL could be done in the same python function. ### Use case Better user experience when opening a map by allowing more flexibility in the map url. ### Database changes No database changes. ### External dependencies No new dependencies.
adam added the type: featurenetboxstatus: backlogcomplexity: low labels 2025-12-29 21:41:06 +01:00
Author
Owner

@NightlyNews commented on GitHub (Jul 17, 2025):

Curly braces and format might be mistaken for a uri template and curly braces aren't reserved or unreserved in uris. We could instead use string Template and it would look like:

MAPS_URL_COORD = "https://www.openstreetmap.org/?mlat=${lat}&mlon=${lon}#map=16/${lat}/${lon}&layers=T"

Here's an example using a fake arbitrary URL that uses both reserved, unreserved and undefined characters with $$ to escape to $:

MAPS_URL_COORD = "https://example.com/search?query={location}$$lat=${lat}$$long=${lon}"

Then in a property you could template

from string import Template
return Template(MAPS_URL_COORD).substitute(lat=self.latitude, lon=self.longitude)

The result would be like:

'https://example.com/search?query={location}$lat=48.858258$long=2.294498'
@NightlyNews commented on GitHub (Jul 17, 2025): Curly braces and format might be mistaken for a [uri template](https://www.rfc-editor.org/rfc/rfc6570.html) and curly braces aren't reserved or unreserved in uris. We could instead use [string Template](https://peps.python.org/pep-0292/) and it would look like: ``` MAPS_URL_COORD = "https://www.openstreetmap.org/?mlat=${lat}&mlon=${lon}#map=16/${lat}/${lon}&layers=T" ``` Here's an example using a fake arbitrary URL that uses both [reserved, unreserved and undefined characters](https://www.rfc-editor.org/rfc/rfc3986#section-2.2) with `$$` to escape to `$`: ``` MAPS_URL_COORD = "https://example.com/search?query={location}$$lat=${lat}$$long=${lon}" ``` Then in a property you could template ``` from string import Template return Template(MAPS_URL_COORD).substitute(lat=self.latitude, lon=self.longitude) ``` The result would be like: ``` 'https://example.com/search?query={location}$lat=48.858258$long=2.294498' ```
Author
Owner

@joernheissler commented on GitHub (Jul 18, 2025):

Sounds like yet another reasonable option for a templating language.

With python format strings, you can use {{ to get a single {. With Jinja2 it's more complicated like {{ "{" }}

Personally I don't care much which option is used, but It think it should be consistent with how NetBox does similar things, but I'm not sure if there are precedents.

Could a NetBox maintainer just pick one, please?

@joernheissler commented on GitHub (Jul 18, 2025): Sounds like yet another reasonable option for a templating language. With python format strings, you can use `{{` to get a single `{`. With Jinja2 it's more complicated like `{{ "{" }}` Personally I don't care much which option is used, but It think it should be consistent with how NetBox does similar things, but I'm not sure if there are precedents. Could a NetBox maintainer just pick one, please?
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#11157