Search code examples
google-mapslatitude-longitudegoogle-latitude

Google Maps user location


I wanna set up a way in which users can place a marker on the map to tell their address and i should be able to extract the lat and long and store it in the database.

Is there somekinda plugin which i can modify and use ?


Solution

  • First create a function that a accepts a latlng object. This function will add the info to your database and then add the marker if it was successful.

    function addRestaurant( latlng ) {
        lat = latlng.lat;
        lng = latlng.lng;
        //Code to add restaurant
        if ( dbase_successful ) {
            var marker = new google.maps.Marker({
             position: latlng,
             title: "Some text"
                map: map //make your map global
            });
        }
    }
    

    Then add an event listener on the click event of the map that calls the function you just created. Add this to your map initialize code.

    google.maps.event.addListener(map, 'click', function(event) { addRestaurant( event.latlng ) } );
    

    Now when your map is clicked add_restaurant will be called with the latlng of the click event on the map.