Просмотр исходного кода

Closes #19460: Support {lat}/{lon} placeholders in MAPS_URL for GPS coordinates

When MAPS_URL contains {lat} or {lon}, format it as a Python format string.
Otherwise fall back to the existing behavior of appending lat,lon to the URL.
This enables full control over coordinate placement and parameter naming,
e.g. OpenStreetMap's mlat=/mlon= params, zoom levels, layer selection.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Brian Tiemann 2 дней назад
Родитель
Сommit
f211430cf6

+ 12 - 1
docs/configuration/miscellaneous.md

@@ -190,7 +190,18 @@ Setting this to `True` will display a "maintenance mode" banner at the top of ev
 
 Default: `https://maps.google.com/?q=` (Google Maps)
 
-This specifies the URL to use when presenting a map of a physical location by street address or GPS coordinates. The URL must accept either a free-form street address or a comma-separated pair of numeric coordinates appended to it. Set this to `None` to disable the "map it" button within the UI.
+This specifies the URL to use when presenting a map of a physical location by street address or GPS coordinates. Set this to `None` to disable the "map it" button within the UI.
+
+**For street addresses**, the URL must accept a free-form address string appended directly to it.
+
+**For GPS coordinates**, two formats are supported:
+
+* **Simple prefix** (default behavior): The latitude and longitude are appended as a comma-separated pair. For example, `https://maps.google.com/?q=` produces `https://maps.google.com/?q=48.858,2.294`.
+* **Format string**: Include `{lat}` and/or `{lon}` placeholders anywhere in the URL for full control over placement. For example:
+
+```
+MAPS_URL = "https://www.openstreetmap.org/?mlat={lat}&mlon={lon}#map=16/{lat}/{lon}"
+```
 
 ---
 

+ 4 - 1
netbox/netbox/config/parameters.py

@@ -229,7 +229,10 @@ PARAMS = (
         name='MAPS_URL',
         label=_('Maps URL'),
         default='https://maps.google.com/?q=',
-        description=_("Base URL for mapping geographic locations")
+        description=_(
+            "URL for mapping geographic locations. For GPS coordinates, include {lat} and {lon} placeholders, "
+            "or omit them to append coordinates as a comma-separated pair."
+        )
     ),
 
 )

+ 10 - 1
netbox/netbox/ui/attrs.py

@@ -500,13 +500,22 @@ class GPSCoordinatesAttr(MapURLMixin, ObjectAttribute):
         longitude = resolve_attr_path(obj, self.longitude_attr)
         if latitude is None or longitude is None:
             return self.placeholder
+        map_url = self.map_url
+        if map_url:
+            map_url = self._build_coords_url(map_url, latitude, longitude)
         return render_to_string(self.template_name, {
             'name': context['name'],
             'latitude': latitude,
             'longitude': longitude,
-            'map_url': self.map_url,
+            'map_url': map_url,
         })
 
+    @staticmethod
+    def _build_coords_url(map_url, latitude, longitude):
+        if '{lat}' in map_url or '{lon}' in map_url:
+            return map_url.format(lat=latitude, lon=longitude)
+        return f'{map_url}{latitude},{longitude}'
+
 
 class DateTimeAttr(ObjectAttribute):
     """

+ 1 - 1
netbox/templates/ui/attrs/gps_coordinates.html

@@ -2,7 +2,7 @@
 {% load l10n %}
 <span>{{ latitude }}, {{ longitude }}</span>
 {% if map_url %}
-  <a href="{{ map_url }}{{ latitude|unlocalize }},{{ longitude|unlocalize }}" target="_blank" class="btn btn-primary btn-sm print-none">
+  <a href="{{ map_url }}" target="_blank" class="btn btn-primary btn-sm print-none">
     <i class="mdi mdi-map-marker"></i> {% trans "Map" %}
   </a>
 {% endif %}