简体   繁体   English

Ajax加载Google地图标记

[英]Ajax Load Google Map Markers

I have a google map which loads results on page load which is fine but I have an ajax search form which updates the results by ajax in a separate div but it doesn't update the map. 我有一个谷歌地图,可以在页面加载时加载结果,这很好,但是我有一个ajax搜索表单,该表单通过ajax在单独的div中更新结果,但它不会更新地图。 I am trying to figure out how to update the map when the ajax call is completed but I am stuck. 我试图弄清楚ajax调用完成后如何更新地图,但我被卡住了。 Any help would be greatly appreciated! 任何帮助将不胜感激!

Here is the code: 这是代码:

<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>

Since you are displaying markers via MarkerClusterer i would propose the following solution to update the map. 由于您是通过MarkerClusterer显示标记的, MarkerClusterer我建议采用以下解决方案来更新地图。

  • once the data is retrieved, clear the existing markets from map using MarkerCluster.clearMarkers function 检索到数据后,使用MarkerCluster.clearMarkers函数从地图上清除现有市场
  • initialize a MarkerCluster with a new data 用新数据初始化MarkerCluster

The below example demonstrates how to "refresh" markers on the map: 下面的示例演示了如何“刷新”地图上的标记:

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 演示版

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

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