Skip to content

Commit

Permalink
Add support for pre-defined coordinates
Browse files Browse the repository at this point in the history
... in case no geocoding is required/intended
  • Loading branch information
hbruch committed Nov 29, 2024
1 parent 7d3c76f commit d2a7433
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 16 deletions.
10 changes: 9 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,19 @@ To use it create a `<div>`, give it an ID and at least `200px` height and execut
new StadtnaviAddressBox(divId, title, address, options);
```

or, if the coordinates are known and no on-the-fly geocoding is required:

```js
new StadtnaviAddressBox(divId, title, address, options, lat, lon);
```

#### Arguments

- `divId`: ID of the `<div>` element into which the interactive map is placed.
- `title`: Name of the place for which the address box is for. This can be choosen freely and is used only for display purposes.
- `address`: A full address with street name, house number, postal code and city. This is used for geolocating the place on the map so precise information is required.
- `address`: A full address with street name, house number, postal code and city. This is used for geolocating the place on the map so precise information is required, in case lat/lon are unknown.
- `lat`: Optional latitude value (-90 <=lat <= 90) for locating the place.
- `lon`: Optional longitude value (-180 <=lat <= 180) for locating the place.

#### Options

Expand Down
33 changes: 21 additions & 12 deletions src/address-box.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ L.control.logo = function(opts) {

class StadtnaviAddressBox {

constructor(divId, title, address, options) {
constructor(divId, title, address, options, lat, lon) {

this.defaults = {
... {
Expand Down Expand Up @@ -82,19 +82,28 @@ class StadtnaviAddressBox {
map.attributionControl.setPrefix(false);
map.dragging.disable();

const lonFloat = parseFloat(lon);
const latFloat = parseFloat(lat);
if (lonFloat >= -180 && lonFloat <= 180 && latFloat >= -90 && latFloat <= 90 ) {
this.zoomTo([latFloat, lonFloat], map, title, address);
} else {
fetch(`${Stadtnavi.photonUrl}/api?q=${address}`)
.then(r => r.json())
.then(json => {

const latLng = json.features[0].geometry.coordinates;
latLng.reverse();
this.zoomTo(latLng, map, title, address);
});
}
}

fetch(`${Stadtnavi.photonUrl}/api?q=${address}`)
.then(r => r.json())
.then(json => {

const latLng = json.features[0].geometry.coordinates;
latLng.reverse();
map.setView(latLng)
zoomTo(latLng, map, title, address ) {
map.setView(latLng)

Stadtnavi.marker(latLng, mergedOptions).addTo(map);
L.control.address({ position: 'topleft', lat: latLng[0], lng: latLng[1], title, address,
stadtnaviLinkText: mergedOptions.stadtnaviLinkText, stadtnaviBaseUrl: mergedOptions.stadtnaviBaseUrl }).addTo(map);
});
Stadtnavi.marker(latLng, this.mergedOptions).addTo(map);
L.control.address({ position: 'topleft', lat: latLng[0], lng: latLng[1], title, address,
stadtnaviLinkText: this.mergedOptions.stadtnaviLinkText, stadtnaviBaseUrl: this.mergedOptions.stadtnaviBaseUrl }).addTo(map);
}

computeOptions(options) {
Expand Down
9 changes: 6 additions & 3 deletions src/demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -77,21 +77,24 @@ <h3>Callback result</h3>
</section>

<section>
<h2 class="mt-5">Address box</h2>
<h2 class="mt-5">Address box with well-known coordinates</h2>

<h4>JavaScript code</h4>
<pre>
new StadtnaviAddressBox('widget-2', "Volkshochschule Außenstelle Ehningen", "Gartenstraße 11, 71139 Ehningen", {});</pre>
new StadtnaviAddressBox('widget-2', "Volkshochschule Außenstelle Ehningen", "Gartenstraße 11, 71139 Ehningen", {}, 48.659697, 8.943804);</pre>

<div id='widget-2' class="stadtnavi-widget"></div>

<script type="text/javascript">
new StadtnaviAddressBox('widget-2', "Volkshochschule Außenstelle Ehningen", "Gartenstraße 11, 71139 Ehningen", {});
new StadtnaviAddressBox('widget-2', "Volkshochschule Außenstelle Ehningen", "Gartenstraße 11, 71139 Ehningen", {}, 48.659697, 8.943804);
</script>

</section>


<section>
<h2 class="mt-5">Address box with on-the-fly geocoding</h2>

<h4>JavaScript code</h4>
<pre>
new StadtnaviAddressBox('widget-3', "Rathaus Bietigheim-Bissingen", "Marktplatz 8, 74321 Bietigheim-Bissingen", {});</pre>
Expand Down

0 comments on commit d2a7433

Please sign in to comment.