-1

试图在我创建的联系页面上的谷歌地图上添加一个标记。地图渲染良好,但没有出现标记。这是我的代码:

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

1 回答 1

4

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);
            }
于 2012-10-05T10:50:23.333 回答