javascript - Google Map jQuery - getting LatLng from mouse click - Stack Overflow

I'm currently using Google Maps v3 API with their jQuery client. I'm trying to get the latitu

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 pletely 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});
});

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 pletely 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});
});
Share Improve this question edited Jul 24, 2012 at 14:57 The Alpha 146k29 gold badges293 silver badges311 bronze badges asked Jul 24, 2012 at 13:28 Richard StokesRichard Stokes 3,5528 gold badges44 silver badges57 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7
$(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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742367225a4430556.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信