标记没有添加到我的地图

时间:2012-10-05 10:42:35

标签: javascript google-maps google-maps-api-3

尝试在我创建的联系页面上向Google地图添加标记。地图渲染正常但标记不会出现。继承我的代码:

function initialize() {
                    var latlng = new google.maps.LatLng(-43.51187,172.621192);
                    var myOptions = {
                      zoom: 15,
                      center: latlng,
                      mapTypeId: google.maps.MapTypeId.ROADMAP
                };
                var map = new google.maps.Map(document.getElementById("map_canvas"),
                    myOptions);
                    NSMarker();
                }

                // Function for adding a marker to the page.
                function addMarker(location) {
                    marker = new google.maps.Marker({
                        position: location,
                        map: map
                    });
                }

                // Add Marker Location
                function NSMarker() {
                    NSArchitects = new google.maps.LatLng(-43.51187,172.621192);
                    addMarker(NSArchitects);
                }

1 个答案:

答案 0 :(得分:4)

在initialize()函数之外声明var map

var map; // declare globel

function initialize() {

    var latlng = new google.maps.LatLng(-43.51187,172.621192);
    var myOptions = {
          zoom: 15,
          center: latlng,
          mapTypeId: google.maps.MapTypeId.ROADMAP
     };
    map = new google.maps.Map(document.getElementById("map_canvas"),
    myOptions);
    NSMarker();

}

function addMarker(location) {
                marker = new google.maps.Marker({
                    position: location,
                    map: map
                });
            }

            // Add Marker Location
            function NSMarker() {
                NSArchitects = new google.maps.LatLng(-43.51187,172.621192);
                addMarker(NSArchitects);
            }