谷歌地图api标记未显示 [英] Google maps api marker not showing

查看:35
本文介绍了谷歌地图api标记未显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

代码如下:

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

推荐答案

您似乎缺少 markerAtlanta<前面的 var 关键字/code> 变量.为了修复,我还将在 initialize() 函数之外声明 mapmarkerAtlanta 变量全局空间,因此其他功能可以访问它们.试试这个:

You appear to be missing the var keyword in front of the marker and Atlanta variables. 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. 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);

这篇关于谷歌地图api标记未显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆