2013-06-16 2 views
3

모바일 앱에 Google지도와 장소를 사용할 예정이므로 앱로드 속도를 늦추고 싶지는 않습니다.Google 장소를 비동기 적으로로드하십시오.

가장자리 네트워크에서 내 앱이 느리게 시작되는 것을 확인했습니다. 모든 내 js는 로컬이므로 Google지도 & 장소는로드 시간이 오래 걸립니다 (html 파일에서 & 장소를 제거하면로드가 빠름).

index.html 
<script>....</script> 
<script src='http://maps.google.com/maps/api/js?key=mykey&amp;sensor=false'></script> 
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?libraries=places&amp;sensor=false"></script> 
</body> 

나는 비동기 Google지도를 다운로드하는 방법을 Load google maps v3 dynamically with ajax에 발견했습니다 : 내가 사용하려고 할 때

$.getScript("http://maps.google.com/maps/api/js?key=mykey&sensor=false&async=2&callback=MapApiLoaded", function() {}); 

function MapApiLoaded() { 
    $.ajaxSetup({async:false}); 
    $.getScript("http://maps.googleapis.com/maps/api/js?libraries=places&sensor=false"); 
    $.ajaxSetup({async:true}); 
    alert('loading places done'); 
} 

내가 경고를 받고 내가 $ .getScript()가 성공적으로 반환 확인했지만 장소 자바 스크립트 콘솔에 다음과 같은 오류가 발생합니다 : 구글 맵스 장소는 정의되지 않았습니다.

Google지도를 비동기 적으로로드하고 HTML 파일에 위치를 넣으려고했으나 작동하지 않습니다.

아이디어가 있으십니까? 미리 감사드립니다.

답변

6

Maps API를 두 번로드하고 있습니다. 장소 라이브러리를 포함하여 한 번만 수행하십시오.

$.getScript("http://maps.google.com/maps/api/js?key=mykey&libraries=places&sensor=false&callback=MapApiLoaded"); 

function MapApiLoaded() { 
    alert('loading Maps API and Places library done'); 
} 
+0

정말 고마워요. 사실, 다른 질문이 있습니다. MapApiLoaded는 Google지도가로드되어 작동 중일 때 호출됩니다. 지역 정보 라이브러리도로드 될 때 이벤트가 발생했는지 알고 싶습니까? –

+0

여기에 질문을 추가했습니다. http://stackoverflow.com/questions/17135608/how-to-know-when-google-places-has-loaded-and-is-ready-asynchronously –

+0

음 ... 나 콜백이 호출되기 전에 모든 라이브러리가로드 될 것이라는 인상하에있었습니다. 만약 내가'console.log (google.maps.places);를 실행하면'MapApiLoaded'의 시작 부분에 객체가 있음을 보여줍니다. 하지만 어쩌면 라이브러리의 스텁이 전체 라이브러리가 아닌 해당 시점에로드되었을 수도 있습니다. –

관련 문제