简体   繁体   English

Google Maps API标记未显示

[英]Google maps api marker not showing

I can not get the marker to show up on the map. 我无法使标记显示在地图上。 Can anyone help me find out what is wrong that would be a huge help? 谁能帮我找出出什么问题对您有很大帮助?

Here is the code: 这是代码:

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

You appear to be missing the var keyword in front of the marker and Atlanta variables. 您似乎在markerAtlanta变量前面缺少var关键字。 To fix, I would also declare the map , marker and Atlanta variables outside of the initialize() function in the global space, so they are accessible to other functions. 为了解决这个问题,我还将在全局空间中的initialize()函数之外声明mapmarkerAtlanta变量,以便其他函数可以访问它们。 Try this: 尝试这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM