Google地图:无法添加标记 [英] Google Maps: Can't add marker

查看:140
本文介绍了Google地图:无法添加标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无法添加标记,可能非常简单,但我对此很陌生。感谢。

 < script type =text / javascriptsrc =https://maps.googleapis.com/maps / API / JS&安培;传感器=真>< /脚本> 
< script type =text / javascript>

function addMarker(){
var marker = new google.maps.Marker({
position:new google.maps.LatLng(-34.397,150.644),

标题:Hello World!
});
}


函数初始化(){
var mapOptions = {
center:new google.maps.LatLng(-34.397,150.644),
zoom:8,
mapTypeId:google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById(map-canvas),
mapOptions);
}
google.maps.event.addDomListener(window,'load',initialize);
$ b $(document).ready(function(){
addMarker();
})

< / script>


解决方案

有两个问题,你需要设置map变量标记。您的地图变量对于初始化函数是本地的。




  • 在映射初始化后,从初始化函数(映射存在的地方)调用addMarker函数。

      function addMarker(map){
    var marker = new google.maps.Marker({
    position:new google。 maps.LatLng(-34.397,150.644),
    map:map,

    title:Hello World!
    });


    函数初始化(){
    var mapOptions = {
    center:new google.maps.LatLng(-34.397,150.644),
    zoom :8,
    mapTypeId:google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map(document.getElementById(map-canvas),
    mapOptions);
    addMarker(map);

    $ / code $ / pre $

    工作示例


    Can't get a marker added, probably something very simple but I'm new to this. Thanks.

    <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?&sensor=true"></script>
        <script type="text/javascript">
    
            function addMarker() {
                var marker = new google.maps.Marker({
                    position: new google.maps.LatLng(-34.397, 150.644),
    
                    title:"Hello World!"
                });
            }
    
    
            function initialize() {
                var mapOptions = {
                    center: new google.maps.LatLng(-34.397, 150.644),
                    zoom: 8,
                    mapTypeId: google.maps.MapTypeId.ROADMAP
                };
                var map = new google.maps.Map(document.getElementById("map-canvas"),
                mapOptions);
            }
            google.maps.event.addDomListener(window, 'load', initialize);
    
            $(document).ready(function() {
                addMarker();
            })
    
        </script>
    

    解决方案

    Two issues, you need to set the map variable of the marker. Your map variable is local to the initialize function.

    • call the addMarker function from your initialize function (where the map exists), after the map is initialized.

          function addMarker(map) {
            var marker = new google.maps.Marker({
              position: new google.maps.LatLng(-34.397, 150.644),
              map:map,
      
              title:"Hello World!"
            });
          }
      
          function initialize() {
            var mapOptions = {
              center: new google.maps.LatLng(-34.397, 150.644),
              zoom: 8,
              mapTypeId: google.maps.MapTypeId.ROADMAP
            };
            var map = new google.maps.Map(document.getElementById("map-canvas"),
            mapOptions);
            addMarker(map);
          }
      

    working example

    这篇关于Google地图:无法添加标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆