简体   繁体   English

JavaFX:WebPane中的Google Maps-移动标记会留下旧标记

[英]JavaFX: Google Maps in a WebPane - Moving a Marker leaves old one behind

I've got an application that runs an instance of the Google Maps API in a JavaFX WebView, and am trying to allow the user to move the map marker around. 我有一个在JavaFX WebView中运行Google Maps API实例的应用程序,并试图允许用户在周围移动地图标记。

I've tried the following: 我尝试了以下方法:

google.maps.event.addListener(map, 'click', function(event) {
  marker.setPosition(event.latLng);
}

As well as: 以及:

google.maps.event.addListener(map, 'click', function(event) {
  marker.setMap(null);
  marker = null;
  marker = new google.maps.Marker({
    position:event.latLng,
    map: map
  });
}

Both of these implementations create the same issue: Clicking on the map creates a marker at the new position, but the old position marker remains on the screen as well. 这两种实现方式都会产生相同的问题:单击地图会在新位置创建一个标记,但是旧的位置标记也会保留在屏幕上。 Moving the map around and forcing the section with the old marker to reload removes that marker, leading me to believe this is not an issue of implementation but a bug in the web browser's handling of it. 移动地图并强制使用旧标记的部分重新加载会删除该标记,使我相信这不是实现的问题,而是Web浏览器处理它的错误。 Any way to fix this, so that there isn't a duplicate marker left behind? 有什么办法可以解决此问题,从而不会留下重复的标记?

I think this is a matter of code implementation. 我认为这是代码实现的问题。 Try this. 尝试这个。 Tested this on jsfiddle and it's working perfectly. jsfiddle上进行了测试 ,它运行良好。

function initMap() {
var myLatLng = {lat: -25.363, lng: 131.044};
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 4,
center: {lat: -25.363882, lng: 131.044922 }
});

var marker = new google.maps.Marker({
position: myLatLng,
map: map,
draggable: true,
});

google.maps.event.addListener(map, 'click', function(e) {
updateMarkerPosition(marker,e);
});
}

function updateMarkerPosition(marker, e){
marker.setPosition(e.latLng);
}

If this seamless execution is not reflected in your JavaFX application, then it's not Google Maps API that has the problem. 如果这种无缝执行未反映在您的JavaFX应用程序中,则不是Google Maps API出现了问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM