Skip to content

Back to home #79

@Radzio1615

Description

@Radzio1615

Hi! I have a question about example 41 (Back to home button).

I retrieve data from nominatim using this code:

fetch(`https://nominatim.openstreetmap.org/search?q=${address} ${post} ${city}&format=json&addressdetails=1&limit=1`)
  .then((response) => {
    return response.json();
  })
  .then((data) => {
    if(data.length){
      const localization = data[0];
        var osm = L.tileLayer('https://api.mapbox.com/styles/v1/mapbox/streets-v11/tiles/{z}/{x}/{y}?access_token={accessToken}', {
          tileSize: 512,
          zoomOffset: -1,
        });
        var map = L.map('map', {
          center: [localization.lat, localization.lon],
          zoom: 14,
          layers: [osm],
        });

        // example code here

    } else {
      throw 'Not found';
    }
  })
.catch((err) => { console.error(err); });

I changed buttonBackToHome.addEventListener from yours example, but I don't know how to adjust the moveend event to make it work properly. Can you help me with this?

function getCenterOfMap() {
    buttonBackToHome.classList.remove("hidden");

    buttonBackToHome.addEventListener("click", () => {
        map.flyTo([localization.lat, localization.lon], 14);
    });

    map.on("moveend", () => {
        const { lat: latCenter, lng: lngCenter } = map.getCenter();

        const latC = latCenter.toFixed(3) * 1;
        const lngC = lngCenter.toFixed(3) * 1;

        const defaultCoordinate = [+lat.toFixed(3), +lng.toFixed(3)];

        const centerCoordinate = [latC, lngC];

        if (compareToArrays(centerCoordinate, defaultCoordinate)) {
        buttonBackToHome.classList.add("hidden");
        }
    });
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions