let autocomplete; let address1Field; let address2Field; let postalField; let infoWindow; let map; let marker; let clientCoordinates; let curloc; let locatieID; let delivery_fee_productID=0; let curloc_addres; function initAutocomplete() { const map = new google.maps.Map(document.getElementById("map"), { center: { lat: 5.839398, lng: -55.199089 }, zoom: 13, }); const infowindow = new google.maps.InfoWindow(); const infowindowContent = document.getElementById("infowindow-content"); infowindow.setContent(infowindowContent); const geocoder = new google.maps.Geocoder(); infoWindow = new google.maps.InfoWindow(); address1Field = document.querySelector("#adres"); // Create the autocomplete object, restricting the search predictions to // addresses in the US and Canada. autocomplete = new google.maps.places.Autocomplete(address1Field, { componentRestrictions: { country: ["sr"] }, fields: ["address_components", "geometry"], types: ["address"], }); address1Field.focus(); // When the user selects an address from the drop-down, populate the // address fields in the form. // autocomplete.addListener("place_changed", fillInAddress); console.log("before in place changed"); autocomplete.addListener("place_changed", () => { console.log("in place changed"); const place = autocomplete.getPlace(); show_google_map(); curloc_addres = place.formatted_address; console.log('adres', curloc_addres); //alert(curloc_addres); $('#adres_target').html(curloc_addres); infowindow.close(); const place_lat = autocomplete.getPlace().geometry.location.lat(); const place_lng = autocomplete.getPlace().geometry.location.lng(); const pos = { lat: place_lat, lng: place_lng, }; curloc = pos; Object.entries(locaties).forEach(([key, value]) => { const locatie_poly = value; const locatie = new google.maps.Polygon({ paths: locatie_poly }); var point = new google.maps.LatLng(curloc); const resultcheck = google.maps.geometry.poly.containsLocation( point, locatie ) if (resultcheck) { locatieID = key; console.log(locatieID); } }); //check delivery fee if (locatieID && !delivery_fee_productID) { Object.entries(locaties[locatieID]).forEach(([key, value]) => { const locatie_poly = value; const locatie = new google.maps.Polygon({ paths: locatie_poly }); var point = new google.maps.LatLng(curloc); const resultcheck = google.maps.geometry.poly.containsLocation( point, locatie ) if (resultcheck) { delivery_fee_productID = key; console.log(delivery_fee_productID); get_product_info_from_db_to_local_storage(delivery_fee_productID); } }); } const marker = new google.maps.Marker({ map: map }); // const latLng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude) map.setCenter(pos); map.setZoom(15); marker.setPosition(pos); marker.setVisible(true); console.log(place_lat,place_lng); if (!place_lat) { console.log("no latitude found"); return; } }); const locationButton = document.getElementById("zoek_restaurants"); locationButton.addEventListener("click", () => { // Try HTML5 geolocation. if (navigator.geolocation) { navigator.geolocation.getCurrentPosition( (position) => { const pos = { lat: position.coords.latitude, lng: position.coords.longitude, }; curloc = pos; getReverseGeocodingData(pos.lat,pos.lng); Object.entries(locaties).forEach(([key, value]) => { const locatie_poly = value; const locatie = new google.maps.Polygon({ paths: locatie_poly }); var point = new google.maps.LatLng(curloc); const resultcheck = google.maps.geometry.poly.containsLocation( point, locatie ) if (resultcheck) { locatieID = key; console.log(locatieID); } }); //check delivery fee if (locatieID) { Object.entries(delivery_fees[locatieID]).forEach(([key, value]) => { console.log(key,value); const locatie_poly = value; const locatie = new google.maps.Polygon({ paths: locatie_poly }); var point = new google.maps.LatLng(curloc); const resultcheck = google.maps.geometry.poly.containsLocation( point, locatie ) if (resultcheck) { delivery_fee_productID = key; console.log(delivery_fee_productID); get_product_info_from_db_to_local_storage(delivery_fee_productID); } }); } // infoWindow.setPosition(pos); // infoWindow.setContent("Location found."); // infoWindow.open(map); // console.log(pos); const marker = new google.maps.Marker({ map: map }); // const latLng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude) map.setCenter(pos); map.setZoom(15); marker.setPosition(pos); marker.setVisible(true); $('.loading').hide(); }, () => { alert('niet gelukt'); //handleLocationError(true, infoWindow, map.getCenter()); $('.loading').hide(); } ); } else { // Browser doesn't support Geolocation handleLocationError(false, infoWindow, map.getCenter()); } }); } function initMap() { const map = new google.maps.Map(document.getElementById("map"), { center: { lat: 5.839398, lng: -55.199089 }, zoom: 13, }); const input = document.getElementById("adres"); const autocomplete = new google.maps.places.Autocomplete(input, { componentRestrictions: { country: ["sr"] }, fields: ["address_components", "geometry"], types: ["address"], }); autocomplete.bindTo("bounds", map); // Specify just the place data fields that you need. autocomplete.setFields(["place_id", "geometry", "name", "formatted_address"]); map.controls[google.maps.ControlPosition.TOP_LEFT].push(input); autocomplete.addListener("place_changed", fillInAddress); const infowindow = new google.maps.InfoWindow(); const infowindowContent = document.getElementById("infowindow-content"); infowindow.setContent(infowindowContent); const geocoder = new google.maps.Geocoder(); const marker = new google.maps.Marker({ map: map }); marker.addListener("click", () => { infowindow.open(map, marker); }); autocomplete.addListener("place_changed", () => { infowindow.close(); const place = autocomplete.getPlace(); if (!place.place_id) { return; } geocoder .geocode({ placeId: place.place_id }) .then(({ results }) => { map.setZoom(11); map.setCenter(results[0].geometry.location); // Set the position of the marker using the place ID and location. marker.setPlace({ placeId: place.place_id, location: results[0].geometry.location, }); marker.setVisible(true); infowindowContent.children["place-name"].textContent = place.name; infowindowContent.children["place-id"].textContent = place.place_id; infowindowContent.children["place-address"].textContent = results[0].formatted_address; infowindow.open(map, marker); }) .catch((e) => window.alert("Geocoder failed due to: " + e)); }); infoWindow = new google.maps.InfoWindow(); const locationButton = document.getElementById("zoek_restaurants"); locationButton.addEventListener("click", () => { // Try HTML5 geolocation. if (navigator.geolocation) { navigator.geolocation.getCurrentPosition( (position) => { const pos = { lat: position.coords.latitude, lng: position.coords.longitude, }; infoWindow.setPosition(pos); infoWindow.setContent("Location found."); infoWindow.open(map); map.setCenter(pos); map.setZoom(15); marker.setPosition(pos); marker.setVisible(true); }, () => { handleLocationError(true, infoWindow, map.getCenter()); } ); } else { // Browser doesn't support Geolocation handleLocationError(false, infoWindow, map.getCenter()); } }); var triangleCoords_1 = [ new google.maps.LatLng(5.88695,-55.1175), new google.maps.LatLng(5.86849,-55.09875), new google.maps.LatLng(5.85328,-55.10414), new google.maps.LatLng(5.83808,-55.12018), new google.maps.LatLng(5.82625,-55.15272), new google.maps.LatLng(5.82205,-55.16208), new google.maps.LatLng(5.81341,-55.16904), new google.maps.LatLng(5.81224,-55.18138), new google.maps.LatLng(5.83378,-55.18734), new google.maps.LatLng(5.89716,-55.18455), new google.maps.LatLng(5.91152,-55.15463), new google.maps.LatLng(5.90231,-55.13435) ]; // Styling & Controls myPolygon_1 = new google.maps.Polygon({ paths: triangleCoords_1, draggable: false, // turn off if it gets annoying editable: false, strokeColor: '#e53935', strokeOpacity: 0.8, strokeWeight: 2, fillColor: '#e53935', fillOpacity: 0.35 }); myPolygon_1.setMap(map); } function handleLocationError(browserHasGeolocation, infoWindow, pos) { infoWindow.setPosition(pos); infoWindow.setContent( browserHasGeolocation ? "Error: The Geolocation service failed." : "Error: Your browser doesn't support geolocation." ); infoWindow.open(map); } function check_branche_and_delivery_fee(curloc){ } function check_location(curloc, curpolygon){ const resultcheck = google.maps.geometry.poly.containsLocation( curloc.latLng, curpolygon ) return resultcheck; } function getReverseGeocodingData(lat, lng) { var latlng = new google.maps.LatLng(lat, lng); // This is making the Geocode request var geocoder = new google.maps.Geocoder(); geocoder.geocode({ 'latLng': latlng }, (results, status) =>{ if (status !== google.maps.GeocoderStatus.OK) { alert(status); } // This is checking to see if the Geoeode Status is OK before proceeding if (status == google.maps.GeocoderStatus.OK) { console.log(results); curloc_addres = (results[0].formatted_address); //alert(curloc_addres); $('#adres_target').html(curloc_addres); } }); } $(document).on('click','#locatie_bevestigen','',function(){ console.log('curloc',curloc); })