2016-07-28 4 views
5

की अलग फ़ाइलों में init जब मैं अपने गूगल मैप्स एपीआई का टुकड़ा था:कैसे गूगल मैप्स के लिए एक कॉलबैक बनाने के लिए एक वेब अनुप्रयोग

<script async defer src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap"></script> 

index.html में, मैं त्रुटि मिली: Uncaught InvalidValueError: initMap is not a function

मैं अपने Yemen-scaffolded AngularJS वेब ऐप पर अपने index.html फ़ाइल में अपने सभी bower_component, सीएसएस, एपीआई, और स्क्रिप्ट घोषणाओं को रखना चाहता हूं। जिस मानचित्र को मैं वास्तव में पुन: बनाने की कोशिश कर रहा था वह एक और मार्ग पर होगा, चलो इसे "afterlogin" मार्ग पर कॉल करें, बस basic map। मैंने जेएस और एचटीएमएल एचटीएमएल घटकों को afterlogin.js और afterlogin.html

इस के लिए कई संभावित कारण हैं। इनमें से एक नामस्थान से मेल खाने के लिए कॉल समायोजित करने के मामले के रूप में प्रस्तुत किया गया था, https://stackoverflow.com/a/34466824/1923016। क्या इसके लिए एक कोणीय सेवा की आवश्यकता होगी? यदि हां, तो सेवा initMap फ़ंक्शन और Google मानचित्र एपी स्निपेट में इसकी कॉल में कैसे काम करेगी?

जटिलताओं में से एक आदेश है। मैं वेब ऐप देव के लिए नया हूं, लेकिन इससे पहले कि मैं index.html पहले लोड कर सकता हूं, initMap फ़ंक्शन पर कॉलबैक बनाने के लिए स्निपेट में यूआरएल का उपयोग करता है जो <script>...</script> में index.html फ़ाइल में और app.js फ़ाइल में नहीं दिखाया गया है। इसके बजाए, चूंकि init फ़ंक्शन किसी रूट के जेएस कोड में है, इसलिए इसे देखा नहीं जा सकता है, इसलिए कॉल के किसी भी प्रकार के "नेमस्पेस" संस्करण की आवश्यकता है। यह लॉगिन मार्ग से भी एक कंसोल त्रुटि की ओर जाता है, जो मानचित्र के लिए div भी नहीं है।

---- संपादित करें: ----

भी ध्यान रखें कि इस मामले में, इस चाल नहीं किया:

window.initMap = function(){ 
//... 
} 

यह भी लागू नहीं होता है, समारोह के रूप में कभी नहीं कहा जाता है: Uncaught InvalidValueError: initMap is not a function

- - - - - -

afterlogin.js

angular.module('myappproject').controller('AfterloginCtrl', function ($scope) { 

    function initMap() { 
    var map = new google.maps.Map(document.getElementById('map'), { 
     zoom: 17, 
     center: {lat: -33.8666, lng: 151.1958} 
    }); 

    var marker = new google.maps.Marker({ 
     map: map, 
     // Define the place with a location, and a query string. 
     place: { 
     location: {lat: -33.8666, lng: 151.1958}, 
     query: 'Google, Sydney, Australia' 

     }, 
     // Attributions help users find your site again. 
     attribution: { 
     source: 'Google Maps JavaScript API', 
     webUrl: 'https://developers.google.com/maps/' 
     } 
    }); 

    // Construct a new InfoWindow. 
    var infoWindow = new google.maps.InfoWindow({ 
     content: 'Google Sydney' 
    }); 

    // Opens the InfoWindow when marker is clicked. 
    marker.addListener('click', function() { 
     infoWindow.open(map, marker); 
    }); 
    } 
}); 

<!DOCTYPE html> 
<html> 
    <head> 
     <title>after login page</title> 
     <meta name="viewport" content="initial-scale=1.0, user-scalable=no"> 
     <meta charset="utf-8"> 
     <style> 
      html, body { 
       height: 100%; 
       margin: 0; 
       padding: 0; 
       } 
      #map { 
       height: 100%; 
      } 
     </style> 
    </head> 
    <body> 

     <div id="map"></div> 

    </body> 
</html> 
+0

ए विशेषता 'async' को हटाने का प्रयास करें तो आपको कॉलबैक की आवश्यकता नहीं है। बी। आप एक मॉड्यूल का उपयोग कर सकते हैं जैसे [कोणीय Google मानचित्र] (http://angular-ui.github.io/angular-google-maps/#!/) –

+0

@MoshFeu कोई और त्रुटि नहीं, लेकिन कोई नक्शा – AlleyOOP

+0

दिखा रहा है यह नहीं करता है 'नियंत्रक' के अंत में 'initMap' को कॉल करने का प्रयास करें। –

उत्तर

9

के बाद से Google नक्शे SDK स्क्रिप्ट लोड एक synchronic (कारण async विशेषता), वहाँ है URL में callback पैरामीटर afterlogin.html।

In the script tag that loads the Maps JavaScript API, it is possible to omit the async attribute and the callback parameter. This will cause the loading of the API to block until the API is downloaded.

This will probably slow your page load. But it means you can write subsequent script tags assuming that the API is already loaded.

:

समस्या को हल करने के लिए आपको

The async attribute lets the browser render the rest of your website while the Maps JavaScript API loads. When the API is ready, it will call the function specified using the callback parameter.

https://developers.google.com/maps/documentation/javascript/tutorial#Loading_the_Maps_API

तो समाधान है स्क्रिप्ट synchronic लोड करने के लिए Google SDK में async तंत्र को समझने की जरूरत https://developers.google.com/maps/documentation/javascript/tutorial#sync

  1. आप निकाल सकते हैं async कि जिस तरह से विशेषता है, जब तक स्क्रिप्ट डाउनलोड किया पूरा हो जाएगा पेज चलना बंद और पेज पर चलेंगे। इसलिए, जब ब्राउज़र आपके कोड पर पहुंच जाएगा, तो सभी एसडीके ऑब्जेक्ट उपलब्ध होंगे।
  2. अब, चूंकि कोई कोड नहीं है जो initMap फ़ंक्शन पर कॉल करता है (याद रखें: इसे किसने कहा था, यह एसडीके स्क्रिप्ट था जो इसे केवल async मोड में कॉल करती है), आपको इसे स्वयं कॉल करना होगा। तो, बस इसे नियंत्रक के अंत में बुलाओ।
संबंधित मुद्दे