adding locate me on form

  javascript, laravel, maps, php

am trying to do a simple project, and I need to have google maps with it, but I faced an issue trying to locate the user address on click action,

so what I need is to locate the client address whenever he clicks the button

Blade File

Script File

<script src="http://maps.google.com/maps/api/js?key=API KEY"></script>
<script>
    /* script */
    function initialize() {
        var latlng = new google.maps.LatLng(25.197433, 55.269497);
        var map = new google.maps.Map(document.getElementById('map'), {
            center: latlng,
            zoom: 10
        });
        var marker = new google.maps.Marker({
            map: map,
            position: latlng,
            draggable: true,
            anchorPoint: new google.maps.Point(0, -29)
        });
        var geocoder = new google.maps.Geocoder();
        var infowindow = new google.maps.InfoWindow();
       
        // this function will work on marker move event into map 
        google.maps.event.addListener(marker, 'dragend', function() {
            geocoder.geocode({
                'latLng': marker.getPosition()
            }, function(results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                    if (results[0]) {
                        bindDataToForm(results[0].formatted_address, marker.getPosition().lat(), marker.getPosition().lng());
                        infowindow.setContent(results[0].formatted_address);
                        infowindow.open(map, marker);
                    }
                }
            });
        });
    }

    function bindDataToForm(address, lat, lng) {
        document.getElementById('map_address').value = address;
        document.getElementById('latitude').value = lat;
        document.getElementById('longitude').value = lng;
    }
    google.maps.event.addDomListener(window, 'load', initialize);
</script>

Thank You in advance

Source: Ask Javascript Questions

LEAVE A COMMENT