const here = {
  apiKey:'KJ-S3Fuz34beh9KbbbzJznJLyEEVmDAymadvIP2ju6Q',
  geocodeUrl:'https://geocode.search.hereapi.com/v1/geocode',
}

const locatethis = async () => {

  console.log("locating");
  let url = new URL((window.location.href).toLowerCase());
  let address = url.searchParams.get("q");

  try {

    let here_locate = await fetch(here.geocodeUrl+"?q=" + encodeURIComponent(address) + "&apiKey=" + encodeURIComponent(here.apiKey), {
      method: "GET",
      headers: { "Content-type": "application/x-www-form-urlencoded" },
      cache: "no-cache"
    });

    let here_query_results = await here_locate.json();
    console.log(here_query_results);
    return true;
  } catch (error) {
    console.log(error);
  }
}

document.addEventListener('DOMContentLoaded', function () {

  locatethis();

  const style = 'normal.day';

  const hereTileUrl = `https://2.base.maps.ls.hereapi.com/maptile/2.1/maptile/newest/${style}/{z}/{x}/{y}/512/png8?apiKey=${here.apiKey}&ppi=320`;

  const map = L.map('map', {
    center: [37.773972, -122.431297],
    zoom: 11,
    zoomControl: false, 
    layers: [L.tileLayer(hereTileUrl)]
  });

  L.control.zoom({
    position: 'topright'
  }).addTo(map);

  map.attributionControl.addAttribution('&copy; HERE 2019');
}, false);