js api loader
v1.16.9
动态加载Google Maps JavaScript API脚本。这从Google-Maps NPM软件包中获得了灵感,但用ES6,Prosisise和tyscript对其进行了更新。
可通过NPM作为软件包 @googlemaps/js-api loader获得。
npm i @googlemaps/js-api-loader另外,您可以使用UMPKG链接将UMD软件包直接添加到HTML文档中。
< script src =" https://unpkg.com/@googlemaps/[email protected]/dist/index.min.js " > </ script >通过UNPKG添加时,可以在google.maps.plugins.loader.Loader上访问加载程序。
打字稿用户需要安装以下类型软件包。
npm i -D @types/google.maps参考文档可以在此链接上找到。 Google Maps JavaScript API文档是加载程序选项的权威来源。
import { Loader } from '@googlemaps/js-api-loader' ;
const loader = new Loader ( {
apiKey : "" ,
version : "weekly" ,
libraries : [ "places" ]
} ) ;
const mapOptions = {
center : {
lat : 0 ,
lng : 0
} ,
zoom : 4
} ;使用特定库的承诺。
// Promise for a specific library
loader
. importLibrary ( 'maps' )
. then ( ( { Map } ) => {
new Map ( document . getElementById ( "map" ) , mapOptions ) ;
} )
. catch ( ( e ) => {
// do something
} ) ;使用脚本加载何时的承诺。
// Promise
loader
. load ( )
. then ( ( google ) => {
new google . maps . Map ( document . getElementById ( "map" ) , mapOptions ) ;
} )
. catch ( e => {
// do something
} ) ;另外,如果要使用回调。
// Callback
loader . loadCallback ( e => {
if ( e ) {
console . log ( e ) ;
} else {
new google . maps . Map ( document . getElementById ( "map" ) , mapOptions ) ;
}
} ) ;在此处查看行动中的软件包。
该图书馆得到了社区的支持。我们对图书馆的稳定性和功能感到满意,我们希望您在其上构建真实的生产应用程序。
如果您找到错误或有功能建议,请记录问题。如果您想做出贡献,请阅读如何做出贡献。