Google Maps Custom Tile Layer input onclick adds same layer as twice












0















I added gebco tiles on my google map with custom input control.



it looks like it works fine but i noticed that layer is added twice.
I created a sample fiddle showing that when you click the input it adds the gebco layer twice on console.



I also added a snippet if it is better. So I couldn't figure out why it adds the layer as twice.



Any ideas how to prevent it adding the gebco layer twice?






var map = new google.maps.Map(document.getElementById('map'), {
zoom: 2,
maxZoom: 8,
minZoom: 1,
center: {
lat: 0,
lng: -0
},
disableDefaultUI: false,
zoomControl: true,
mapTypeControl: false,
streetViewControl: false,
mapTypeId: google.maps.MapTypeId.ROADMAP
});

function CenterControl(controlDiv, map) {

// Bulut Custom Map Control
var controlUI = document.createElement('div');
controlUI.style.cursor = 'pointer';
controlUI.style.textAlign = 'center';
controlUI.title = 'Please Select a Weather Layer';
controlDiv.appendChild(controlUI);

var controlText = document.createElement('div');
controlText.innerHTML = '<table id="otherlayers"><tbody><tr><td><input type="checkbox" id="gebco" data-opacity="1"><label for="gebco">Wind Stream</label></td></tr></tbody></table>';
controlUI.appendChild(controlText);

$("div").on("click", "#otherlayers input[id='gebco']", function (e) {
setgebcolayer(this);
});

}

var gebcoLayer =
new google.maps.ImageMapType(
{
getTileUrl: function (coord, zoom) {
var tilesPerGlobe = 1 << zoom;
var x = coord.x % tilesPerGlobe;
if (x < 0) {
x = tilesPerGlobe + x;
}

var bbox2 = tileCoordsToBBox(map, coord, zoom, 256, 256);
var url = "https://www.gebco.net/data_and_products/gebco_web_services/web_map_service/mapserv?request=getmap&service=wms&BBOX=" + bbox2 + "&crs=EPSG:4326&format=image/png&layers=gebco_latest&width=256&height=256&version=1.3.0"
return url;
},
tileSize: new google.maps.Size(256, 256),
name: "Gebco",
isPng: true,
maxZoom: 18,
opacity: 0.3,
});

function setgebcolayer(control) {
var checked = control.checked;
if (checked == false) {
var layername = "Gebco";
for (_layer in map.overlayMapTypes.getArray()) {
if (map.overlayMapTypes.getAt(_layer).name == layername) {
if (checked == false) {
map.overlayMapTypes.removeAt(_layer);
return;
}
}
}
}
map.overlayMapTypes.insertAt(0, gebcoLayer);
}

function tileCoordsToBBox(map, coord, zoom, tileWidth, tileHeight) {
var proj = map.getProjection();
var scale = Math.pow(2, zoom);
var ne = proj.fromPointToLatLng(new google.maps.Point((coord.x + 1) * tileWidth / scale, coord.y * tileHeight / scale));
var sw = proj.fromPointToLatLng(new google.maps.Point(coord.x * tileWidth / scale, (coord.y + 1) * tileHeight / scale));

return [
sw.lat(),
sw.lng(),
ne.lat(),
ne.lng()
];
};

var centerControlDiv = document.createElement('div');
var centerControl = new CenterControl(centerControlDiv, map);

centerControlDiv.index = 1;
map.controls[google.maps.ControlPosition.TOP_LEFT].push(centerControlDiv);

google.maps.event.addListener(map, "zoom_changed", function () {
var z = map.getZoom();
var layerArray = map.overlayMapTypes.getArray();
console.log(layerArray);
});

html,
body,
#map {
height: 100%;
width: 100%;
padding: 0px;
margin: 0px;
}

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?v=3&libraries=geometry"></script>
<div id="map"></div>












share|improve this question



























    0















    I added gebco tiles on my google map with custom input control.



    it looks like it works fine but i noticed that layer is added twice.
    I created a sample fiddle showing that when you click the input it adds the gebco layer twice on console.



    I also added a snippet if it is better. So I couldn't figure out why it adds the layer as twice.



    Any ideas how to prevent it adding the gebco layer twice?






    var map = new google.maps.Map(document.getElementById('map'), {
    zoom: 2,
    maxZoom: 8,
    minZoom: 1,
    center: {
    lat: 0,
    lng: -0
    },
    disableDefaultUI: false,
    zoomControl: true,
    mapTypeControl: false,
    streetViewControl: false,
    mapTypeId: google.maps.MapTypeId.ROADMAP
    });

    function CenterControl(controlDiv, map) {

    // Bulut Custom Map Control
    var controlUI = document.createElement('div');
    controlUI.style.cursor = 'pointer';
    controlUI.style.textAlign = 'center';
    controlUI.title = 'Please Select a Weather Layer';
    controlDiv.appendChild(controlUI);

    var controlText = document.createElement('div');
    controlText.innerHTML = '<table id="otherlayers"><tbody><tr><td><input type="checkbox" id="gebco" data-opacity="1"><label for="gebco">Wind Stream</label></td></tr></tbody></table>';
    controlUI.appendChild(controlText);

    $("div").on("click", "#otherlayers input[id='gebco']", function (e) {
    setgebcolayer(this);
    });

    }

    var gebcoLayer =
    new google.maps.ImageMapType(
    {
    getTileUrl: function (coord, zoom) {
    var tilesPerGlobe = 1 << zoom;
    var x = coord.x % tilesPerGlobe;
    if (x < 0) {
    x = tilesPerGlobe + x;
    }

    var bbox2 = tileCoordsToBBox(map, coord, zoom, 256, 256);
    var url = "https://www.gebco.net/data_and_products/gebco_web_services/web_map_service/mapserv?request=getmap&service=wms&BBOX=" + bbox2 + "&crs=EPSG:4326&format=image/png&layers=gebco_latest&width=256&height=256&version=1.3.0"
    return url;
    },
    tileSize: new google.maps.Size(256, 256),
    name: "Gebco",
    isPng: true,
    maxZoom: 18,
    opacity: 0.3,
    });

    function setgebcolayer(control) {
    var checked = control.checked;
    if (checked == false) {
    var layername = "Gebco";
    for (_layer in map.overlayMapTypes.getArray()) {
    if (map.overlayMapTypes.getAt(_layer).name == layername) {
    if (checked == false) {
    map.overlayMapTypes.removeAt(_layer);
    return;
    }
    }
    }
    }
    map.overlayMapTypes.insertAt(0, gebcoLayer);
    }

    function tileCoordsToBBox(map, coord, zoom, tileWidth, tileHeight) {
    var proj = map.getProjection();
    var scale = Math.pow(2, zoom);
    var ne = proj.fromPointToLatLng(new google.maps.Point((coord.x + 1) * tileWidth / scale, coord.y * tileHeight / scale));
    var sw = proj.fromPointToLatLng(new google.maps.Point(coord.x * tileWidth / scale, (coord.y + 1) * tileHeight / scale));

    return [
    sw.lat(),
    sw.lng(),
    ne.lat(),
    ne.lng()
    ];
    };

    var centerControlDiv = document.createElement('div');
    var centerControl = new CenterControl(centerControlDiv, map);

    centerControlDiv.index = 1;
    map.controls[google.maps.ControlPosition.TOP_LEFT].push(centerControlDiv);

    google.maps.event.addListener(map, "zoom_changed", function () {
    var z = map.getZoom();
    var layerArray = map.overlayMapTypes.getArray();
    console.log(layerArray);
    });

    html,
    body,
    #map {
    height: 100%;
    width: 100%;
    padding: 0px;
    margin: 0px;
    }

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://maps.googleapis.com/maps/api/js?v=3&libraries=geometry"></script>
    <div id="map"></div>












    share|improve this question

























      0












      0








      0








      I added gebco tiles on my google map with custom input control.



      it looks like it works fine but i noticed that layer is added twice.
      I created a sample fiddle showing that when you click the input it adds the gebco layer twice on console.



      I also added a snippet if it is better. So I couldn't figure out why it adds the layer as twice.



      Any ideas how to prevent it adding the gebco layer twice?






      var map = new google.maps.Map(document.getElementById('map'), {
      zoom: 2,
      maxZoom: 8,
      minZoom: 1,
      center: {
      lat: 0,
      lng: -0
      },
      disableDefaultUI: false,
      zoomControl: true,
      mapTypeControl: false,
      streetViewControl: false,
      mapTypeId: google.maps.MapTypeId.ROADMAP
      });

      function CenterControl(controlDiv, map) {

      // Bulut Custom Map Control
      var controlUI = document.createElement('div');
      controlUI.style.cursor = 'pointer';
      controlUI.style.textAlign = 'center';
      controlUI.title = 'Please Select a Weather Layer';
      controlDiv.appendChild(controlUI);

      var controlText = document.createElement('div');
      controlText.innerHTML = '<table id="otherlayers"><tbody><tr><td><input type="checkbox" id="gebco" data-opacity="1"><label for="gebco">Wind Stream</label></td></tr></tbody></table>';
      controlUI.appendChild(controlText);

      $("div").on("click", "#otherlayers input[id='gebco']", function (e) {
      setgebcolayer(this);
      });

      }

      var gebcoLayer =
      new google.maps.ImageMapType(
      {
      getTileUrl: function (coord, zoom) {
      var tilesPerGlobe = 1 << zoom;
      var x = coord.x % tilesPerGlobe;
      if (x < 0) {
      x = tilesPerGlobe + x;
      }

      var bbox2 = tileCoordsToBBox(map, coord, zoom, 256, 256);
      var url = "https://www.gebco.net/data_and_products/gebco_web_services/web_map_service/mapserv?request=getmap&service=wms&BBOX=" + bbox2 + "&crs=EPSG:4326&format=image/png&layers=gebco_latest&width=256&height=256&version=1.3.0"
      return url;
      },
      tileSize: new google.maps.Size(256, 256),
      name: "Gebco",
      isPng: true,
      maxZoom: 18,
      opacity: 0.3,
      });

      function setgebcolayer(control) {
      var checked = control.checked;
      if (checked == false) {
      var layername = "Gebco";
      for (_layer in map.overlayMapTypes.getArray()) {
      if (map.overlayMapTypes.getAt(_layer).name == layername) {
      if (checked == false) {
      map.overlayMapTypes.removeAt(_layer);
      return;
      }
      }
      }
      }
      map.overlayMapTypes.insertAt(0, gebcoLayer);
      }

      function tileCoordsToBBox(map, coord, zoom, tileWidth, tileHeight) {
      var proj = map.getProjection();
      var scale = Math.pow(2, zoom);
      var ne = proj.fromPointToLatLng(new google.maps.Point((coord.x + 1) * tileWidth / scale, coord.y * tileHeight / scale));
      var sw = proj.fromPointToLatLng(new google.maps.Point(coord.x * tileWidth / scale, (coord.y + 1) * tileHeight / scale));

      return [
      sw.lat(),
      sw.lng(),
      ne.lat(),
      ne.lng()
      ];
      };

      var centerControlDiv = document.createElement('div');
      var centerControl = new CenterControl(centerControlDiv, map);

      centerControlDiv.index = 1;
      map.controls[google.maps.ControlPosition.TOP_LEFT].push(centerControlDiv);

      google.maps.event.addListener(map, "zoom_changed", function () {
      var z = map.getZoom();
      var layerArray = map.overlayMapTypes.getArray();
      console.log(layerArray);
      });

      html,
      body,
      #map {
      height: 100%;
      width: 100%;
      padding: 0px;
      margin: 0px;
      }

      <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
      <script src="https://maps.googleapis.com/maps/api/js?v=3&libraries=geometry"></script>
      <div id="map"></div>












      share|improve this question














      I added gebco tiles on my google map with custom input control.



      it looks like it works fine but i noticed that layer is added twice.
      I created a sample fiddle showing that when you click the input it adds the gebco layer twice on console.



      I also added a snippet if it is better. So I couldn't figure out why it adds the layer as twice.



      Any ideas how to prevent it adding the gebco layer twice?






      var map = new google.maps.Map(document.getElementById('map'), {
      zoom: 2,
      maxZoom: 8,
      minZoom: 1,
      center: {
      lat: 0,
      lng: -0
      },
      disableDefaultUI: false,
      zoomControl: true,
      mapTypeControl: false,
      streetViewControl: false,
      mapTypeId: google.maps.MapTypeId.ROADMAP
      });

      function CenterControl(controlDiv, map) {

      // Bulut Custom Map Control
      var controlUI = document.createElement('div');
      controlUI.style.cursor = 'pointer';
      controlUI.style.textAlign = 'center';
      controlUI.title = 'Please Select a Weather Layer';
      controlDiv.appendChild(controlUI);

      var controlText = document.createElement('div');
      controlText.innerHTML = '<table id="otherlayers"><tbody><tr><td><input type="checkbox" id="gebco" data-opacity="1"><label for="gebco">Wind Stream</label></td></tr></tbody></table>';
      controlUI.appendChild(controlText);

      $("div").on("click", "#otherlayers input[id='gebco']", function (e) {
      setgebcolayer(this);
      });

      }

      var gebcoLayer =
      new google.maps.ImageMapType(
      {
      getTileUrl: function (coord, zoom) {
      var tilesPerGlobe = 1 << zoom;
      var x = coord.x % tilesPerGlobe;
      if (x < 0) {
      x = tilesPerGlobe + x;
      }

      var bbox2 = tileCoordsToBBox(map, coord, zoom, 256, 256);
      var url = "https://www.gebco.net/data_and_products/gebco_web_services/web_map_service/mapserv?request=getmap&service=wms&BBOX=" + bbox2 + "&crs=EPSG:4326&format=image/png&layers=gebco_latest&width=256&height=256&version=1.3.0"
      return url;
      },
      tileSize: new google.maps.Size(256, 256),
      name: "Gebco",
      isPng: true,
      maxZoom: 18,
      opacity: 0.3,
      });

      function setgebcolayer(control) {
      var checked = control.checked;
      if (checked == false) {
      var layername = "Gebco";
      for (_layer in map.overlayMapTypes.getArray()) {
      if (map.overlayMapTypes.getAt(_layer).name == layername) {
      if (checked == false) {
      map.overlayMapTypes.removeAt(_layer);
      return;
      }
      }
      }
      }
      map.overlayMapTypes.insertAt(0, gebcoLayer);
      }

      function tileCoordsToBBox(map, coord, zoom, tileWidth, tileHeight) {
      var proj = map.getProjection();
      var scale = Math.pow(2, zoom);
      var ne = proj.fromPointToLatLng(new google.maps.Point((coord.x + 1) * tileWidth / scale, coord.y * tileHeight / scale));
      var sw = proj.fromPointToLatLng(new google.maps.Point(coord.x * tileWidth / scale, (coord.y + 1) * tileHeight / scale));

      return [
      sw.lat(),
      sw.lng(),
      ne.lat(),
      ne.lng()
      ];
      };

      var centerControlDiv = document.createElement('div');
      var centerControl = new CenterControl(centerControlDiv, map);

      centerControlDiv.index = 1;
      map.controls[google.maps.ControlPosition.TOP_LEFT].push(centerControlDiv);

      google.maps.event.addListener(map, "zoom_changed", function () {
      var z = map.getZoom();
      var layerArray = map.overlayMapTypes.getArray();
      console.log(layerArray);
      });

      html,
      body,
      #map {
      height: 100%;
      width: 100%;
      padding: 0px;
      margin: 0px;
      }

      <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
      <script src="https://maps.googleapis.com/maps/api/js?v=3&libraries=geometry"></script>
      <div id="map"></div>








      var map = new google.maps.Map(document.getElementById('map'), {
      zoom: 2,
      maxZoom: 8,
      minZoom: 1,
      center: {
      lat: 0,
      lng: -0
      },
      disableDefaultUI: false,
      zoomControl: true,
      mapTypeControl: false,
      streetViewControl: false,
      mapTypeId: google.maps.MapTypeId.ROADMAP
      });

      function CenterControl(controlDiv, map) {

      // Bulut Custom Map Control
      var controlUI = document.createElement('div');
      controlUI.style.cursor = 'pointer';
      controlUI.style.textAlign = 'center';
      controlUI.title = 'Please Select a Weather Layer';
      controlDiv.appendChild(controlUI);

      var controlText = document.createElement('div');
      controlText.innerHTML = '<table id="otherlayers"><tbody><tr><td><input type="checkbox" id="gebco" data-opacity="1"><label for="gebco">Wind Stream</label></td></tr></tbody></table>';
      controlUI.appendChild(controlText);

      $("div").on("click", "#otherlayers input[id='gebco']", function (e) {
      setgebcolayer(this);
      });

      }

      var gebcoLayer =
      new google.maps.ImageMapType(
      {
      getTileUrl: function (coord, zoom) {
      var tilesPerGlobe = 1 << zoom;
      var x = coord.x % tilesPerGlobe;
      if (x < 0) {
      x = tilesPerGlobe + x;
      }

      var bbox2 = tileCoordsToBBox(map, coord, zoom, 256, 256);
      var url = "https://www.gebco.net/data_and_products/gebco_web_services/web_map_service/mapserv?request=getmap&service=wms&BBOX=" + bbox2 + "&crs=EPSG:4326&format=image/png&layers=gebco_latest&width=256&height=256&version=1.3.0"
      return url;
      },
      tileSize: new google.maps.Size(256, 256),
      name: "Gebco",
      isPng: true,
      maxZoom: 18,
      opacity: 0.3,
      });

      function setgebcolayer(control) {
      var checked = control.checked;
      if (checked == false) {
      var layername = "Gebco";
      for (_layer in map.overlayMapTypes.getArray()) {
      if (map.overlayMapTypes.getAt(_layer).name == layername) {
      if (checked == false) {
      map.overlayMapTypes.removeAt(_layer);
      return;
      }
      }
      }
      }
      map.overlayMapTypes.insertAt(0, gebcoLayer);
      }

      function tileCoordsToBBox(map, coord, zoom, tileWidth, tileHeight) {
      var proj = map.getProjection();
      var scale = Math.pow(2, zoom);
      var ne = proj.fromPointToLatLng(new google.maps.Point((coord.x + 1) * tileWidth / scale, coord.y * tileHeight / scale));
      var sw = proj.fromPointToLatLng(new google.maps.Point(coord.x * tileWidth / scale, (coord.y + 1) * tileHeight / scale));

      return [
      sw.lat(),
      sw.lng(),
      ne.lat(),
      ne.lng()
      ];
      };

      var centerControlDiv = document.createElement('div');
      var centerControl = new CenterControl(centerControlDiv, map);

      centerControlDiv.index = 1;
      map.controls[google.maps.ControlPosition.TOP_LEFT].push(centerControlDiv);

      google.maps.event.addListener(map, "zoom_changed", function () {
      var z = map.getZoom();
      var layerArray = map.overlayMapTypes.getArray();
      console.log(layerArray);
      });

      html,
      body,
      #map {
      height: 100%;
      width: 100%;
      padding: 0px;
      margin: 0px;
      }

      <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
      <script src="https://maps.googleapis.com/maps/api/js?v=3&libraries=geometry"></script>
      <div id="map"></div>





      var map = new google.maps.Map(document.getElementById('map'), {
      zoom: 2,
      maxZoom: 8,
      minZoom: 1,
      center: {
      lat: 0,
      lng: -0
      },
      disableDefaultUI: false,
      zoomControl: true,
      mapTypeControl: false,
      streetViewControl: false,
      mapTypeId: google.maps.MapTypeId.ROADMAP
      });

      function CenterControl(controlDiv, map) {

      // Bulut Custom Map Control
      var controlUI = document.createElement('div');
      controlUI.style.cursor = 'pointer';
      controlUI.style.textAlign = 'center';
      controlUI.title = 'Please Select a Weather Layer';
      controlDiv.appendChild(controlUI);

      var controlText = document.createElement('div');
      controlText.innerHTML = '<table id="otherlayers"><tbody><tr><td><input type="checkbox" id="gebco" data-opacity="1"><label for="gebco">Wind Stream</label></td></tr></tbody></table>';
      controlUI.appendChild(controlText);

      $("div").on("click", "#otherlayers input[id='gebco']", function (e) {
      setgebcolayer(this);
      });

      }

      var gebcoLayer =
      new google.maps.ImageMapType(
      {
      getTileUrl: function (coord, zoom) {
      var tilesPerGlobe = 1 << zoom;
      var x = coord.x % tilesPerGlobe;
      if (x < 0) {
      x = tilesPerGlobe + x;
      }

      var bbox2 = tileCoordsToBBox(map, coord, zoom, 256, 256);
      var url = "https://www.gebco.net/data_and_products/gebco_web_services/web_map_service/mapserv?request=getmap&service=wms&BBOX=" + bbox2 + "&crs=EPSG:4326&format=image/png&layers=gebco_latest&width=256&height=256&version=1.3.0"
      return url;
      },
      tileSize: new google.maps.Size(256, 256),
      name: "Gebco",
      isPng: true,
      maxZoom: 18,
      opacity: 0.3,
      });

      function setgebcolayer(control) {
      var checked = control.checked;
      if (checked == false) {
      var layername = "Gebco";
      for (_layer in map.overlayMapTypes.getArray()) {
      if (map.overlayMapTypes.getAt(_layer).name == layername) {
      if (checked == false) {
      map.overlayMapTypes.removeAt(_layer);
      return;
      }
      }
      }
      }
      map.overlayMapTypes.insertAt(0, gebcoLayer);
      }

      function tileCoordsToBBox(map, coord, zoom, tileWidth, tileHeight) {
      var proj = map.getProjection();
      var scale = Math.pow(2, zoom);
      var ne = proj.fromPointToLatLng(new google.maps.Point((coord.x + 1) * tileWidth / scale, coord.y * tileHeight / scale));
      var sw = proj.fromPointToLatLng(new google.maps.Point(coord.x * tileWidth / scale, (coord.y + 1) * tileHeight / scale));

      return [
      sw.lat(),
      sw.lng(),
      ne.lat(),
      ne.lng()
      ];
      };

      var centerControlDiv = document.createElement('div');
      var centerControl = new CenterControl(centerControlDiv, map);

      centerControlDiv.index = 1;
      map.controls[google.maps.ControlPosition.TOP_LEFT].push(centerControlDiv);

      google.maps.event.addListener(map, "zoom_changed", function () {
      var z = map.getZoom();
      var layerArray = map.overlayMapTypes.getArray();
      console.log(layerArray);
      });

      html,
      body,
      #map {
      height: 100%;
      width: 100%;
      padding: 0px;
      margin: 0px;
      }

      <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
      <script src="https://maps.googleapis.com/maps/api/js?v=3&libraries=geometry"></script>
      <div id="map"></div>






      javascript jquery google-maps google-maps-api-3






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 28 '18 at 21:21









      Bulut KartalBulut Kartal

      34116




      34116
























          3 Answers
          3






          active

          oldest

          votes


















          1





          +100









          This will solve your issue:



          $("div").on("click", "#otherlayers input[id='gebco']", function (e) {
          e.stopPropagation()
          setgebcolayer(this);
          var layerArray = map.overlayMapTypes.getArray();
          console.log(layerArray);
          });


          you just have to add e.stopPropagation()



          Fiddle: link






          share|improve this answer































            2














            As you are using input checkbox, listening to the change event is the better choice as follows. It works!!



             $(document).on("change","input[id='gebco']",function(e){

            setgebcolayer(this);

            var layerArray = map.overlayMapTypes.getArray();
            console.log(layerArray);

            })





            var map = new google.maps.Map(document.getElementById('map'), {
            zoom: 2,
            maxZoom: 8,
            minZoom: 1,
            center: {
            lat: 0,
            lng: -0
            },
            disableDefaultUI: false,
            zoomControl: true,
            mapTypeControl: false,
            streetViewControl: false,
            mapTypeId: google.maps.MapTypeId.ROADMAP
            });

            function CenterControl(controlDiv, map) {

            // Bulut Custom Map Control
            var controlUI = document.createElement('div');
            controlUI.style.cursor = 'pointer';
            controlUI.style.textAlign = 'center';
            controlUI.title = 'Please Select a Weather Layer';
            controlDiv.appendChild(controlUI);

            var controlText = document.createElement('div');
            controlText.innerHTML = '<table id="otherlayers"><tbody><tr><td><input type="checkbox" id="gebco" data-opacity="1"><label for="gebco">Wind Stream</label></td></tr></tbody></table>';
            controlUI.appendChild(controlText);

            $(document).on("change","input[id='gebco']",function(e){

            setgebcolayer(this);

            var layerArray = map.overlayMapTypes.getArray();
            console.log(layerArray);

            })

            }

            var gebcoLayer =
            new google.maps.ImageMapType(
            {
            getTileUrl: function (coord, zoom) {
            var tilesPerGlobe = 1 << zoom;
            var x = coord.x % tilesPerGlobe;
            if (x < 0) {
            x = tilesPerGlobe + x;
            }

            var bbox2 = tileCoordsToBBox(map, coord, zoom, 256, 256);
            var url = "https://www.gebco.net/data_and_products/gebco_web_services/web_map_service/mapserv?request=getmap&service=wms&BBOX=" + bbox2 + "&crs=EPSG:4326&format=image/png&layers=gebco_latest&width=256&height=256&version=1.3.0"
            return url;
            },
            tileSize: new google.maps.Size(256, 256),
            name: "Gebco",
            isPng: true,
            maxZoom: 18,
            opacity: 0.3,
            });

            function setgebcolayer(control) {
            var checked = control.checked;
            if (checked == false) {
            var layername = "Gebco";
            for (_layer in map.overlayMapTypes.getArray()) {
            if (map.overlayMapTypes.getAt(_layer).name == layername) {
            if (checked == false) {
            map.overlayMapTypes.removeAt(_layer);
            return;
            }
            }
            }
            }
            map.overlayMapTypes.insertAt(0, gebcoLayer);
            }

            function tileCoordsToBBox(map, coord, zoom, tileWidth, tileHeight) {
            var proj = map.getProjection();
            var scale = Math.pow(2, zoom);
            var ne = proj.fromPointToLatLng(new google.maps.Point((coord.x + 1) * tileWidth / scale, coord.y * tileHeight / scale));
            var sw = proj.fromPointToLatLng(new google.maps.Point(coord.x * tileWidth / scale, (coord.y + 1) * tileHeight / scale));

            return [
            sw.lat(),
            sw.lng(),
            ne.lat(),
            ne.lng()
            ];
            };

            var centerControlDiv = document.createElement('div');
            var centerControl = new CenterControl(centerControlDiv, map);

            centerControlDiv.index = 1;
            map.controls[google.maps.ControlPosition.TOP_LEFT].push(centerControlDiv);

            google.maps.event.addListener(map, "zoom_changed", function () {
            var z = map.getZoom();
            var layerArray = map.overlayMapTypes.getArray();
            console.log(layerArray);
            });

            html,
            body,
            #map {
            height: 100%;
            width: 100%;
            padding: 0px;
            margin: 0px;
            }

            <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
            <script src="https://maps.googleapis.com/maps/api/js?v=3&libraries=geometry"></script>
            <div id="map"></div>








            share|improve this answer
























            • @Bulut Kartal Is the above answer solved your issue completely or do you need further help on it?

              – Vishnu Vishwa
              Dec 5 '18 at 17:56



















            1














            JQuery is running the click event twice. The line below triggers twice on a click:



            $("div").on("click", "#otherlayers input[id='gebco']", function (e) {   





            share|improve this answer
























            • thank you for your answer. I noticed that too. How can I prevent it?

              – Bulut Kartal
              Dec 2 '18 at 2:25












            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53528305%2fgoogle-maps-custom-tile-layer-input-onclick-adds-same-layer-as-twice%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1





            +100









            This will solve your issue:



            $("div").on("click", "#otherlayers input[id='gebco']", function (e) {
            e.stopPropagation()
            setgebcolayer(this);
            var layerArray = map.overlayMapTypes.getArray();
            console.log(layerArray);
            });


            you just have to add e.stopPropagation()



            Fiddle: link






            share|improve this answer




























              1





              +100









              This will solve your issue:



              $("div").on("click", "#otherlayers input[id='gebco']", function (e) {
              e.stopPropagation()
              setgebcolayer(this);
              var layerArray = map.overlayMapTypes.getArray();
              console.log(layerArray);
              });


              you just have to add e.stopPropagation()



              Fiddle: link






              share|improve this answer


























                1





                +100







                1





                +100



                1




                +100





                This will solve your issue:



                $("div").on("click", "#otherlayers input[id='gebco']", function (e) {
                e.stopPropagation()
                setgebcolayer(this);
                var layerArray = map.overlayMapTypes.getArray();
                console.log(layerArray);
                });


                you just have to add e.stopPropagation()



                Fiddle: link






                share|improve this answer













                This will solve your issue:



                $("div").on("click", "#otherlayers input[id='gebco']", function (e) {
                e.stopPropagation()
                setgebcolayer(this);
                var layerArray = map.overlayMapTypes.getArray();
                console.log(layerArray);
                });


                you just have to add e.stopPropagation()



                Fiddle: link







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Dec 7 '18 at 21:58









                ObservablerxjsObservablerxjs

                407112




                407112

























                    2














                    As you are using input checkbox, listening to the change event is the better choice as follows. It works!!



                     $(document).on("change","input[id='gebco']",function(e){

                    setgebcolayer(this);

                    var layerArray = map.overlayMapTypes.getArray();
                    console.log(layerArray);

                    })





                    var map = new google.maps.Map(document.getElementById('map'), {
                    zoom: 2,
                    maxZoom: 8,
                    minZoom: 1,
                    center: {
                    lat: 0,
                    lng: -0
                    },
                    disableDefaultUI: false,
                    zoomControl: true,
                    mapTypeControl: false,
                    streetViewControl: false,
                    mapTypeId: google.maps.MapTypeId.ROADMAP
                    });

                    function CenterControl(controlDiv, map) {

                    // Bulut Custom Map Control
                    var controlUI = document.createElement('div');
                    controlUI.style.cursor = 'pointer';
                    controlUI.style.textAlign = 'center';
                    controlUI.title = 'Please Select a Weather Layer';
                    controlDiv.appendChild(controlUI);

                    var controlText = document.createElement('div');
                    controlText.innerHTML = '<table id="otherlayers"><tbody><tr><td><input type="checkbox" id="gebco" data-opacity="1"><label for="gebco">Wind Stream</label></td></tr></tbody></table>';
                    controlUI.appendChild(controlText);

                    $(document).on("change","input[id='gebco']",function(e){

                    setgebcolayer(this);

                    var layerArray = map.overlayMapTypes.getArray();
                    console.log(layerArray);

                    })

                    }

                    var gebcoLayer =
                    new google.maps.ImageMapType(
                    {
                    getTileUrl: function (coord, zoom) {
                    var tilesPerGlobe = 1 << zoom;
                    var x = coord.x % tilesPerGlobe;
                    if (x < 0) {
                    x = tilesPerGlobe + x;
                    }

                    var bbox2 = tileCoordsToBBox(map, coord, zoom, 256, 256);
                    var url = "https://www.gebco.net/data_and_products/gebco_web_services/web_map_service/mapserv?request=getmap&service=wms&BBOX=" + bbox2 + "&crs=EPSG:4326&format=image/png&layers=gebco_latest&width=256&height=256&version=1.3.0"
                    return url;
                    },
                    tileSize: new google.maps.Size(256, 256),
                    name: "Gebco",
                    isPng: true,
                    maxZoom: 18,
                    opacity: 0.3,
                    });

                    function setgebcolayer(control) {
                    var checked = control.checked;
                    if (checked == false) {
                    var layername = "Gebco";
                    for (_layer in map.overlayMapTypes.getArray()) {
                    if (map.overlayMapTypes.getAt(_layer).name == layername) {
                    if (checked == false) {
                    map.overlayMapTypes.removeAt(_layer);
                    return;
                    }
                    }
                    }
                    }
                    map.overlayMapTypes.insertAt(0, gebcoLayer);
                    }

                    function tileCoordsToBBox(map, coord, zoom, tileWidth, tileHeight) {
                    var proj = map.getProjection();
                    var scale = Math.pow(2, zoom);
                    var ne = proj.fromPointToLatLng(new google.maps.Point((coord.x + 1) * tileWidth / scale, coord.y * tileHeight / scale));
                    var sw = proj.fromPointToLatLng(new google.maps.Point(coord.x * tileWidth / scale, (coord.y + 1) * tileHeight / scale));

                    return [
                    sw.lat(),
                    sw.lng(),
                    ne.lat(),
                    ne.lng()
                    ];
                    };

                    var centerControlDiv = document.createElement('div');
                    var centerControl = new CenterControl(centerControlDiv, map);

                    centerControlDiv.index = 1;
                    map.controls[google.maps.ControlPosition.TOP_LEFT].push(centerControlDiv);

                    google.maps.event.addListener(map, "zoom_changed", function () {
                    var z = map.getZoom();
                    var layerArray = map.overlayMapTypes.getArray();
                    console.log(layerArray);
                    });

                    html,
                    body,
                    #map {
                    height: 100%;
                    width: 100%;
                    padding: 0px;
                    margin: 0px;
                    }

                    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
                    <script src="https://maps.googleapis.com/maps/api/js?v=3&libraries=geometry"></script>
                    <div id="map"></div>








                    share|improve this answer
























                    • @Bulut Kartal Is the above answer solved your issue completely or do you need further help on it?

                      – Vishnu Vishwa
                      Dec 5 '18 at 17:56
















                    2














                    As you are using input checkbox, listening to the change event is the better choice as follows. It works!!



                     $(document).on("change","input[id='gebco']",function(e){

                    setgebcolayer(this);

                    var layerArray = map.overlayMapTypes.getArray();
                    console.log(layerArray);

                    })





                    var map = new google.maps.Map(document.getElementById('map'), {
                    zoom: 2,
                    maxZoom: 8,
                    minZoom: 1,
                    center: {
                    lat: 0,
                    lng: -0
                    },
                    disableDefaultUI: false,
                    zoomControl: true,
                    mapTypeControl: false,
                    streetViewControl: false,
                    mapTypeId: google.maps.MapTypeId.ROADMAP
                    });

                    function CenterControl(controlDiv, map) {

                    // Bulut Custom Map Control
                    var controlUI = document.createElement('div');
                    controlUI.style.cursor = 'pointer';
                    controlUI.style.textAlign = 'center';
                    controlUI.title = 'Please Select a Weather Layer';
                    controlDiv.appendChild(controlUI);

                    var controlText = document.createElement('div');
                    controlText.innerHTML = '<table id="otherlayers"><tbody><tr><td><input type="checkbox" id="gebco" data-opacity="1"><label for="gebco">Wind Stream</label></td></tr></tbody></table>';
                    controlUI.appendChild(controlText);

                    $(document).on("change","input[id='gebco']",function(e){

                    setgebcolayer(this);

                    var layerArray = map.overlayMapTypes.getArray();
                    console.log(layerArray);

                    })

                    }

                    var gebcoLayer =
                    new google.maps.ImageMapType(
                    {
                    getTileUrl: function (coord, zoom) {
                    var tilesPerGlobe = 1 << zoom;
                    var x = coord.x % tilesPerGlobe;
                    if (x < 0) {
                    x = tilesPerGlobe + x;
                    }

                    var bbox2 = tileCoordsToBBox(map, coord, zoom, 256, 256);
                    var url = "https://www.gebco.net/data_and_products/gebco_web_services/web_map_service/mapserv?request=getmap&service=wms&BBOX=" + bbox2 + "&crs=EPSG:4326&format=image/png&layers=gebco_latest&width=256&height=256&version=1.3.0"
                    return url;
                    },
                    tileSize: new google.maps.Size(256, 256),
                    name: "Gebco",
                    isPng: true,
                    maxZoom: 18,
                    opacity: 0.3,
                    });

                    function setgebcolayer(control) {
                    var checked = control.checked;
                    if (checked == false) {
                    var layername = "Gebco";
                    for (_layer in map.overlayMapTypes.getArray()) {
                    if (map.overlayMapTypes.getAt(_layer).name == layername) {
                    if (checked == false) {
                    map.overlayMapTypes.removeAt(_layer);
                    return;
                    }
                    }
                    }
                    }
                    map.overlayMapTypes.insertAt(0, gebcoLayer);
                    }

                    function tileCoordsToBBox(map, coord, zoom, tileWidth, tileHeight) {
                    var proj = map.getProjection();
                    var scale = Math.pow(2, zoom);
                    var ne = proj.fromPointToLatLng(new google.maps.Point((coord.x + 1) * tileWidth / scale, coord.y * tileHeight / scale));
                    var sw = proj.fromPointToLatLng(new google.maps.Point(coord.x * tileWidth / scale, (coord.y + 1) * tileHeight / scale));

                    return [
                    sw.lat(),
                    sw.lng(),
                    ne.lat(),
                    ne.lng()
                    ];
                    };

                    var centerControlDiv = document.createElement('div');
                    var centerControl = new CenterControl(centerControlDiv, map);

                    centerControlDiv.index = 1;
                    map.controls[google.maps.ControlPosition.TOP_LEFT].push(centerControlDiv);

                    google.maps.event.addListener(map, "zoom_changed", function () {
                    var z = map.getZoom();
                    var layerArray = map.overlayMapTypes.getArray();
                    console.log(layerArray);
                    });

                    html,
                    body,
                    #map {
                    height: 100%;
                    width: 100%;
                    padding: 0px;
                    margin: 0px;
                    }

                    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
                    <script src="https://maps.googleapis.com/maps/api/js?v=3&libraries=geometry"></script>
                    <div id="map"></div>








                    share|improve this answer
























                    • @Bulut Kartal Is the above answer solved your issue completely or do you need further help on it?

                      – Vishnu Vishwa
                      Dec 5 '18 at 17:56














                    2












                    2








                    2







                    As you are using input checkbox, listening to the change event is the better choice as follows. It works!!



                     $(document).on("change","input[id='gebco']",function(e){

                    setgebcolayer(this);

                    var layerArray = map.overlayMapTypes.getArray();
                    console.log(layerArray);

                    })





                    var map = new google.maps.Map(document.getElementById('map'), {
                    zoom: 2,
                    maxZoom: 8,
                    minZoom: 1,
                    center: {
                    lat: 0,
                    lng: -0
                    },
                    disableDefaultUI: false,
                    zoomControl: true,
                    mapTypeControl: false,
                    streetViewControl: false,
                    mapTypeId: google.maps.MapTypeId.ROADMAP
                    });

                    function CenterControl(controlDiv, map) {

                    // Bulut Custom Map Control
                    var controlUI = document.createElement('div');
                    controlUI.style.cursor = 'pointer';
                    controlUI.style.textAlign = 'center';
                    controlUI.title = 'Please Select a Weather Layer';
                    controlDiv.appendChild(controlUI);

                    var controlText = document.createElement('div');
                    controlText.innerHTML = '<table id="otherlayers"><tbody><tr><td><input type="checkbox" id="gebco" data-opacity="1"><label for="gebco">Wind Stream</label></td></tr></tbody></table>';
                    controlUI.appendChild(controlText);

                    $(document).on("change","input[id='gebco']",function(e){

                    setgebcolayer(this);

                    var layerArray = map.overlayMapTypes.getArray();
                    console.log(layerArray);

                    })

                    }

                    var gebcoLayer =
                    new google.maps.ImageMapType(
                    {
                    getTileUrl: function (coord, zoom) {
                    var tilesPerGlobe = 1 << zoom;
                    var x = coord.x % tilesPerGlobe;
                    if (x < 0) {
                    x = tilesPerGlobe + x;
                    }

                    var bbox2 = tileCoordsToBBox(map, coord, zoom, 256, 256);
                    var url = "https://www.gebco.net/data_and_products/gebco_web_services/web_map_service/mapserv?request=getmap&service=wms&BBOX=" + bbox2 + "&crs=EPSG:4326&format=image/png&layers=gebco_latest&width=256&height=256&version=1.3.0"
                    return url;
                    },
                    tileSize: new google.maps.Size(256, 256),
                    name: "Gebco",
                    isPng: true,
                    maxZoom: 18,
                    opacity: 0.3,
                    });

                    function setgebcolayer(control) {
                    var checked = control.checked;
                    if (checked == false) {
                    var layername = "Gebco";
                    for (_layer in map.overlayMapTypes.getArray()) {
                    if (map.overlayMapTypes.getAt(_layer).name == layername) {
                    if (checked == false) {
                    map.overlayMapTypes.removeAt(_layer);
                    return;
                    }
                    }
                    }
                    }
                    map.overlayMapTypes.insertAt(0, gebcoLayer);
                    }

                    function tileCoordsToBBox(map, coord, zoom, tileWidth, tileHeight) {
                    var proj = map.getProjection();
                    var scale = Math.pow(2, zoom);
                    var ne = proj.fromPointToLatLng(new google.maps.Point((coord.x + 1) * tileWidth / scale, coord.y * tileHeight / scale));
                    var sw = proj.fromPointToLatLng(new google.maps.Point(coord.x * tileWidth / scale, (coord.y + 1) * tileHeight / scale));

                    return [
                    sw.lat(),
                    sw.lng(),
                    ne.lat(),
                    ne.lng()
                    ];
                    };

                    var centerControlDiv = document.createElement('div');
                    var centerControl = new CenterControl(centerControlDiv, map);

                    centerControlDiv.index = 1;
                    map.controls[google.maps.ControlPosition.TOP_LEFT].push(centerControlDiv);

                    google.maps.event.addListener(map, "zoom_changed", function () {
                    var z = map.getZoom();
                    var layerArray = map.overlayMapTypes.getArray();
                    console.log(layerArray);
                    });

                    html,
                    body,
                    #map {
                    height: 100%;
                    width: 100%;
                    padding: 0px;
                    margin: 0px;
                    }

                    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
                    <script src="https://maps.googleapis.com/maps/api/js?v=3&libraries=geometry"></script>
                    <div id="map"></div>








                    share|improve this answer













                    As you are using input checkbox, listening to the change event is the better choice as follows. It works!!



                     $(document).on("change","input[id='gebco']",function(e){

                    setgebcolayer(this);

                    var layerArray = map.overlayMapTypes.getArray();
                    console.log(layerArray);

                    })





                    var map = new google.maps.Map(document.getElementById('map'), {
                    zoom: 2,
                    maxZoom: 8,
                    minZoom: 1,
                    center: {
                    lat: 0,
                    lng: -0
                    },
                    disableDefaultUI: false,
                    zoomControl: true,
                    mapTypeControl: false,
                    streetViewControl: false,
                    mapTypeId: google.maps.MapTypeId.ROADMAP
                    });

                    function CenterControl(controlDiv, map) {

                    // Bulut Custom Map Control
                    var controlUI = document.createElement('div');
                    controlUI.style.cursor = 'pointer';
                    controlUI.style.textAlign = 'center';
                    controlUI.title = 'Please Select a Weather Layer';
                    controlDiv.appendChild(controlUI);

                    var controlText = document.createElement('div');
                    controlText.innerHTML = '<table id="otherlayers"><tbody><tr><td><input type="checkbox" id="gebco" data-opacity="1"><label for="gebco">Wind Stream</label></td></tr></tbody></table>';
                    controlUI.appendChild(controlText);

                    $(document).on("change","input[id='gebco']",function(e){

                    setgebcolayer(this);

                    var layerArray = map.overlayMapTypes.getArray();
                    console.log(layerArray);

                    })

                    }

                    var gebcoLayer =
                    new google.maps.ImageMapType(
                    {
                    getTileUrl: function (coord, zoom) {
                    var tilesPerGlobe = 1 << zoom;
                    var x = coord.x % tilesPerGlobe;
                    if (x < 0) {
                    x = tilesPerGlobe + x;
                    }

                    var bbox2 = tileCoordsToBBox(map, coord, zoom, 256, 256);
                    var url = "https://www.gebco.net/data_and_products/gebco_web_services/web_map_service/mapserv?request=getmap&service=wms&BBOX=" + bbox2 + "&crs=EPSG:4326&format=image/png&layers=gebco_latest&width=256&height=256&version=1.3.0"
                    return url;
                    },
                    tileSize: new google.maps.Size(256, 256),
                    name: "Gebco",
                    isPng: true,
                    maxZoom: 18,
                    opacity: 0.3,
                    });

                    function setgebcolayer(control) {
                    var checked = control.checked;
                    if (checked == false) {
                    var layername = "Gebco";
                    for (_layer in map.overlayMapTypes.getArray()) {
                    if (map.overlayMapTypes.getAt(_layer).name == layername) {
                    if (checked == false) {
                    map.overlayMapTypes.removeAt(_layer);
                    return;
                    }
                    }
                    }
                    }
                    map.overlayMapTypes.insertAt(0, gebcoLayer);
                    }

                    function tileCoordsToBBox(map, coord, zoom, tileWidth, tileHeight) {
                    var proj = map.getProjection();
                    var scale = Math.pow(2, zoom);
                    var ne = proj.fromPointToLatLng(new google.maps.Point((coord.x + 1) * tileWidth / scale, coord.y * tileHeight / scale));
                    var sw = proj.fromPointToLatLng(new google.maps.Point(coord.x * tileWidth / scale, (coord.y + 1) * tileHeight / scale));

                    return [
                    sw.lat(),
                    sw.lng(),
                    ne.lat(),
                    ne.lng()
                    ];
                    };

                    var centerControlDiv = document.createElement('div');
                    var centerControl = new CenterControl(centerControlDiv, map);

                    centerControlDiv.index = 1;
                    map.controls[google.maps.ControlPosition.TOP_LEFT].push(centerControlDiv);

                    google.maps.event.addListener(map, "zoom_changed", function () {
                    var z = map.getZoom();
                    var layerArray = map.overlayMapTypes.getArray();
                    console.log(layerArray);
                    });

                    html,
                    body,
                    #map {
                    height: 100%;
                    width: 100%;
                    padding: 0px;
                    margin: 0px;
                    }

                    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
                    <script src="https://maps.googleapis.com/maps/api/js?v=3&libraries=geometry"></script>
                    <div id="map"></div>








                    var map = new google.maps.Map(document.getElementById('map'), {
                    zoom: 2,
                    maxZoom: 8,
                    minZoom: 1,
                    center: {
                    lat: 0,
                    lng: -0
                    },
                    disableDefaultUI: false,
                    zoomControl: true,
                    mapTypeControl: false,
                    streetViewControl: false,
                    mapTypeId: google.maps.MapTypeId.ROADMAP
                    });

                    function CenterControl(controlDiv, map) {

                    // Bulut Custom Map Control
                    var controlUI = document.createElement('div');
                    controlUI.style.cursor = 'pointer';
                    controlUI.style.textAlign = 'center';
                    controlUI.title = 'Please Select a Weather Layer';
                    controlDiv.appendChild(controlUI);

                    var controlText = document.createElement('div');
                    controlText.innerHTML = '<table id="otherlayers"><tbody><tr><td><input type="checkbox" id="gebco" data-opacity="1"><label for="gebco">Wind Stream</label></td></tr></tbody></table>';
                    controlUI.appendChild(controlText);

                    $(document).on("change","input[id='gebco']",function(e){

                    setgebcolayer(this);

                    var layerArray = map.overlayMapTypes.getArray();
                    console.log(layerArray);

                    })

                    }

                    var gebcoLayer =
                    new google.maps.ImageMapType(
                    {
                    getTileUrl: function (coord, zoom) {
                    var tilesPerGlobe = 1 << zoom;
                    var x = coord.x % tilesPerGlobe;
                    if (x < 0) {
                    x = tilesPerGlobe + x;
                    }

                    var bbox2 = tileCoordsToBBox(map, coord, zoom, 256, 256);
                    var url = "https://www.gebco.net/data_and_products/gebco_web_services/web_map_service/mapserv?request=getmap&service=wms&BBOX=" + bbox2 + "&crs=EPSG:4326&format=image/png&layers=gebco_latest&width=256&height=256&version=1.3.0"
                    return url;
                    },
                    tileSize: new google.maps.Size(256, 256),
                    name: "Gebco",
                    isPng: true,
                    maxZoom: 18,
                    opacity: 0.3,
                    });

                    function setgebcolayer(control) {
                    var checked = control.checked;
                    if (checked == false) {
                    var layername = "Gebco";
                    for (_layer in map.overlayMapTypes.getArray()) {
                    if (map.overlayMapTypes.getAt(_layer).name == layername) {
                    if (checked == false) {
                    map.overlayMapTypes.removeAt(_layer);
                    return;
                    }
                    }
                    }
                    }
                    map.overlayMapTypes.insertAt(0, gebcoLayer);
                    }

                    function tileCoordsToBBox(map, coord, zoom, tileWidth, tileHeight) {
                    var proj = map.getProjection();
                    var scale = Math.pow(2, zoom);
                    var ne = proj.fromPointToLatLng(new google.maps.Point((coord.x + 1) * tileWidth / scale, coord.y * tileHeight / scale));
                    var sw = proj.fromPointToLatLng(new google.maps.Point(coord.x * tileWidth / scale, (coord.y + 1) * tileHeight / scale));

                    return [
                    sw.lat(),
                    sw.lng(),
                    ne.lat(),
                    ne.lng()
                    ];
                    };

                    var centerControlDiv = document.createElement('div');
                    var centerControl = new CenterControl(centerControlDiv, map);

                    centerControlDiv.index = 1;
                    map.controls[google.maps.ControlPosition.TOP_LEFT].push(centerControlDiv);

                    google.maps.event.addListener(map, "zoom_changed", function () {
                    var z = map.getZoom();
                    var layerArray = map.overlayMapTypes.getArray();
                    console.log(layerArray);
                    });

                    html,
                    body,
                    #map {
                    height: 100%;
                    width: 100%;
                    padding: 0px;
                    margin: 0px;
                    }

                    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
                    <script src="https://maps.googleapis.com/maps/api/js?v=3&libraries=geometry"></script>
                    <div id="map"></div>





                    var map = new google.maps.Map(document.getElementById('map'), {
                    zoom: 2,
                    maxZoom: 8,
                    minZoom: 1,
                    center: {
                    lat: 0,
                    lng: -0
                    },
                    disableDefaultUI: false,
                    zoomControl: true,
                    mapTypeControl: false,
                    streetViewControl: false,
                    mapTypeId: google.maps.MapTypeId.ROADMAP
                    });

                    function CenterControl(controlDiv, map) {

                    // Bulut Custom Map Control
                    var controlUI = document.createElement('div');
                    controlUI.style.cursor = 'pointer';
                    controlUI.style.textAlign = 'center';
                    controlUI.title = 'Please Select a Weather Layer';
                    controlDiv.appendChild(controlUI);

                    var controlText = document.createElement('div');
                    controlText.innerHTML = '<table id="otherlayers"><tbody><tr><td><input type="checkbox" id="gebco" data-opacity="1"><label for="gebco">Wind Stream</label></td></tr></tbody></table>';
                    controlUI.appendChild(controlText);

                    $(document).on("change","input[id='gebco']",function(e){

                    setgebcolayer(this);

                    var layerArray = map.overlayMapTypes.getArray();
                    console.log(layerArray);

                    })

                    }

                    var gebcoLayer =
                    new google.maps.ImageMapType(
                    {
                    getTileUrl: function (coord, zoom) {
                    var tilesPerGlobe = 1 << zoom;
                    var x = coord.x % tilesPerGlobe;
                    if (x < 0) {
                    x = tilesPerGlobe + x;
                    }

                    var bbox2 = tileCoordsToBBox(map, coord, zoom, 256, 256);
                    var url = "https://www.gebco.net/data_and_products/gebco_web_services/web_map_service/mapserv?request=getmap&service=wms&BBOX=" + bbox2 + "&crs=EPSG:4326&format=image/png&layers=gebco_latest&width=256&height=256&version=1.3.0"
                    return url;
                    },
                    tileSize: new google.maps.Size(256, 256),
                    name: "Gebco",
                    isPng: true,
                    maxZoom: 18,
                    opacity: 0.3,
                    });

                    function setgebcolayer(control) {
                    var checked = control.checked;
                    if (checked == false) {
                    var layername = "Gebco";
                    for (_layer in map.overlayMapTypes.getArray()) {
                    if (map.overlayMapTypes.getAt(_layer).name == layername) {
                    if (checked == false) {
                    map.overlayMapTypes.removeAt(_layer);
                    return;
                    }
                    }
                    }
                    }
                    map.overlayMapTypes.insertAt(0, gebcoLayer);
                    }

                    function tileCoordsToBBox(map, coord, zoom, tileWidth, tileHeight) {
                    var proj = map.getProjection();
                    var scale = Math.pow(2, zoom);
                    var ne = proj.fromPointToLatLng(new google.maps.Point((coord.x + 1) * tileWidth / scale, coord.y * tileHeight / scale));
                    var sw = proj.fromPointToLatLng(new google.maps.Point(coord.x * tileWidth / scale, (coord.y + 1) * tileHeight / scale));

                    return [
                    sw.lat(),
                    sw.lng(),
                    ne.lat(),
                    ne.lng()
                    ];
                    };

                    var centerControlDiv = document.createElement('div');
                    var centerControl = new CenterControl(centerControlDiv, map);

                    centerControlDiv.index = 1;
                    map.controls[google.maps.ControlPosition.TOP_LEFT].push(centerControlDiv);

                    google.maps.event.addListener(map, "zoom_changed", function () {
                    var z = map.getZoom();
                    var layerArray = map.overlayMapTypes.getArray();
                    console.log(layerArray);
                    });

                    html,
                    body,
                    #map {
                    height: 100%;
                    width: 100%;
                    padding: 0px;
                    margin: 0px;
                    }

                    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
                    <script src="https://maps.googleapis.com/maps/api/js?v=3&libraries=geometry"></script>
                    <div id="map"></div>






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Dec 2 '18 at 18:53









                    Vishnu VishwaVishnu Vishwa

                    12916




                    12916













                    • @Bulut Kartal Is the above answer solved your issue completely or do you need further help on it?

                      – Vishnu Vishwa
                      Dec 5 '18 at 17:56



















                    • @Bulut Kartal Is the above answer solved your issue completely or do you need further help on it?

                      – Vishnu Vishwa
                      Dec 5 '18 at 17:56

















                    @Bulut Kartal Is the above answer solved your issue completely or do you need further help on it?

                    – Vishnu Vishwa
                    Dec 5 '18 at 17:56





                    @Bulut Kartal Is the above answer solved your issue completely or do you need further help on it?

                    – Vishnu Vishwa
                    Dec 5 '18 at 17:56











                    1














                    JQuery is running the click event twice. The line below triggers twice on a click:



                    $("div").on("click", "#otherlayers input[id='gebco']", function (e) {   





                    share|improve this answer
























                    • thank you for your answer. I noticed that too. How can I prevent it?

                      – Bulut Kartal
                      Dec 2 '18 at 2:25
















                    1














                    JQuery is running the click event twice. The line below triggers twice on a click:



                    $("div").on("click", "#otherlayers input[id='gebco']", function (e) {   





                    share|improve this answer
























                    • thank you for your answer. I noticed that too. How can I prevent it?

                      – Bulut Kartal
                      Dec 2 '18 at 2:25














                    1












                    1








                    1







                    JQuery is running the click event twice. The line below triggers twice on a click:



                    $("div").on("click", "#otherlayers input[id='gebco']", function (e) {   





                    share|improve this answer













                    JQuery is running the click event twice. The line below triggers twice on a click:



                    $("div").on("click", "#otherlayers input[id='gebco']", function (e) {   






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Dec 1 '18 at 1:48









                    geocodezipgeocodezip

                    128k10148178




                    128k10148178













                    • thank you for your answer. I noticed that too. How can I prevent it?

                      – Bulut Kartal
                      Dec 2 '18 at 2:25



















                    • thank you for your answer. I noticed that too. How can I prevent it?

                      – Bulut Kartal
                      Dec 2 '18 at 2:25

















                    thank you for your answer. I noticed that too. How can I prevent it?

                    – Bulut Kartal
                    Dec 2 '18 at 2:25





                    thank you for your answer. I noticed that too. How can I prevent it?

                    – Bulut Kartal
                    Dec 2 '18 at 2:25


















                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53528305%2fgoogle-maps-custom-tile-layer-input-onclick-adds-same-layer-as-twice%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

                    Calculate evaluation metrics using cross_val_predict sklearn

                    Insert data from modal to MySQL (multiple modal on website)