簡體   English   中英

Ajax加載Google地圖標記

[英]Ajax Load Google Map Markers

我有一個谷歌地圖,可以在頁面加載時加載結果,這很好,但是我有一個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>

由於您是通過MarkerClusterer顯示標記的, 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' });

}

演示版

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM