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

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

问题描述

无法添加标记,可能很简单,但是我对此并不陌生.谢谢.

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>

推荐答案

两个问题,您需要设置标记的map变量.您的map变量位于initialize函数的本地.

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

  • 在初始化地图后,从您的Initialize函数(存在地图的地方)调用addMarker函数.

  • 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);
    }

工作示例

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

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