简体   繁体   English

google map API一起显示标记和融合层

[英]google map API display marker and fusion layer all together

Is it possibly to display marker and fusion layer at the same time on google map when click on check box? 单击复选框时是否可能在Google地图上同时显示标记和融合层?

This is what i have so far but it does not display anything when i click on the checkbox. 这是我到目前为止的内容,但是当我单击复选框时,它什么都不显示。 It only display when i put var marker and var layer inside initMap() function. 仅在将var markervar layer放入initMap()函数中时显示。 But not when I want to implement a check box 但是当我想要实现一个复选框时不是

function initMap() {
    var myLatLng = {lat: 38.5816, lng: -121.4944};

    return new google.maps.Map(document.getElementById('map'), {
           zoom: 4,
          center: myLatLng
    });
}

var map = initMap();


$(document).ready(function () {
    // If city is clicked
    $(".city-marker").click(function () {

        if(this.checked == true) {
            var marker = new google.maps.Marker({
                position: {lat: 38.5816, lng: -121.4944},
                map: map
            });
         }
    })

    // If county is clicked
    $(".county-border").click(function () {
         if(this.checked == true) {
               var layer = new google.maps.FusionTablesLayer({
                   query: {
                            select: '05000US06001',
                            from: '196LqydLhOq1Wl9612hNhcGoh4vUmRjTaiFvDhA',
                            where: "'County Name' = 'San Francisco'"
                          }
               });
              layer.setMap(map);
          }
     })
})

https://jsfiddle.net/tuyenle/j2rc2zvu/2/ https://jsfiddle.net/tuyenle/j2rc2zvu/2/

The problem with your code is, that you are loading google maps asynchronously (notice the async and defer tags) on this line: 您的代码存在的问题是,您正在此行上异步加载google地图(请注意asyncdefer标记):

<script async defer
src="https://maps.googleapis.com/maps/api/js?key=...&callback=initMap">
</script>

So only after it's loaded you can create the map and add Marker / Fusion Layer. 因此,只有在加载后,您才能创建地图并添加标记/融合层。 When it's loaded it would call initMap function (notice callback=initMap in the link) So when adding the marker, you should check if the map objects exist, one of possible solutions might something like this: 加载后,它将调用initMap函数(请注意链接中的callback=initMap )。因此,在添加标记时,应检查地图对象是否存在,一种可能的解决方案可能是这样的:

var map = null;
function initMap() { //is called when google maps api is loaded
    var myLatLng = {lat: 38.5816, lng: -121.4944};

    map = new google.maps.Map(document.getElementById('map'), {
           zoom: 4,
          center: myLatLng
    });
}

$(document).ready(function () {
    // If city is clicked
    $(".city-marker").click(function () {
        if(map == null){ //at this point the Google maps api is still not loaded, you can maybe display a loading bar or disable the checkboxes unit it is. Anyway, this will almost never happen if user is not on very very slow internet connection.
            console.log("Google maps not loaded yet"); 
            return;
        }
        if(this.checked == true) {
            var marker = new google.maps.Marker({
                position: {lat: 38.5816, lng: -121.4944},
                map: map
            });
         }
    });

    // If county is clicked
    $(".county-border").click(function () {
         if(map == null){
            console.log("Google maps not loaded yet"); 
            return;
        }
        if(this.checked == true) {
               var layer = new google.maps.FusionTablesLayer({
                   query: {
                            select: '05000US06001',
                            from: '196LqydLhOq1Wl9612hNhcGoh4vUmRjTaiFvDhA',
                            where: "'County Name' = 'San Francisco'"
                          }
               });
              layer.setMap(map);
          }
     })
});

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

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