summaryrefslogtreecommitdiffstats
path: root/src/assets/javascripts/helpers/google-maps.js
blob: 3a041c6768a3fbc6ac8ef39e5a50e723a2612f30 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
export default class {
  static targets = /https?:\/\/(((www|maps)\.)?(google\.).*(\/maps)|maps\.(google\.).*)/;
  static redirects = ["https://openstreetmap.org"];
  static mapCentreRegex = /@(-?\d[0-9.]*),(-?\d[0-9.]*),(\d{1,2})[.z]/;
  static dataLatLngRegex = /(!3d|!4d)(-?[0-9]{1,10}.[0-9]{1,10})/g;
  static placeRegex = /\/place\/(.*)\//;
  static travelModes = {
    driving: "fossgis_osrm_car",
    walking: "fossgis_osrm_foot",
    bicycling: "fossgis_osrm_bike",
    transit: "fossgis_osrm_car", // not implemented on OSM, default to car.
  };
  static layers = {
    none: "S",
    transit: "T",
    traffic: "S", // not implemented on OSM, default to standard.
    bicycling: "C",
  };
  static addressToLatLng(address, callback) {
    const xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = () => {
      if (xmlhttp.readyState === XMLHttpRequest.DONE) {
        if (xmlhttp.status === 200) {
          const json = JSON.parse(xmlhttp.responseText)[0];
          if (json) {
            callback(
              `${json.lat}%2C${json.lon}`,
              `${json.boundingbox[2]},${json.boundingbox[1]},${json.boundingbox[3]},${json.boundingbox[0]}`
            );
          }
        } else {
          console.info("Error: Status is " + xmlhttp.status);
        }
      }
    };
    xmlhttp.open(
      "GET",
      `https://nominatim.openstreetmap.org/search/${address}?format=json&limit=1`,
      false
    );
    xmlhttp.send();
  }
}