谷歌地图点击事件移动标记
Google Maps Click Event to move the marker
我使用谷歌地图从用户那里获取坐标,拖动标记可以很好地工作,但我需要用点击事件来更改它。我在上尝试了"点击"而不是"拖动"
google.maps.event.addListener(marker, 'dragend', function(a)
不起作用的
我的代码是
var latlng = new google.maps.LatLng(<?php echo $villa['loc']; ?>);
var map = new google.maps.Map(document.getElementById('map'), {
center: latlng,
zoom: 5,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var marker = new google.maps.Marker({
position: latlng,
map: map,
title: 'move and pin exact point of your property',
draggable: true
});
google.maps.event.addListener(marker, 'dragend', function(a) {
var result = a.latLng.lat().toFixed(4) + ', ' + a.latLng.lng().toFixed(4);
$('#loc').val(result);
});
我从DB收到第一个标记位置。用户可以通过移动标记来改变位置,我会得到更新的结果,并将其写入数据库。拖动有效,但"单击"无效。感谢您的帮助。
<script src="http://maps.google.com/maps/api/js?sensor=false"></script>
找到解决方案
<script>
$(document).ready(function() {
var latlng = new google.maps.LatLng(<?php echo $villa['loc']; ?>);
var map = new google.maps.Map(document.getElementById('map'), {
center: latlng,
zoom: 12,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var marker = new google.maps.Marker({
position: latlng,
map: map,
title: 'move and pin exact point of your property',
draggable: true
});
google.maps.event.addListener(marker, 'dragend', function(a) {
var sonuc1 = a.latLng.lat().toFixed(4) + ', ' + a.latLng.lng().toFixed(4);
$('#loc').val(sonuc1);
});
function placeMarker(location) {
if ( marker ) {
marker.setPosition(location);
} else {
marker = new google.maps.Marker({
position: location,
map: map,
title: 'move and pin exact point of your property'
});
}
}
google.maps.event.addListener(map, 'click', function(event) {
placeMarker(event.latLng);
var sonuc2 = event.latLng.lat().toFixed(4) + ', ' + event.latLng.lng().toFixed(4);
$('#loc').val(sonuc2);
});
});
$("#il").change(function() {
var val = $('#il option:selected').val();
var sonuc3 = (val);
$('#sonucx').html(sonuc3);
var mappos = document.getElementById("il").options[document.getElementById("il").selectedIndex].getAttribute('latLng');
var latlngStr = mappos.split(',', 2);
var lat = parseFloat(latlngStr[0]);
var lng = parseFloat(latlngStr[1]);
var latlng = new google.maps.LatLng(lat, lng);
var map = new google.maps.Map(document.getElementById('map'), {
center: latlng,
zoom: 12,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var marker = new google.maps.Marker({
position: latlng,
map: map,
title: 'pin exact point of your property',
draggable: true
});
var sonuc4 = lat + "," + lng;
$('#loc').val(sonuc4);
google.maps.event.addListener(marker, 'dragend', function(a) {
var sonuc6 = a.latLng.lat().toFixed(4) + ', ' + a.latLng.lng().toFixed(4);
$('#loc').val(sonuc6);
});
function placeMarker(location) {
if ( marker ) {
marker.setPosition(location);
} else {
marker = new google.maps.Marker({
position: location,
map: map,
title: 'move and pin exact point of your property'
});
}
}
google.maps.event.addListener(map, 'click', function(event) {
placeMarker(event.latLng);
var sonuc5 = event.latLng.lat().toFixed(4) + ', ' + event.latLng.lng().toFixed(4);
$('#loc').val(sonuc5);
});
});
</script>
相关文章:
- 如何从画布上的某个移动事件中获取X和Y
- 如何在jquery中找到鼠标滚轮/触摸移动事件的走向
- 如何在鼠标移动事件时更改图像的窗口中心和宽度
- 如何从QT调用滑块移动事件
- 将stopImmediatePropagation()应用于所有jQuery移动事件
- 鼠标移动事件导致无限循环,即使在鼠标移动停止后也是如此
- Javascript move 元素与鼠标移动事件 60 FPS 请求动画帧
- 鼠标移动事件在火狐中不起作用
- 用于加载动态 JSON 数据的 JQuery 移动事件
- 在 IE11 中,不会为文本框触发指针移动事件
- 在jQuery或Javascript中是否有办法确定触摸移动事件的速度
- 如何从 jQuery 触发鼠标移动事件
- 科尔多瓦 - “触摸移动”事件不会立即触发
- 当绑定到多个元素时,Firefox 中的缓慢鼠标移动事件
- Backbone.View 的 setElement 不会移动事件侦听器
- 全屏模式下的 JavaScript 鼠标移动事件
- 需要触摸设备在覆盖另一个元素时保持触摸移动事件触发
- FabricJS:对象:移动事件不会在用户选择多个对象时触发
- 带有 JSON 数据的 JavaScript Kendo 调度程序仅移动事件一次
- 由谷歌地图绘制的矩形 绘图管理器不会触发鼠标移动事件