kim*_*ngh 2 javascript jquery google-maps
我有这个代码:'click dragend'事件的function()中的代码没有执行,文档中的latFld和llgFld没有更新,我该怎么办?
DEMO &&完整代码: http ://jsfiddle.net/DSc7r/
...
google.maps.event.addListener(map, 'click dragend', function (event) {
$('.MapLat').val(event.latLng.lat());
$('.MapLon').val(event.latLng.lng());
alert(event.latLng.place.name)
});
$("#searchTextField").focusin(function () {
$(document).keypress(function (e) {
if (e.which == 13) {
return false;
infowindow.close();
var firstResult = $(".pac-container .pac-item:first").text();
var geocoder = new google.maps.Geocoder();
geocoder.geocode({
"address": firstResult
}, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var lat = results[0].geometry.location.lat(),
lng = results[0].geometry.location.lng(),
placeName = results[0].address_components[0].long_name,
latlng = new google.maps.LatLng(lat, lng);
moveMarker(placeName, latlng);
$("input").val(firstResult);
alert(firstResult)
}
});
}
});
});
...
Run Code Online (Sandbox Code Playgroud)
首先是小提琴中的示例代码,autocomplete未定义,因此导致错误.据我所知,你不能一次绑定多个事件,所以你必须明确地绑定它们.
//So instead of
google.maps.event.addListener(map, 'click dragend', function (event) { ...
// You would want to do this
google.maps.event.addListener(map, 'click', function (event) { ...
// and
google.maps.event.addListener(marker, 'dragend', function (event) { ...
Run Code Online (Sandbox Code Playgroud)
还要注意的是,你不能从获得的经纬度dragend的地图,而是标记的,因此在dragend收听势必给marker
看看更新的小提琴http://jsfiddle.net/DSc7r/1/看什么我的意思是.
| 归档时间: |
|
| 查看次数: |
4171 次 |
| 最近记录: |