dongyong6332 2016-06-13 07:13
浏览 7
已采纳

PHP:如何更改标记而不刷新PHP ajax调用中的谷歌地图?

I'm currently implementing google maps in my php project. I'm showing the markers according to database values. In ajax call the markers are changing. I want to show markers on google map with out refreshing the map.

function load() {

    var map = new google.maps.Map(document.getElementById("offers-map"), {
        center: new google.maps.LatLng(44.910398, -93.271976),
        zoom: 15,
        mapTypeId: 'roadmap'
      });
    var infoWindow = new google.maps.InfoWindow;
     /*  var infoWindow = new google.maps.InfoWindow(); */
      // Change this depending on the name of your PHP file
       $.ajax({  
            type: "POST",  
            url: "logic_abstract.php",   
            success: function(msg){ 
                //alert(msg);
                // call the count values
                countthedata(msg);
                msg = jQuery.parseJSON(msg);
                var len1 = Object.keys(msg).length;
                if(msg =="")
                {
                    swal("There is no data that matches your search criteria");
                }
                for (i=0;i<len1;i++){
                 var mapimgpath = msg[i].imagepath;
                 var type = msg[i].reason_name;
                 var zip = msg[i].zip;
                 var addr = msg[i].addr;
                 var repname = msg[i].repname;
                 var point = new google.maps.LatLng(parseFloat(msg[i].latitude), parseFloat(msg[i].longitude));
                 var html = "<div class='gm-style-iw'><b>" + type + "</b><br>" + repname + "<br>" + addr + " , " + zip+"</div>";
                 var icon='<?php echo  BASE_PATH;?>/mapview/'+mapimgpath;
                 marker = new google.maps.Marker({ map: map, position: point,icon: icon});
                 bindInfoWindow(marker, map, infoWindow, html);
                }
            }
        });    
    }
  • 写回答

1条回答 默认 最新

  • duanen19871021 2016-06-13 14:38
    关注
    //create map as global    
    var map = new google.maps.Map(document.getElementById("offers-map"), {
            center: new google.maps.LatLng(44.910398, -93.271976),
            zoom: 20,
            mapTypeId: 'roadmap'
          });
          //create global marker array
        var gMarkers=[];
          //global variable for length
        var locationLength=0;
        // call in loading time
    
    
        // remove the previous markers
            for(i=0; i<gMarkers.length; i++){
                gMarkers[i].setMap(null);
            }
            gMarkers=[];//empty the global marker array
    
         var map = new google.maps.Map(document.getElementById("offers-map"), {
                center: new google.maps.LatLng(44.910398, -93.271976),
                zoom: 15,
                mapTypeId: 'roadmap'
              });
            var infoWindow = new google.maps.InfoWindow;
             /*  var infoWindow = new google.maps.InfoWindow(); */
              // Change this depending on the name of your PHP file
               $.ajax({  
                    type: "POST",  
                    url: "logic_abstract.php",   
                    success: function(msg){ 
                        //alert(msg);
                        // call the count values
                        countthedata(msg);
                        msg = jQuery.parseJSON(msg);
                        var len1 = Object.keys(msg).length;
                        if(msg =="")
                        {
                            swal("There is no data that matches your search criteria");
                        }
                        for (i=0;i<len1;i++){
                         var mapimgpath = msg[i].imagepath;
                         var type = msg[i].reason_name;
                         var zip = msg[i].zip;
                         var addr = msg[i].addr;
                         var repname = msg[i].repname;
                         var point = new google.maps.LatLng(parseFloat(msg[i].latitude), parseFloat(msg[i].longitude));
                         var html = "<div class='gm-style-iw'><b>" + type + "</b><br>" + repname + "<br>" + addr + " , " + zip+"</div>";
                         var icon='<?php echo  BASE_PATH;?>/mapview/'+mapimgpath;
                         marker = new google.maps.Marker({ map: map, position: point,icon: icon});
                         bindInfoWindow(marker, map, infoWindow, html);
                        }
                    }
                });    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误