【问题标题】:Ajax Load Google Map MarkersAjax 加载谷歌地图标记
【发布时间】:2018-07-17 15:35:07
【问题描述】:

我有一个谷歌地图,可以在页面加载时加载结果,这很好,但我有一个 ajax 搜索表单,它通过 ajax 在单独的 div 中更新结果,但它不会更新地图。我试图弄清楚当ajax调用完成时如何更新地图,但我被卡住了。任何帮助将不胜感激!

代码如下:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.1/jquery.min.js"></script>
<script src="https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/markerclusterer.js"></script>

<script>
$(document).ready(function() {


    var markersInfo = $('.ia-card').map(function() {

            var info = {
                id: $(this).data('map-id'),
                address: $(this).data('map-address'),
                title: $(this).data('map-title'),
                price: $(this).data('map-price'),
                latitude: $(this).data('map-latitude'),
                longitude: $(this).data('map-longitude'),
                html: "<img src=" + $(this).data('map-image') + ">",
                link: $(this).data("map-link"),
          contentHtml:  "<div class='image'>" + "<img src=" + $(this).data('map-image') + ">" + "</div>" + '<b>' + $(this).data('map-title') + '</b><br>' + "<div class='changeprice'><div style='display: none;' class='currency-selector'></div>" + $(this).data('map-price') + "</div>" + "<br><a href='" + $(this).data("map-link") + "'>More>></a>"
            };

        return info;
    }).get();


var distinctMarkerInfo = [];
markersInfo.forEach(function(item) {
    if (!distinctMarkerInfo.some(function(distinct) {
            return distinct.id == item.id;
        })) distinctMarkerInfo.push(item);
});

initGoogleMap(distinctMarkerInfo);




// GMAP ON SEARCH RESULTS PAGE
function initGoogleMap(markersInfo) {

    var mapOptions = {
        // zoom: 2,
        // center: new google.maps.LatLng(53.334430, -7.736673)
    },
    bounds = new google.maps.LatLngBounds(),
    mapElement = document.getElementById('stm_map_results'),
    map = new google.maps.Map(mapElement, mapOptions);
    markerList = []; // create an array to hold the markers

    var geocoder = new google.maps.Geocoder();


    var iconBase = '../assets/images/';



    $.each(markersInfo, function(key, val) {
        var marker = new google.maps.Marker({
            //map: map,
            position: {lat: parseFloat(val.latitude), lng: parseFloat(val.longitude)},
            title: val.title,
            icon: iconBase + 'single.png',
            info: new google.maps.InfoWindow({
                content: val.contentHtml
            })

        });


        markerList.push(marker); // add the marker to the list

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


        loc = new google.maps.LatLng(val.latitude, val.longitude);
            bounds.extend(loc);
    });


    map.fitBounds(bounds);
        map.panToBounds(bounds);

  var markerCluster = new MarkerClusterer(map, markerList, {
  imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'
  });


};


});
</script>

<div id="stm_map_results" style="width:100%; height:600px;"></div>

【问题讨论】:

    标签: javascript jquery ajax wordpress google-maps


    【解决方案1】:

    由于您通过MarkerClusterer 显示标记,我建议使用以下解决方案来更新地图。

    • 检索数据后,使用以下命令从地图中清除现有市场 MarkerCluster.clearMarkers函数
    • 用新数据初始化MarkerCluster

    以下示例演示如何“刷新”地图上的标记:

    function placeMarkers(data){
         var markers = data.map(function (item, i) {
              return new google.maps.Marker({
                  position: { lat: item.lat, lng: item.lng }
              });
         });
    
         //1.clear existing markers
         if (markerCluster)
              markerCluster.clearMarkers();
    
         //2.init marker cluster
         markerCluster = new MarkerClusterer(map, markers,
               { imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m' });
    
    }
    

    Demo

    【讨论】:

    • 我在一个单独的页面(例如 ajax.js)上运行了 ajax 函数,并且我有从另一个页面(例如 map.php)加载地图的代码,并且每当我尝试访问地图时js文件中php文件上的脚本我收到一条错误消息,指出函数未定义,因此我无法从外部重新初始化该页面的地图。请问有什么想法可以做到吗?
    • 我现在已经将 js 页面中的代码与 php 页面结合起来了,那么我应该将上面的代码放在我的代码中的什么位置呢?我有点不知道要换什么。我是 Javascript 的新手......谢谢
    猜你喜欢
    • 2015-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-08
    • 1970-01-01
    • 2021-09-13
    相关资源
    最近更新 更多