Advertisement
fabiobiondi

Playing with Google Map API and Svelte

Mar 8th, 2021 (edited)
1,334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <script>
  2.   // by anonymous
  3.   import { onMount } from 'svelte';
  4.   import {TOKEN} from "../config";
  5.  
  6.   let cityInputTxt = '';
  7.   let map;
  8.   let panorama;
  9.   let marker;
  10.   let google = window.google;
  11.   // GEOCODING
  12.   const geocoder = new google.maps.Geocoder();
  13.  
  14.   onMount(() => {
  15.  
  16.     // MAP
  17.     const coords = { lat: 42.345573, lng: -71.098326 };
  18.     map = new google.maps.Map(document.getElementById("map0"), {
  19.       zoom: 15,
  20.       center: coords,
  21.     });
  22.  
  23.     // MARKER
  24.     marker = new google.maps.Marker({
  25.       position: coords,
  26.       map: map,
  27.       draggable: true,
  28.       animation: google.maps.Animation.DROP,
  29.     });
  30.     marker.addListener("dragend", () => {
  31.       // map.setZoom(8);
  32.       // console.log(marker.getPosition());
  33.       panorama.setPosition(marker.getPosition());
  34.       geocodeLatLng(marker.getPosition())
  35.     });
  36.  
  37.     // STREET VIEW
  38.     panorama = new google.maps.StreetViewPanorama(
  39.       document.getElementById("pano0"),
  40.       {
  41.         position: coords,
  42.         pov: {
  43.           heading: 34,
  44.           pitch: 10,
  45.         },
  46.       }
  47.     );
  48.     panorama.addListener("position_changed", () => {
  49.       // console.log(panorama.getPosition());
  50.       marker.setPosition(panorama.getPosition());
  51.       // const panoCell = document.getElementById("pano-cell");
  52.       // panoCell.innerHTML = panorama.getPano();
  53.     });
  54.  
  55.     // map.setStreetView(panorama);
  56.  
  57.     // PLACES
  58.     const options = {
  59.       componentRestrictions: { country: "it" },
  60.       fields: ["formatted_address", "geometry", "name"],
  61.       origin: map.getCenter(),
  62.       strictBounds: false,
  63.       types: ["geocode"],
  64.     };
  65.  
  66.     const autocomplete = new google.maps.places.Autocomplete(document.getElementById("myInput"), options);
  67.  
  68.     autocomplete.addListener("place_changed", () => {
  69.       const place = autocomplete.getPlace();
  70.       console.log(place.name)
  71.       console.log(place.geometry)
  72.       console.log(place.geometry.location)
  73.       console.log(place.geometry.viewport)
  74.  
  75.       // If the place has a geometry, then present it on a map.
  76.       if (place.geometry.viewport) {
  77.         map.fitBounds(place.geometry.viewport);
  78.       } else {
  79.         map.setCenter(place.geometry.location);
  80.         map.setZoom(17);
  81.       }
  82.       marker.setPosition(place.geometry.location);
  83.       marker.setVisible(true);
  84.  
  85.       panorama.setPosition(place.geometry.location)
  86.  
  87.     });
  88.  
  89.     function geocodeLatLng(coords) {
  90.       geocoder.geocode({ location: coords }, (results, status) => {
  91.         if (status === "OK") {
  92.           if (results[0]) {
  93.             // map.setZoom(11);
  94.             cityInputTxt = results[0].formatted_address;
  95.  
  96.             /*const marker = new google.maps.Marker({
  97.               position: coords,
  98.               map: map,
  99.             });*/
  100.           } else {
  101.             window.alert("No results found");
  102.           }
  103.         } else {
  104.           window.alert("Geocoder failed due to: " + status);
  105.         }
  106.       });
  107.     }
  108.  
  109.   })
  110.  
  111.   function getPlaces() {
  112.     fetch(`https://maps.googleapis.com/maps/api/place/autocomplete/json?input=Vict&types=geocode&language=it&key=${TOKEN}`)
  113.       .then(res => res.json())
  114.       .then(res => console.log(res))
  115.     // console.log('hello', marker.getPosition().lat(), marker.getPosition().lng());
  116.   }
  117.  
  118.  
  119.  
  120. </script>
  121.  
  122. Hello SImple Map
  123. <button on:click={getPlaces}>get Places</button>
  124.  
  125. <input type="text" id="myInput" bind:value={cityInputTxt}>
  126. <div id="map0"></div>
  127. <div id="pano0"></div>
  128. <style>
  129.   #map0, #pano0  {
  130.     width: 100%;
  131.     height: 300px;
  132.   }
  133. </style>
  134.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement