Google Maps Custom Tile Layer input onclick adds same layer as twice
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>
javascript jquery google-maps google-maps-api-3
add a comment |
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>
javascript jquery google-maps google-maps-api-3
add a comment |
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>
javascript jquery google-maps google-maps-api-3
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
javascript jquery google-maps google-maps-api-3
asked Nov 28 '18 at 21:21
Bulut KartalBulut Kartal
34116
34116
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
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
add a comment |
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>
@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
add a comment |
JQuery is running the click event twice. The line below triggers twice on a click:
$("div").on("click", "#otherlayers input[id='gebco']", function (e) {
thank you for your answer. I noticed that too. How can I prevent it?
– Bulut Kartal
Dec 2 '18 at 2:25
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
add a comment |
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
add a comment |
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
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
answered Dec 7 '18 at 21:58
ObservablerxjsObservablerxjs
407112
407112
add a comment |
add a comment |
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>
@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
add a comment |
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>
@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
add a comment |
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>
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>
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
add a comment |
@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
add a comment |
JQuery is running the click event twice. The line below triggers twice on a click:
$("div").on("click", "#otherlayers input[id='gebco']", function (e) {
thank you for your answer. I noticed that too. How can I prevent it?
– Bulut Kartal
Dec 2 '18 at 2:25
add a comment |
JQuery is running the click event twice. The line below triggers twice on a click:
$("div").on("click", "#otherlayers input[id='gebco']", function (e) {
thank you for your answer. I noticed that too. How can I prevent it?
– Bulut Kartal
Dec 2 '18 at 2:25
add a comment |
JQuery is running the click event twice. The line below triggers twice on a click:
$("div").on("click", "#otherlayers input[id='gebco']", function (e) {
JQuery is running the click event twice. The line below triggers twice on a click:
$("div").on("click", "#otherlayers input[id='gebco']", function (e) {
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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