2013-07-07 3 views
1

마커를지도에 표시 할 수 없습니다. 누가 큰 도움이 될 것인가가 잘못된지 알아내는 데 도움을 줄 수 있습니까?Google지도 api marker not showing

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

답변

2

당신은 markerAtlanta 변수 앞에 var 키워드를 누락 된 것으로 나타납니다 여기

는 코드입니다. 수정하려면 전역 공간에서 , 및 Atlanta 변수를 initialize() 함수 외부에 선언하여 다른 함수에서 액세스 할 수 있도록하십시오. 사용해보기 :

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

불행히도 마커는 여전히 표시되지 않습니다. – zachstarnes

+0

적절한 Google지도 js 파일을 포함 시켰습니까? 콘솔에는 무엇이 표시됩니까? –

+0

또한 내 대답을 업데이트했습니다 -'initialize()'밖에서'var map '을 선언하십시오. –