addListener单击不显示添加标记
addListener click is not showing add marker
我已经创建了下面的代码,但我试图让用户添加标记,我试图使用带有点击事件的addListener。当我试图点击地图时,什么也没发生,我做错了什么?
<head>
<style>
html, body {
height:100%;
width: 100%;
margin-left: 0px;
margin-right: 0px;
margin-bottom: 0px;
margin-top: 0px;
}
#mapContainer {
height: 100%;
width: 100%;
display: block;
margin-left: 0px;
margin-right: 0px;
margin-bottom: 0px;
margin-top: 0px;
}
#map {
height: 100%;
}
.gm-style-mtc {
display: none;
}
.gmnoprint {
display: none;
}
</style>
<script src="https://maps.googleapis.com/maps/api/js"></script>
</head>
<body>
<div id="mapContainer">
<div id="map"></div>
</div>
<script>
var mapCanvas;
function initialize() {
var myOptions = {
center: {lat: 40.740, lng: -74.18},
zoom : 15,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
mapCanvas = new google.maps.Map(document.getElementById("mapContainer"), myOptions);
var marker = new google.maps.Marker({
position: mapCanvas.getCenter(),
map: mapCanvas
});
var imageBounds = {
north: 40.773941,
south: 40.712216,
east: -74.12544,
west: -74.22655
};
historicalOverlay = new google.maps.GroundOverlay(
'https://i.stack.imgur.com/0mgx2.jpg',
imageBounds);
historicalOverlay.setMap(mapCanvas);
// This event listener calls addMarker() when the map is clicked.
google.maps.event.addListener(mapCanvas, 'click', function(e) {
placeMarker(e.latLng, mapCanvas);
});
//Changes zoom levels when the projection is available.
google.maps.event.addListenerOnce(mapCanvas, "projection_changed", function(){
mapCanvas.setMapTypeId(google.maps.MapTypeId.HYBRID); //Changes the MapTypeId in short time.
setZoomLimit(mapCanvas, google.maps.MapTypeId.ROADMAP);
setZoomLimit(mapCanvas, google.maps.MapTypeId.HYBRID);
setZoomLimit(mapCanvas, google.maps.MapTypeId.SATELLITE);
setZoomLimit(mapCanvas, google.maps.MapTypeId.TERRAIN);
mapCanvas.setMapTypeId(google.maps.MapTypeId.ROADMAP); //Sets the MapTypeId to original.
});
}
function placeMarker(location) {
var marker = new google.maps.Marker({
position: location,
map: mapCanvas
});
map.setCenter(location);
}
function setZoomLimit(map, mapTypeId){
//Gets MapTypeRegistry
var mapTypeRegistry = mapCanvas.mapTypes;
//Gets the specified MapType
var mapType = mapTypeRegistry.get(mapTypeId);
//Sets limits to MapType
mapType.maxZoom = 15; //It doesn't work with SATELLITE and HYBRID maptypes.
mapType.minZoom = 15;
}
google.maps.event.addDomListener(window, "load", initialize);
</script>
<body>
GroundOverlay正在捕捉点击事件。
你有两个选择:
- 将
clickable
选项设置为false
historicalOverlay = new google.maps.GroundOverlay(
'https://i.stack.imgur.com/0mgx2.jpg',imageBounds,{clickable: false});
historicalOverlay.setMap(mapCanvas);
小提琴
- 将侦听器添加到覆盖
google.maps.event.addListener(historicalOverlay, 'click', function(e) {
placeMarker(e.latLng, mapCanvas);
});
小提琴
覆盖正在阻止默认贴图。将侦听器添加到覆盖中。
google.maps.event.addListener(historicalOverlay, 'click', function(e) {
placeMarker(e.latLng, mapCanvas);
});
尝试在下面显示演示:
var mapCanvas;
function initialize() {
var myOptions = {
center: {
lat: 40.740,
lng: -74.18
},
zoom: 15,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
mapCanvas = new google.maps.Map(document.getElementById("mapContainer"), myOptions);
var marker = new google.maps.Marker({
position: mapCanvas.getCenter(),
map: mapCanvas
});
var imageBounds = {
north: 40.773941,
south: 40.712216,
east: -74.12544,
west: -74.22655
};
historicalOverlay = new google.maps.GroundOverlay(
'https://i.stack.imgur.com/0mgx2.jpg',
imageBounds);
historicalOverlay.setMap(mapCanvas);
// This event listener calls addMarker() when the map is clicked.
google.maps.event.addListener(historicalOverlay, 'click', function(e) {
console.log("clicked'");
placeMarker(e.latLng, mapCanvas);
});
//Changes zoom levels when the projection is available.
google.maps.event.addListenerOnce(mapCanvas, "projection_changed", function() {
mapCanvas.setMapTypeId(google.maps.MapTypeId.HYBRID); //Changes the MapTypeId in short time.
setZoomLimit(mapCanvas, google.maps.MapTypeId.ROADMAP);
setZoomLimit(mapCanvas, google.maps.MapTypeId.HYBRID);
setZoomLimit(mapCanvas, google.maps.MapTypeId.SATELLITE);
setZoomLimit(mapCanvas, google.maps.MapTypeId.TERRAIN);
mapCanvas.setMapTypeId(google.maps.MapTypeId.ROADMAP); //Sets the MapTypeId to original.
});
}
function placeMarker(location) {
console.log("place a marker");
var marker = new google.maps.Marker({
position: location,
map: mapCanvas
});
map.setCenter(location);
}
function setZoomLimit(map, mapTypeId) {
//Gets MapTypeRegistry
var mapTypeRegistry = mapCanvas.mapTypes;
//Gets the specified MapType
var mapType = mapTypeRegistry.get(mapTypeId);
//Sets limits to MapType
mapType.maxZoom = 15; //It doesn't work with SATELLITE and HYBRID maptypes.
mapType.minZoom = 15;
}
google.maps.event.addDomListener(window, "load", initialize);
html,
body {
height: 100%;
width: 100%;
margin-left: 0px;
margin-right: 0px;
margin-bottom: 0px;
margin-top: 0px;
}
#mapContainer {
height: 100%;
width: 100%;
display: block;
margin-left: 0px;
margin-right: 0px;
margin-bottom: 0px;
margin-top: 0px;
}
#map {
height: 100%;
}
.gm-style-mtc {
display: none;
}
.gmnoprint {
display: none;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="mapContainer">
<div id="map"></div>
</div>
</body>
</html>
并且您的映射变量名称是mapCanvas而不是map
function placeMarker(location) {
var marker = new google.maps.Marker({
position: location,
map: mapCanvas
});
**mapCanvas**.setCenter(location);
}
您可以获得免费的api密钥。像这样:
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?v=3&key=**AIza14746817467416y01Dipl7TZVXfDmUCyY**&sensor=true&language=tr"></script>
相关文章:
- 如何使用javascript选择字符串的部分,添加html标记或删除部分
- 动态添加的标记不会'无法正确使用日期选择器
- 添加@javascript标记时,Cucumber中的HTTP身份验证失败
- 在 Javascript 生成的列表中,如何将自定义属性添加到 标记
- 如何在添加新标记和图层之前清除所有标记和图层的传单地图
- 如何在Phonegap中进行AJAX调用时添加加载屏幕
- 如何将多段线添加到标记阵列中
- 在添加新标记之前删除以前的标记
- 如何隐藏或删除Ngmap中动态添加的标记
- 在AngularJS模型中添加/删除标记
- 根据谷歌 Api v3 书/也用一个按钮添加一个标记
- 如何在加载前添加加载动画
- 通过添加自动标记库来了解 meteorjs 范围
- 在谷歌地图中添加新标记之前,先删除以前的标记
- 如何在网页加载之前添加加载页面
- 将数据属性添加到标记并使用 JQuery 检索它
- 将声音添加到标记数组 - 谷歌地图JavaScript
- 将侦听器添加到标记在循环中不起作用
- 在加载后添加预标记;t生效
- Google Maps API不会加载正确的中心位置,而是在最后添加的标记区域加载