[英]Google map markers not showing up when placed through listener event
SPOILER ALERT: I had a typo in my code. SPOILER ALERT:我的代码中有错字。
The code below successfully displays the initial map with the initial marker at the latitude/longitude set. 下面的代码成功显示了初始地图,并在纬度/经度处设置了初始标记。
I tried to add a listener to place a new marker when I click on the map, but nothing appears to happen. 当我单击地图时,我试图添加一个侦听器以放置一个新标记,但是似乎什么也没有发生。 I tried having an InfoWindow display on the marker, but since the marker wasn't displaying the InfoWindow showed in the upper-left corner.
我尝试在标记上显示InfoWindow,但是由于标记未显示左上角显示的InfoWindow。
I'm using http://maps.googleapis.com/maps/api/js
: 我正在使用
http://maps.googleapis.com/maps/api/js
:
function initialize() {
var mapProp = {
center: new google.maps.LatLng(45.8590, -122.8158),
zoom: 9,
mapTypeId: google.maps.MapTypeId.ROADMAP,
};
map = new google.maps.Map(document.getElementById('googleMap'), mapProp);
var marker = new google.maps.Marker({
position: new google.maps.LatLng(45.8590, -122.8158),
animation: google.maps.Animation.DROP,
map: map,
});
google.maps.event.addListener(map, 'click', function(event) {
placeMarker(event.latLng);
});
function placeMarker(location) {
var subMarker = new google.maps.Marker({
postion: location,
map: map,
})
}
}
google.maps.event.addDomListener(window, 'load', initialize);
What am I doing wrong? 我究竟做错了什么?
You have postion: location
in new Marker(...)
, it should be position
. 您有
postion: location
new Marker(...)
postion: location
,应该是position
。
Original Answer: 原始答案:
You call function placeMarker(event.latlng);
您调用函数
placeMarker(event.latlng);
but there are should be two arguments: map and location as in definition. 但是应该有两个参数:定义中的map和location。
function placeMarker(map, location) { ... }
, function placeMarker(map, location) { ... }
,
event.latlng
should be event.latLng
. event.latlng
应该是event.latLng
。
One of possible solutions could be like this: google.maps.event.addListener(map, 'click', function(event) { placeMarker(map, event.latLng); });
可能的解决方案之一可能是这样的:
google.maps.event.addListener(map, 'click', function(event) { placeMarker(map, event.latLng); });
You can see example in documentation . 您可以在文档中看到示例。
You have a typo: postion
should be position
in the MarkerOptions 你有一个错字:
postion
应该是position
在的MarkerOptions
code snippet: 代码段:
function initialize() { var mapProp = { center: new google.maps.LatLng(45.8590, -122.8158), zoom: 9, mapTypeId: google.maps.MapTypeId.ROADMAP, }; var map = new google.maps.Map(document.getElementById('googleMap'), mapProp); var marker = new google.maps.Marker({ position: new google.maps.LatLng(45.8590, -122.8158), animation: google.maps.Animation.DROP, map: map, }); google.maps.event.addListener(map, 'click', function(event) { placeMarker(event.latLng); }); function placeMarker(location) { var subMarker = new google.maps.Marker({ position: location, map: map, }); } } google.maps.event.addDomListener(window, 'load', initialize);
html, body, #googleMap { height: 100%; width: 100%; margin: 0px; padding: 0px }
<script src="https://maps.googleapis.com/maps/api/js"></script> <div id="googleMap"></div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.