I am faced with a dilemma that I tried to solve in various ways but without success.
I work with a form step by step and to move to the next point use:
@if($pasos == 1)
$this->pasos++;
Now within these steps I want to load a map from Google Maps and it does not recognize the scripts and I understand why the script is executed at the beginning and until it reaches step 1 this condition is not met @if ($ steps == 1).
Code structure:
@if($pasos == 1)
<div class="form-group">
<label for="direccion">Mapa</label>
<div wire:ignore>
<input
id="pac-input"
class="mt-2 controls form-control"
type="text"
placeholder="Buscar dirección o zona en el mapa"
/>
<div style="width: 100%; height: 450px">
<div id="map"></div>
</div>
</div>
</div>
@endif
If I take the map code out of the IF conditional it works perfect.
I don’t know if it is relevant but at the end of the view the script is executed:
<script>
function initAutocomplete() {
const map = new google.maps.Map(document.getElementById("map"), {
center: { lat: -35.2385, lng: -65.6740 },
zoom: 6,
mapTypeId: "roadmap",
});
// Create the search box and link it to the UI element.
const input = document.getElementById("pac-input");
const searchBox = new google.maps.places.SearchBox(input);
map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);
// Bias the SearchBox results towards current map's viewport.
map.addListener("bounds_changed", () => {
searchBox.setBounds(map.getBounds());
});
let markers = [];
// Listen for the event fired when the user selects a prediction and retrieve
// more details for that place.
searchBox.addListener("places_changed", () => {
const places = searchBox.getPlaces();
if (places.length == 0) {
return;
}
// Clear out the old markers.
markers.forEach((marker) => {
marker.setMap(null);
});
markers = [];
// For each place, get the icon, name and location.
const bounds = new google.maps.LatLngBounds();
places.forEach((place) => {
if (!place.geometry || !place.geometry.location) {
console.log("Returned place contains no geometry");
return;
}
const icon = {
url: place.icon,
size: new google.maps.Size(71, 71),
origin: new google.maps.Point(0, 0),
anchor: new google.maps.Point(17, 34),
scaledSize: new google.maps.Size(25, 25),
};
// Create a marker for each place.
markers.push(
new google.maps.Marker({
map,
icon,
title: place.name,
position: place.geometry.location,
})
);
if (place.geometry.viewport) {
// Only geocodes have viewport.
bounds.union(place.geometry.viewport);
} else {
bounds.extend(place.geometry.location);
}
/* SETEAMOS LAS VARIABLES DE LIVEWIRE LATITUD Y LONGITUD CON LOS VALORES SELECCIONADOS EN EL INPUT */
@this.set('latitud', place.geometry['location'].lat());
@this.set('longitud', place.geometry['location'].lng());
/* @this.set('direccion_propiedad', place.formatted_address); */
});
/* console.log(searchBox); */
map.fitBounds(bounds);
});
}
</script>
{{-- GOOGLE MAPS --}}
<script src="https://maps.googleapis.com/maps/api/js?key=MICLAVE&callback=initAutocomplete&libraries=places&v=weekly" async>
</script>
Is there a way to IGNORE the IF only for the MAP? Can you think of something that can be done in this very particular case?