简体   繁体   English

如何在谷歌地图上只显示 1 个标记?

[英]How to show only 1 marker on Google Maps?

Hi I have this weird problem with Google Maps V3 API.嗨,我在使用 Google Maps V3 API 时遇到了这个奇怪的问题。 Showing multiple markers on a google map is fine, but when I only want to show a single marker on the map, I get no markers displaying?在谷歌 map 上显示多个标记很好,但是当我只想在 map 上显示一个标记时,我没有显示任何标记?

The code below only shows a map with no markers.下面的代码仅显示没有标记的 map。

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

marker1 = new google.maps.Marker({  
    position: new google.maps.LatLng(1.288693,103.846733),
    map: map
}); 

Adding a second marker shows 2 markers on the map.添加第二个标记会在 map 上显示 2 个标记。

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

marker1 = new google.maps.Marker({  
    position: new google.maps.LatLng(1.288693,103.846733),
    map: map
}); 

marker2 = new google.maps.Marker({  
    position: new google.maps.LatLng(1.288693,103.846733),
    map: map
}); 

So my question is, how do I only display a single marker on the map?所以我的问题是,我如何只在 map 上显示一个标记? Currently my workaround is to have 2 markers of the same lat/lng, but its not very elegant lol目前我的解决方法是使用 2 个相同 lat/lng 的标记,但它不是很优雅,哈哈

Your first code block totally works for me.你的第一个代码块完全适合我。 Here's the entire file I have where it's working, if this helps:这是我工作的整个文件,如果这有帮助的话:

<html>
<head>
<title>Map Test</title>
<script type="application/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script> 

<script>
    var latlng = new google.maps.LatLng(1.288693,103.846733);

    var options = {  
        zoom: 16,  
        center: latlng,  
        mapTypeId: google.maps.MapTypeId.ROADMAP,
    };
</script>
</head>
<body>
<h1>Map</h1>
<div id="map_canvas" style="height:100%; width:100%;"></div>
<script>
var map = new google.maps.Map(document.getElementById('map_canvas'), options);

marker1 = new google.maps.Marker({  
    position: new google.maps.LatLng(1.288693,103.846733),
    map: map
}); 
</script>
</body>
</html>

to display one marker on v3 google maps i did mine like this在 v3 谷歌地图上显示一个标记,我是这样做的

function initialize() {
            var latlng = new google.maps.LatLng(-34.397, 150.644);
            var myOptions = {
                zoom: 8,
                center: latlng,
                mapTypeId: google.maps.MapTypeId.ROADMAP // map view, can be set to satellite, street, roadview, aerialview
            };
            map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

            var marker = new google.maps.Marker({
                position: latlng,
                map: map,
                animation: google.maps.Animation.DROP,
                title: "Uluru (Ayers Rock)"
            });

            marker.setMap(map);  
        }

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

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