Pathik Vejani Pathik Vejani - 6 days ago 5
Javascript Question

how to get address on drag map pin

How can I get the address using map by dragging a pin?

here is the link which I am using the map : MAP-ADDRESS

The problem is, when I drag the pin that time latlong is changeable but the address is not.

javascript code:

$(function(){
$("#geocomplete").geocomplete({
map: ".map_canvas",
details: "form",
markerOptions: {
draggable: true
},
types: ["geocode", "establishment"]
});

// I can get lat long from here, but how to get the address?
$("#geocomplete").bind("geocode:dragged", function(event, latLng){
$("input[name=lat]").val(latLng.lat());
$("input[name=lng]").val(latLng.lng());
$("input[name=formatted_address]").val('address value');
$("#reset").show();
});
$("#find").click(function(){
$("#geocomplete").trigger("geocode");
});
});

Answer

https://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&key=YOUR_API_KEY

    $.ajax({
                    type: 'post',
                    dataType: "jsonp",
                    crossdomain: true,
                    url: 'https://maps.googleapis.com/maps/api/geocode/json',
                    data: {
                        key: 'key',
                        latlng: lat + "," + lng
                    },
                    success: function (json) {
                      //show on page
                    },
                    async: false,
                    error: function ()
                    {
                       console.log()
                    }
                });
Comments