Snap to nearest street

萝らか妹 提交于 2019-11-29 04:53:35

Blackpool Community Church Javascript Team has an excellent example of exactly this (direct link to the fourth example). Check out their other examples as well.

(disclaimer: I'm not affiliated with them, but have learned a lot about GMaps from their examples)

Edit: I suspect the map events fire somewhat like this (pseudocode, for real event names etc. check the GMaps docs):

  • map click: mousedown, mouseup, click:{set red marker}
  • drag red marker: mousedown, dragstart{red marker}, mouseup, click:{set marker b} (mousedown+mouseup), dragend
  • both markers are set? Yes, get directions

What I'd suggest: in red-marker and marker-A dragstart functions, set some flag "dragging a marker", reset it in dragend function; in the Set marker B function, only set marker if we're currently NOT dragging something (flag is not set).

The code I gave you previously listened for the first two clicks, and added a marker for each. The problem is that when you drag the first marker, it's calling the "click" event again - and thus adding another marker at the same location.

Fortunately, the click event lets you know whether an overlay was clicked. So only execute the code that adds a new marker if overlay is null. Note that overlay is not a boolean.

var listener = GEvent.addListener(map, "click", function(overlay, latlng) {
  if (overlay == null) {
    // code to add new marker
  }
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!