how to select longitude and latitude in google maps laravel

Solutions on MaxInterview for how to select longitude and latitude in google maps laravel by the best coders in the world

showing results for - "how to select longitude and latitude in google maps laravel"
Maddie
22 Jun 2017
1function initialize() {
2
3    $('form').on('keyup keypress', function(e) {
4        var keyCode = e.keyCode || e.which;
5        if (keyCode === 13) {
6            e.preventDefault();
7            return false;
8        }
9    });
10    const locationInputs = document.getElementsByClassName("map-input");
11
12    const autocompletes = [];
13    const geocoder = new google.maps.Geocoder;
14    for (let i = 0; i < locationInputs.length; i++) {
15
16        const input = locationInputs[i];
17        const fieldKey = input.id.replace("-input", "");
18        const isEdit = document.getElementById(fieldKey + "-latitude").value != '' && document.getElementById(fieldKey + "-longitude").value != '';
19
20        const latitude = parseFloat(document.getElementById(fieldKey + "-latitude").value) || -33.8688;
21        const longitude = parseFloat(document.getElementById(fieldKey + "-longitude").value) || 151.2195;
22
23        const map = new google.maps.Map(document.getElementById(fieldKey + '-map'), {
24            center: {lat: latitude, lng: longitude},
25            zoom: 13
26        });
27        const marker = new google.maps.Marker({
28            map: map,
29            position: {lat: latitude, lng: longitude},
30        });
31
32        marker.setVisible(isEdit);
33
34        const autocomplete = new google.maps.places.Autocomplete(input);
35        autocomplete.key = fieldKey;
36        autocompletes.push({input: input, map: map, marker: marker, autocomplete: autocomplete});
37    }
38
39    for (let i = 0; i < autocompletes.length; i++) {
40        const input = autocompletes[i].input;
41        const autocomplete = autocompletes[i].autocomplete;
42        const map = autocompletes[i].map;
43        const marker = autocompletes[i].marker;
44
45        google.maps.event.addListener(autocomplete, 'place_changed', function () {
46            marker.setVisible(false);
47            const place = autocomplete.getPlace();
48
49            geocoder.geocode({'placeId': place.place_id}, function (results, status) {
50                if (status === google.maps.GeocoderStatus.OK) {
51                    const lat = results[0].geometry.location.lat();
52                    const lng = results[0].geometry.location.lng();
53                    setLocationCoordinates(autocomplete.key, lat, lng);
54                }
55            });
56
57            if (!place.geometry) {
58                window.alert("No details available for input: '" + place.name + "'");
59                input.value = "";
60                return;
61            }
62
63            if (place.geometry.viewport) {
64                map.fitBounds(place.geometry.viewport);
65            } else {
66                map.setCenter(place.geometry.location);
67                map.setZoom(17);
68            }
69            marker.setPosition(place.geometry.location);
70            marker.setVisible(true);
71
72        });
73    }
74}
75
76function setLocationCoordinates(key, lat, lng) {
77    const latitudeField = document.getElementById(key + "-" + "latitude");
78    const longitudeField = document.getElementById(key + "-" + "longitude");
79    latitudeField.value = lat;
80    longitudeField.value = lng;
81}