最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论