【问题标题】:Changing javascript in google maps doesn't update it?更改谷歌地图中的 javascript 不会更新它?
【发布时间】:2016-11-26 10:55:48
【问题描述】:

我正在尝试制作 AJAX 地图 Google 地图,其中地点标记会更新,但地图不会更新。

我有一个带有 settimeout 函数的 ajax 设置,它返回新的 javascript。当新的 javascript 加载到我的 HTML 中时,这些更改不会反映在我的谷歌地图上。

当我进入 firefox 检查器并尝试操作 javascript(尝试更改标记 GPS 坐标)时,地图没有任何反应,并且点仍然相同。为什么这不会影响地图?

我查看了几个链接来尝试帮助我,但没有一个可以解释 javascript 背后的逻辑。

我的 PHP 脚本以纯文本形式返回 javascript。但是当这些得到 添加到HTML,谷歌地图没有变化,看起来像 在识别新的javascript之前需要重新初始化吗?

谁能解释我应该如何更新 javascript,以便地图重新以最新标记为中心并放置最新标记而不刷新页面/重新初始化地图?

 <div id="map"></div>
<script>

  function initMap() {
    var map = new google.maps.Map(document.getElementById('map'), {
     zoom: 13,
      center: {lat:  -20.530637, lng: 46.450046}
    });

    // Create an array of alphabetical characters used to label the markers.
    var labels = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';

    // Add some markers to the map.
    // Note: The code uses the JavaScript Array.prototype.map() method to
    // create an array of markers based on a given "locations" array.
    // The map() method here has nothing to do with the Google Maps API.
    var markers = locations.map(function(location, i) {
      return new google.maps.Marker({
        position: location,
        label: labels[i % labels.length]
      });
    });

    // Add a marker clusterer to manage the markers.
    var markerCluster = new MarkerClusterer(map, markers,
        {imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'});
  }
  var locations = [
      new google.maps.LatLng("-21.530637,46.450046),          
      new google.maps.LatLng("-22.530637,46.450046),

  ]
</script>
<script src="https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/markerclusterer.js">
</script>
<div class="result"></div>

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>
    function refresh_div() {
        jQuery.ajax({
            url:'content.php',
            type:'POST',
            success:function(results) {
                locations=results;

                jQuery(".result").html(results);
            }
        });
    }

    t = setInterval(refresh_div,5000);
</script>

我的“content.php”文件。以纯文本形式返回更多谷歌地图 LatLng 标记。

所以 PHP 输出是:

new google.maps.LatLng("-23.530637,46.450046"),
new google.maps.LatLng("-24.530637,46.450046"),
new google.maps.LatLng("-25.530637,46.450046"),
new google.maps.LatLng("-26.530637,46.450046")

【问题讨论】:

    标签: javascript php jquery google-maps google-maps-api-3


    【解决方案1】:

    我想总结一下你的 ajax 映射的行为:

    • 初始化地图(可能使用一组预定义的位置)
    • 重复调用 content.php 以检索标记的新更新
    • 在您的地图上绘制新标记

    问题在于您处理 ajax 结果的代码没有进行任何地图操作。事实上,它必须用新的位置集更新变量位置。然后使用更新的位置列表创建新标记,然后调用 MarkerCluster 应用它们。 我创建了一个小提琴来演示它:https://jsfiddle.net/anhhnt/paffzadz/1/ 我所做的是从 initMap 中提取标记创建部分,以便在更新位置后可以多次调用它。

    function updateMarker () {
      // Create an array of alphabetical characters used to label the markers.
      var labels = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    
      // Add some markers to the map.
      // Note: The code uses the JavaScript Array.prototype.map() method to
      // create an array of markers based on a given "locations" array.
      // The map() method here has nothing to do with the Google Maps API.
      var markers = locations.map(function(location, i) {
        return new google.maps.Marker({
        position: location,
        label: labels[i % labels.length]
      });
    });
    
    // Add a marker clusterer to manage the markers.
    var markerCluster = new MarkerClusterer(map, markers,
      {imagePath:     'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'});
    };
    function initMap() {
    
      window.map = new google.maps.Map(document.getElementById('map'), {
        zoom: 3,
        center: {lat: -28.024, lng: 140.887}
      });
      updateMarker(map);
    };
    var locations = [
      {lat: -31.563910, lng: 147.154312},
      {lat: -33.718234, lng: 150.363181},
      {lat: -33.727111, lng: 150.371124}
    ]
    function refresh_div() {
      jQuery.ajax({
      url:'/echo/json/',
      type:'POST',
      data: {
      json: JSON.stringify([
        {lat: -42.735258, lng: 147.438000},
        {lat: -43.999792, lng: 170.463352}
       ])
      },
     success:function(results) {
      locations.concat(results);
      updateMarker();
     }
    });
    }
    
    t = setInterval(refresh_div,5000);
    

    希望对你有帮助。

    【讨论】:

    • 非常感谢您的回复。对于 refresh_div() 函数,我的 PHP 需要输出 JSON 吗?
    • 我还要注意,您的小提琴没有显示任何内容。
    • 您可以使用任何您想要的格式,只要您的 JavaScript 代码可以理解并将result 更新为locations。但是,为了简单和标准,推荐使用 JSON
    • 我不知道为什么你看不到小提琴,它对我来说仍然显示得很好。
    • 对不起,我有点糊涂了希望你不要介意。我在哪里检索我的 PHP 文件发送的数据?是否可以在不使用 JSON.stringify 的情况下仅应用 php 输出?
    【解决方案2】:

    也许您的代码中有简单的语法错误 - 缺少右双引号:

     new google.maps.LatLng("-23.530637,46.450046"),
     ...
    

    ..或更好 - 删除第一个双引号:

    new google.maps.LatLng(-23.530637,46.450046),
     ...
    

    【讨论】:

    • 不,与此无关。很遗憾。我试过简单地将 PHP 脚本打印到位置标记中。它们都打印出来并显示在地图上。问题是当我去使用检查器调整 javascript 时,没有任何变化反映。也许地图需要重新初始化?
    • 当我把它放在 SO 上时,引号是我的错误
    • 当我使用 AJAX 更新位置标记时,我可以在 javascript 中看到它们,但在地图上没有任何变化。
    猜你喜欢
    • 1970-01-01
    • 2015-03-24
    • 2017-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-19
    • 2014-06-24
    • 1970-01-01
    相关资源
    最近更新 更多