2016-09-12 2 views
1

Google지도 js api를 WordPress 사이트에로드하려고하는데 "initMap is not a function"오류가 계속 발생합니다. 아마 내가 쓰는 코드가 잘못되었다는 것을 알 수 있습니다. 독립형 페이지에서이 코드를 적용하여 아마도 wordpress를 사용하여 작업을 엉망으로 만들었을 것입니다. 올바른 순서로로드되지 않는 JS 파일에 문제가 있었지만 지금은 JS가 Google지도보다 먼저로드되므로 문제가되지 않습니다. initMap 함수에 이상이 있어야합니다. 이 오류를 일으키는 기능입니다. 전 세계적인 범위로 넘어 갔다고 생각했지만 제대로했는지 확신 할 수 없습니다.전체 범위의 자바 스크립트 기능

API 광고 키 호출 JS에서

wp_enqueue_script('google-maps-js', 'https://maps.googleapis.com/maps/api/js?v=3&key=###key###&callback=initMap', array(), '20151215', true); 

기능

jQuery(function($) { 
    window.initMap = function() { 

     // Disallow drag on mobile 
     var isDraggable = $(document).width() > 480 ? true : false; 

     map = new google.maps.Map(document.getElementById('map'), { 
     scrollwheel: false, 
     draggable: isDraggable, 
     streetViewControl: false, 
     styles: [{"featureType":"administrative","elementType":"labels.text","stylers":[{"visibility":"off"}]},{"featureType":"administrative","elementType":"labels.text.fill","stylers":[{"color":"#444444"}]},{"featureType":"landscape","elementType":"all","stylers":[{"color":"#f2f2f2"}]},{"featureType":"poi","elementType":"all","stylers":[{"visibility":"off"}]},{"featureType":"road","elementType":"all","stylers":[{"saturation":-100},{"lightness":45}]},{"featureType":"road","elementType":"labels","stylers":[{"visibility":"off"}]},{"featureType":"road.highway","elementType":"all","stylers":[{"visibility":"simplified"}]},{"featureType":"road.highway","elementType":"geometry.fill","stylers":[{"visibility":"on"},{"color":"#1764c0"}]},{"featureType":"road.highway","elementType":"geometry.stroke","stylers":[{"visibility":"on"},{"color":"#1764c0"}]},{"featureType":"road.highway","elementType":"labels","stylers":[{"visibility":"off"}]},{"featureType":"road.arterial","elementType":"geometry.fill","stylers":[{"visibility":"on"},{"color":"#2fa000"}]},{"featureType":"road.arterial","elementType":"geometry.stroke","stylers":[{"color":"#2fa000"},{"visibility":"simplified"}]},{"featureType":"road.arterial","elementType":"labels.icon","stylers":[{"visibility":"off"}]},{"featureType":"road.local","elementType":"geometry.fill","stylers":[{"color":"#2fa000"}]},{"featureType":"road.local","elementType":"geometry.stroke","stylers":[{"visibility":"on"},{"color":"#2fa000"}]},{"featureType":"transit","elementType":"all","stylers":[{"visibility":"off"}]},{"featureType":"water","elementType":"all","stylers":[{"color":"#46bcec"},{"visibility":"on"}]}] 
     }); 

     // Center map in between California LatLng(36.778261, -119.417932) and New York LatLng(40.712784, -74.005941) 
     map.fitBounds(new google.maps.LatLngBounds(new google.maps.LatLng(36.778261, -119.417932), new google.maps.LatLng(40.712784, -74.005941))) 
    }; 
}); 
+0

에는'initMaps' 기능이 없습니다 이외의 기능을 initMap 이동하려고 파일 - 당신을 'initMap' - 단수형을가집니다. 또한 아무 곳에서도 호출되지 않으며'initMaps' (복수형)도 호출되지 않습니다. 최소한 코드에 나와 있습니다. – vlaz

+0

죄송합니다, 나는 initMap을 의미합니다. InitMaps는 어디서나 함수가 아닙니다! initMap에 대한 오류입니다. –

+0

키를 표시하지 마십시오 – jollarvia

답변

1

가 JQuery와 초기화

<script> 
    function initMap() { 

     // Disallow drag on mobile 
     var isDraggable = $(document).width() > 480 ? true : false; 

     map = new google.maps.Map(document.getElementById('map'), { 
     scrollwheel: false, 
     draggable: isDraggable, 
     streetViewControl: false, 
     styles: [{"featureType":"administrative","elementType":"labels.text","stylers":[{"visibility":"off"}]},{"featureType":"administrative","elementType":"labels.text.fill","stylers":[{"color":"#444444"}]},{"featureType":"landscape","elementType":"all","stylers":[{"color":"#f2f2f2"}]},{"featureType":"poi","elementType":"all","stylers":[{"visibility":"off"}]},{"featureType":"road","elementType":"all","stylers":[{"saturation":-100},{"lightness":45}]},{"featureType":"road","elementType":"labels","stylers":[{"visibility":"off"}]},{"featureType":"road.highway","elementType":"all","stylers":[{"visibility":"simplified"}]},{"featureType":"road.highway","elementType":"geometry.fill","stylers":[{"visibility":"on"},{"color":"#1764c0"}]},{"featureType":"road.highway","elementType":"geometry.stroke","stylers":[{"visibility":"on"},{"color":"#1764c0"}]},{"featureType":"road.highway","elementType":"labels","stylers":[{"visibility":"off"}]},{"featureType":"road.arterial","elementType":"geometry.fill","stylers":[{"visibility":"on"},{"color":"#2fa000"}]},{"featureType":"road.arterial","elementType":"geometry.stroke","stylers":[{"color":"#2fa000"},{"visibility":"simplified"}]},{"featureType":"road.arterial","elementType":"labels.icon","stylers":[{"visibility":"off"}]},{"featureType":"road.local","elementType":"geometry.fill","stylers":[{"color":"#2fa000"}]},{"featureType":"road.local","elementType":"geometry.stroke","stylers":[{"visibility":"on"},{"color":"#2fa000"}]},{"featureType":"transit","elementType":"all","stylers":[{"visibility":"off"}]},{"featureType":"water","elementType":"all","stylers":[{"color":"#46bcec"},{"visibility":"on"}]}] 
     }); 

     // Center map in between California LatLng(36.778261, -119.417932) and New York LatLng(40.712784, -74.005941) 
     map.fitBounds(new google.maps.LatLngBounds(new google.maps.LatLng(36.778261, -119.417932), new google.maps.LatLng(40.712784, -74.005941))) 
    }; 

</script> 
+0

그랬습니다! Dang it, 나는 오류를 수정하려고 노력했고 그것은 분명히 그것을 더 악화 시켰습니다. 감사! –

+0

아마 타이밍 문제. jquery로드가 완료되기 전에 initmap을 호출 해 보았습니다. – jollarvia

+0

전체 문서가 생성되었을 때 만들어야하는 기능을 Google지도에서 찾지 못하면 Google지도가 콜백 기능을 찾지 못합니다.하지만 스크립트 범위에서 선언하면 함수가 생성됩니다 전체 문서를로드하십시오. – scaisEdge