简体   繁体   English

Google Maps V3标记未显示

[英]Google Maps V3 Marker not showing

I'm writing the below script to add a marker on click, the map loads fine but when I click on it no marker is added and there are no errors in the console, anyone have any suggestions as to what might be going on? 我正在编写以下脚本,以在单击时添加标记,地图加载正常,但是当我单击它时,未添加标记,并且控制台中没有错误,任何人都对可能发生的事情有任何建议?

function selectMapLocation()
{
    var mapOptions = {
        zoom: 8,
        center: new google.maps.LatLng(37.7699298, -122.4469157)
    };

    var map = new google.maps.Map(document.getElementById('map'), mapOptions);

    google.maps.event.addListener(map, 'click', function(event){
        var marker = new google.maps.Marker({
            position: event.LatLng, 
            map: map
        });
    });
}

function loadSelectMapLocation()
{
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = 'https://maps.googleapis.com/maps/api/js?v=3.exp&callback=selectMapLocation';
    document.body.appendChild(script);
}

A google.maps.MarkerOptions object doesn't have a setMap property. google.maps.MarkerOptions对象没有setMap属性。

map | 地图| Map|StreetViewPanorama | 地图|街景全景| Map on which to display Marker. 要在其上显示标记的地图。

The MouseClick event doesn't have a LatLng property. MouseClick事件没有LatLng属性。 It is latLng (javascript is case sensitive). 它是latLng(JavaScript区分大小写)。

latLng | latLng | LatLng | LatLng | The latitude/longitude that was below the cursor when the event occurred. 事件发生时光标下方的纬度/经度。

 function selectMapLocation() { var mapOptions = { zoom: 8, center: new google.maps.LatLng(37.7699298, -122.4469157) }; var map = new google.maps.Map(document.getElementById('map'), mapOptions); google.maps.event.addListener(map, 'click', function(event){ var marker = new google.maps.Marker({ position: event.latLng, map: map }); }); } function loadSelectMapLocation() { var script = document.createElement('script'); script.type = 'text/javascript'; script.src = 'https://maps.googleapis.com/maps/api/js?v=3.exp&callback=selectMapLocation'; document.body.appendChild(script); } loadSelectMapLocation(); 
 <div id="map" style="width:750px; height:450px; border: 2px solid #3872ac;"></div> 

You should use the map property on object construction instead of setMap . 您应该在对象构造上使用map属性,而不要使用setMap

EDIT : also LatLng is latLng , as mentioned in another response. 编辑LatLng也是latLng ,如另一个响应中所述。

var marker = new google.maps.Marker({
    position: event.latLng, 
    map: map
});

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

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