써먹는 웹개발

[Js] google is not defined 해결방법 본문

웹개발/Js & Jquery

[Js] google is not defined 해결방법

kmhan 2023. 11. 15. 16:12


728x90
반응형

구글 지도 구현 중에 (그 유명한) 로컬에서는 되는데 운영서버에서 'google is not defined' 나서 해결한 방법입니다.

 

문제의 소스

1
2
3
4
5
    var mapOptions = {
                        zoom: 2// 지도를 띄웠을 때의 줌 크기
                        mapTypeId: google.maps.MapTypeId.ROADMAP,
                        center: new google.maps.LatLng(15.042621143.753442)
                    };
cs

 

로컬에서는 조회속도가 빨라서 google 변수가 금방 불러왔지만 운영서버에서는 속도가 느려서 google 변수를 불러오지 못해서 로직에러가 난 것

 

해결방법 : init() 함수 맨 앞에 google이 undefined 일때 계속 init 함수 재호출하도록 소스 추가

1
2
3
4
5
6
7
function init() {
      if (typeof google === "undefined") {
        setTimeout(init(), 1000);
        return;
      }
      // ...
}
cs

 

 

참고 : https://stackoverflow.com/questions/14229695/google-maps-api-throws-uncaught-referenceerror-google-is-not-defined-only-whe

 

728x90
반응형


Comments