Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
166
rated 0 times [  169] [ 3]  / answers: 1 / hits: 28191  / 7 Years ago, mon, october 23, 2017, 12:00:00

Is there any package available on npm for google maps? Or am I really
supposed to paste this



<script src=https://maps.googleapis.com/maps/api/jskey=YOUR_API_KEY>
</script>


to my index.html and download this js file on every refresh?



This is super annoying, because sometimes I get ReferenceError: google is not defined.


More From » npm

 Answers
30

Or am I really supposed to paste this
to my index.html and download this js file on every refresh?



Yes. This is the only way to do so. There are some packages to dynamically do this for you, but the behavior is the same.


To reiterate, there is no official package for loading the Google Maps JavaScript for the web environment on NPM. The @google/maps referenced by others is for node only.


google is not defined errors can be avoided by using the callback query parameter when loading the google maps script.


Update - 2020/01/17


I wrote @googlemaps/js-api-loader to help load the script dynamically and support promises.


import { Loader } from '@googlemaps/js-api-loader';

const loader = new Loader({
apiKey: "",
version: "weekly",
libraries: []
});

loader
.load()
.then(() => {
new google.maps.Map(div, mapOptions);
})
.catch(e => {
// do something
});

[#56146] Friday, October 20, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
rociom

Total Points: 287
Total Questions: 88
Total Answers: 101

Location: Oman
Member since Wed, Nov 17, 2021
3 Years ago
;