Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Map jQuery - getting LatLng from mouse click

I'm currently using Google Maps v3 API with their jQuery client. I'm trying to get the latitude and longitude from a mouse click on the map, concatenate them to a string and add the string to an input field on the page. Being quite new to jQuery, I'm completely lost. Can anyone point me in the right direction? My code that initializes the Google Map on my page is as follows:

$(document).ready(function() { 
    var yourStartLatLng = new google.maps.LatLng(53.307697, -6.222317);
    $('#map-canvas').gmap({'center': yourStartLatLng, zoom: 15});
});
like image 485
Richard Stokes Avatar asked Jul 24 '12 13:07

Richard Stokes


1 Answers

$(document).ready(function() {
    var yourStartLatLng = new google.maps.LatLng(53.307697, -6.222317);
    $('#map_canvas').gmap({'center': yourStartLatLng, zoom: 15})
    .bind('init', function(event, map) { 
        $(map).click( function(event) {
            var lat=event.latLng.lat();
            var lng=event.latLng.lng();
            $('#latlng').val(lat+', '+lng); // 'latlng' is the id of the input
        });
    });
});

DEMO.

like image 126
The Alpha Avatar answered Oct 18 '22 01:10

The Alpha