将标记添加到Google地图

时间:2014-02-07 17:55:41

标签: javascript api google-maps

我正在使用Javascript API创建地图,我在显示标记时遇到了一些问题。

我已按照本教程创建地图,效果很好:

https://developers.google.com/maps/tutorials/fundamentals/adding-a-google-map

然后我按照本教程添加标记,但它没有加载:

https://developers.google.com/maps/documentation/javascript/examples/marker-simple

现在是我的代码:

            <script>
        function initialize() {
            var map_canvas = document.getElementById('map_canvas');
            var map_options = {
            center: new google.maps.LatLng(43.643296, -79.408475),
            zoom: 15,
            mapTypeId: google.maps.MapTypeId.ROADMAP }
            var map = new google.maps.Map(map_canvas, map_options, marker);
            var marker = new google.maps.Marker({
            position: myLatlng,
            map: map,
            title:"Hello World!" });
            }

            google.maps.event.addDomListener(window, 'load', initialize);

            </script>

2 个答案:

答案 0 :(得分:1)

答案 1 :(得分:1)

这一行

var map = new google.maps.Map(map_canvas, map_options, marker);

错了。 map构造函数只有两个参数。它应该是

   var map = new google.maps.Map(map_canvas, map_options);

myLatlng未定义。因此,您可以将代码更改为:

function initialize() {
    myLatlng = new google.maps.LatLng(43.643296, -79.408475);

    var map_canvas = document.getElementById('map');
    var map_options = {
        center: myLatlng,
        zoom: 15,
    mapTypeId: google.maps.MapTypeId.ROADMAP }
    var map = new google.maps.Map(map_canvas, map_options);

    var marker = new google.maps.Marker({
        position: myLatlng,
        map: map,
        title:"Hello World!" });
}