简体   繁体   English

点击事件在Google地图中显示标记

[英]Displaying marker in Google map on click event

I have the following script: 我有以下脚本:

<script src="https://maps.googleapis.com/maps/api/js?sensor=false&libraries=visualization">
</script>
<script>
   var map;

  function initialize() {
    var mapOptions = {
      zoom: 5,
      center: new google.maps.LatLng(2.8,-187.3),
      mapTypeId: google.maps.MapTypeId.TERRAIN
    };
    map = new google.maps.Map(document.getElementById('map_canvas'),
          mapOptions);
    var script = document.createElement('script');
    script.src = 'geojson';
    document.getElementsByTagName('head')[0].appendChild(script);
  }
    window.eqfeed_callback = function(results) {
    for (var i = 0; i < results.features.length; i++) {
      var coords = results.features[i].geometry.coordinates;
      var latLng = new google.maps.LatLng(coords[1],coords[0]);
      var marker = new google.maps.Marker({
        position: latLng,
        map: map
      });
    }
  }

</script>

As it is visible that the script.src is a file called 'geojson' I am developing a browser application where the user clicks on a year from a dropdown menu. 可以看出script.src是一个名为'geojson'的文件,我正在开发一个浏览器应用程序,用户在其中单击下拉菜单中的年份。 The back end scripts gets executed and it generates a new geojson file which includes new coordinates in geojson format. 后端脚本被执行,并生成一个新的geojson文件,其中包括geojson格式的新坐标。 however these positions are not visible in the current map until I reload the page. 但是这些位置在我重新加载页面之前在当前地图中不可见。 Is it possible to reload the map 1 or 2 seconds after the script execution or is there some way to send the data back through ajax so that the map detects the new positions and displays them. 是否可以在脚本执行后1或2秒钟重新加载地图,或者是否可以通过ajax将数据发送回去,以便地图检测到新位置并显示它们。 Thanx for any help. 感谢任何帮助。

I finally did this by calling this function initialize() on the click event. 我最终通过在click事件上调用此函数initialize()来完成此操作。 So that every time a user clicks on the button this function is called and now the script.src refers to the new geojson file formed by the script. 这样,每次用户单击按钮时都会调用此函数,现在script.src引用由脚本形成的新geojson文件。

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

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