[openstreetmap/openstreetmap-website] Fixed displacing points specified from context menu (PR #4904)

David Tsiklauri notifications at github.com
Tue Aug 6 11:25:52 UTC 2024


@nertc commented on this pull request.



> @@ -109,26 +125,61 @@ OSM.Directions = function (map) {
 
       var viewbox = map.getBounds().toBBoxString(); // <sw lon>,<sw lat>,<ne lon>,<ne lat>
 
-      $.getJSON(OSM.NOMINATIM_URL + "search?q=" + encodeURIComponent(endpoint.value) + "&format=json&viewbox=" + viewbox, function (json) {
-        endpoint.awaitingGeocode = false;
-        endpoint.hasGeocode = true;
-        if (json.length === 0) {
-          input.addClass("is-invalid");
-          alert(I18n.t("javascripts.directions.errors.no_place", { place: endpoint.value }));
-          return;
-        }
+      if (endpoint.value.match(/^([+-]?\d+(\.\d*)?)(?:\s+|\s*[/,]\s*)([+-]?\d+(\.\d*)?)$/)) {

As we reuse `/^([+-]?\d+(\.\d*)?)(?:\s+|\s*[/,]\s*)([+-]?\d+(\.\d*)?)$/` regex, it would be more understandable if we create separate constant variable for it and then use it with the variable name.

>  
-        geocodeCallback();
-      });
+          if (endpoint.latlng.distanceTo(L.latLng(json.lat, json.lon)) > 200.0) {
+            input.val(endpoint.value);
+          } else {
+            endpoint.value = json.display_name;
+            input.val(json.display_name);
+          }
+
+          geocodeCallback();
+        });
+      } else {
+        $.getJSON(OSM.NOMINATIM_URL + "search?q=" + encodeURIComponent(endpoint.value) + "&format=json&viewbox=" + viewbox, function (json) {

When concatenating many strings, I think, using [Template literals](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals) are more suitable and easier to read way.

> -        geocodeCallback();
-      });
+          if (endpoint.latlng.distanceTo(L.latLng(json.lat, json.lon)) > 200.0) {
+            input.val(endpoint.value);
+          } else {
+            endpoint.value = json.display_name;
+            input.val(json.display_name);
+          }
+
+          geocodeCallback();
+        });
+      } else {
+        $.getJSON(OSM.NOMINATIM_URL + "search?q=" + encodeURIComponent(endpoint.value) + "&format=json&viewbox=" + viewbox, function (json) {
+          endpoint.awaitingGeocode = false;
+          endpoint.hasGeocode = true;
+          if (!json || json.length === 0) {

This can be shortened as `!json?.length`

-- 
Reply to this email directly or view it on GitHub:
https://github.com/openstreetmap/openstreetmap-website/pull/4904#pullrequestreview-2220996355
You are receiving this because you are subscribed to this thread.

Message ID: <openstreetmap/openstreetmap-website/pull/4904/review/2220996355 at github.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.openstreetmap.org/pipermail/rails-dev/attachments/20240806/8f6c4594/attachment.htm>


More information about the rails-dev mailing list