2017-09-14 79 views
1

我目前正在嘗試創建一個頁面,該頁面可以找到用戶所在的位置,然後使用Google地圖位置庫顯示他們附近的醫院。使用Angular 2谷歌地圖地方加載附近的醫院

這個Codepen是我在加載頁面時想顯示的一個完美例子。但是,實現該代碼並沒有有效地處理我的Angular/Ionic應用程序。

首先,我開始通過裝載使用

<script src="https://maps.googleapis.com/maps/api/js 
key=AIzaSyBuBzeYkYimIquGG5KkIcB6vFmtHMUzDFo&libraries=places" async defer> 
</script> 

,然後針對組件內的谷歌地圖圖書館我創建了一個ngOnInit()將運行在codepen中的JavaScript。除了該函數的末尾,我添加了initMap(),以便它初始化地圖,就像在Codepen中它是對腳本的回調一樣。其次,我試圖讓它與Angular Google Maps一起工作,但再次無法讓它起作用。當我試圖把它與AGM的工作,我收到了試圖去哪個地圖所在的頁面時,出現以下錯誤:

Error: Uncaught (in promise): Error: Cannot find control with unspecified 
name attribute 
Error: Cannot find control with unspecified name attribute 
at _throwError (forms.es5.js:1830) 
at setUpControl (forms.es5.js:1738) 
at FormControlDirective.ngOnChanges (forms.es5.js:4520) 
at checkAndUpdateDirectiveInline (core.es5.js:10812) 
at checkAndUpdateNodeInline (core.es5.js:12238) 
at checkAndUpdateNode (core.es5.js:12177) 
at debugCheckAndUpdateNode (core.es5.js:12880) 
at debugCheckDirectivesFn (core.es5.js:12821) 
at Object.eval [as updateDirectives] (MapPage.html:17) 
at Object.debugUpdateDirectives [as updateDirectives] (core.es5.js:12806) 
at _throwError (forms.es5.js:1830) 
at setUpControl (forms.es5.js:1738) 
at FormControlDirective.ngOnChanges (forms.es5.js:4520) 
at checkAndUpdateDirectiveInline (core.es5.js:10812) 
at checkAndUpdateNodeInline (core.es5.js:12238) 
at checkAndUpdateNode (core.es5.js:12177) 
at debugCheckAndUpdateNode (core.es5.js:12880) 
at debugCheckDirectivesFn (core.es5.js:12821) 
at Object.eval [as updateDirectives] (MapPage.html:17) 
at Object.debugUpdateDirectives [as updateDirectives] (core.es5.js:12806) 
at c (polyfills.js:3) 
at Object.reject (polyfills.js:3) 
at NavControllerBase._fireError (nav-controller-base.js:322) 
at NavControllerBase._failed (nav-controller-base.js:310) 
at nav-controller-base.js:365 
at t.invoke (polyfills.js:3) 
at Object.onInvoke (core.es5.js:4149) 
at t.invoke (polyfills.js:3) 
at r.run (polyfills.js:3) 
at polyfills.js:3 

但我不能完全肯定這個問題有與AGM做,因爲一兩秒後,出現一個參考錯誤,並說ReferenceError: google is not defined。我之前得到這個錯誤,並且我運行命令npm install --save @types/googlemaps爲了下載谷歌的類型,但仍然收到這個錯誤,即使我這樣做。要重申,我嘗試使用Google Maps Places加載用戶附近的醫院,但無法使用Angular與此工作,但上面鏈接的Codepen是我希望將它帶入我的應用程序的功能,任何幫助將不勝感激。

回答

1

就像codepen您鏈接,該腳本調用有一個參數去了解其工作模式使用其回調&callback=initMap

<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBuBzeYkYimIquGG5KkIcB6vFmtHMUzDFo&signed_in=true&libraries=places&callback=initMap" async defer></script>

您得到ReferenceError: google is not defined因爲劇本是異步調用,在加載腳本之前,會提前調用initMap()。所以就像在codepen中做的一樣。