1

我无法让标记显示在地图上。谁能帮我找出问题所在,这将是一个巨大的帮助?

这是代码:

function initialize() {
    var map_canvas = document.getElementById('map_canvas');
    var myLatlng = new google.maps.LatLng(33.748995, -84.387982);
    var mapOptions = {
        center: myLatlng,
        zoom: 11,
        mapTypeId: google.maps.MapTypeId.TERRAIN
    }
    var map = new google.maps.Map(map_canvas, mapOptions);
    TestMarker();    
}

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

// Testing the addMarker function
function TestMarker() {
       Atlanta = new google.maps.LatLng(33.748995, -84.387982);
       addMarker(Atlanta);
}

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

1 回答 1

2

您似乎缺少and变量var前面的关键字。为了解决这个问题,我还将在全局空间中的函数之外声明,和变量,以便其他函数可以访问它们。尝试这个:markerAtlantamapmarkerAtlantainitialize()

var map;
var marker;
var Atlanta;

function initialize() {
    var map_canvas = document.getElementById('map_canvas');
    var myLatlng = new google.maps.LatLng(33.748995, -84.387982);
    var mapOptions = {
        center: myLatlng,
        zoom: 11,
        mapTypeId: google.maps.MapTypeId.TERRAIN
    };
    map = new google.maps.Map(map_canvas, mapOptions);
    TestMarker();    
}

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

// Testing the addMarker function
function TestMarker() {
       Atlanta = new google.maps.LatLng(33.748995, -84.387982);
       addMarker(Atlanta);
}

google.maps.event.addDomListener(window, 'load', initialize);
于 2013-07-07T21:05:43.993 回答