GoogleWebComponents / google-map

Google Maps web components

Home Page:https://elements.polymer-project.org/elements/google-map

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

element re-loads Maps API even when already loaded on the page

tigerhawkvok opened this issue · comments

It causes the following error to be thrown:

You have included the Google Maps API multiple times on this page. This may cause unexpected errors.

image

I can tell from the callback (and my own code) that it's the one that's being loaded later, since my call to it (I need it earlier) looks like this:

  unless google?.maps?
    loadJS "https://maps.googleapis.com/maps/api/js?key=#{gMapsApiKey}&callback=gMapsCallback"
  else
    window.gMapsCallback()

Can you provide a jsbin that repos the issue?

Here you go:

Screenshot from fiddle:

image

The fiddle's debug output is having an issue, but it doesn't affect the core of it.

The google-maps elements load the maps API for you. Since you're trying to load it separately, you'll need to load the URL with the same parameters (api key, lang, etc) that the components are using. Is there a reason you need to load it again? The components load all libraries by default. It should be everything you need.

Because whether the map is needed or not is determined later, based on user
permissions. It may not be used at all, and is therefore loaded lazily.

You'll also note it is loaded with the same api key.

On Tue, Jul 5, 2016, 07:24 Eric Bidelman notifications@github.com wrote:

The google-maps elements load the maps API for you. Since you're trying to
load it separately, you'll need to load the URL with the same parameters
(api key, lang, etc) that the components are using. Is there a reason you
need to load it again? The components load all libraries by default. It
should be everything you need.


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
#308 (comment),
or mute the thread
https://github.com/notifications/unsubscribe/AAKIMYxfRVKWPVAPRAppsGqLs5F8xL5-ks5qSmjsgaJpZM4JDvoE
.

-via Nexus 6P-


Philip Kahn
1314 Hopkins St
Berkeley CA
94702

510.859.3142

But can you help me understand why there's an explicit separate load call?
The components do that for you.

On Tue, Jul 5, 2016, 12:18 PM Philip Kahn notifications@github.com wrote:

Because whether the map is needed or not is determined later, based on user
permissions. It may not be used at all, and is therefore loaded lazily.

You'll also note it is loaded with the same api key.

On Tue, Jul 5, 2016, 07:24 Eric Bidelman notifications@github.com wrote:

The google-maps elements load the maps API for you. Since you're trying
to
load it separately, you'll need to load the URL with the same parameters
(api key, lang, etc) that the components are using. Is there a reason you
need to load it again? The components load all libraries by default. It
should be everything you need.


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<
#308 (comment)
,
or mute the thread
<
https://github.com/notifications/unsubscribe/AAKIMYxfRVKWPVAPRAppsGqLs5F8xL5-ks5qSmjsgaJpZM4JDvoE

.

-via Nexus 6P-


Philip Kahn
1314 Hopkins St
Berkeley CA
94702

510.859.3142


You are receiving this because you modified the open/close state.

Reply to this email directly, view it on GitHub
#308 (comment),
or mute the thread
https://github.com/notifications/unsubscribe/AAOigPnBMl7bHil4rymStZNds_Oj_89aks5qSpHwgaJpZM4JDvoE
.

Because I always need the maps API for other functions (say, maybe a
geocode) whether or not a map (and therefore a google-map) may be needed.

In my case, this relates to restricted permissions to datasets based on
users and published state of the data.

On Tue, Jul 5, 2016, 12:25 Eric Bidelman notifications@github.com wrote:

But can you help me understand why there's an explicit separate load call?
The components do that for you.

On Tue, Jul 5, 2016, 12:18 PM Philip Kahn notifications@github.com
wrote:

Because whether the map is needed or not is determined later, based on
user
permissions. It may not be used at all, and is therefore loaded lazily.

You'll also note it is loaded with the same api key.

On Tue, Jul 5, 2016, 07:24 Eric Bidelman notifications@github.com
wrote:

The google-maps elements load the maps API for you. Since you're trying
to
load it separately, you'll need to load the URL with the same
parameters
(api key, lang, etc) that the components are using. Is there a reason
you
need to load it again? The components load all libraries by default. It
should be everything you need.


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<

#308 (comment)

,
or mute the thread
<

https://github.com/notifications/unsubscribe/AAKIMYxfRVKWPVAPRAppsGqLs5F8xL5-ks5qSmjsgaJpZM4JDvoE

.

-via Nexus 6P-


Philip Kahn
1314 Hopkins St
Berkeley CA
94702

510.859.3142


You are receiving this because you modified the open/close state.

Reply to this email directly, view it on GitHub
<
#308 (comment)
,
or mute the thread
<
https://github.com/notifications/unsubscribe/AAOigPnBMl7bHil4rymStZNds_Oj_89aks5qSpHwgaJpZM4JDvoE

.


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
#308 (comment),
or mute the thread
https://github.com/notifications/unsubscribe/AAKIMeGWTeaXGITVlviqLvlVsvS7X1aiks5qSq_HgaJpZM4JDvoE
.

-via Nexus 6P-


Philip Kahn
1314 Hopkins St
Berkeley CA
94702

510.859.3142

I see. So we try to load all (most?) of the libraries by default to prevent
these types of issues. From there you can just use the apis directly on the
page once a map component is loaded. Should we add geocode?

https://github.com/GoogleWebComponents/google-apis/blob/master/google-maps-api.html#L115

On Tue, Jul 5, 2016, 4:45 PM Philip Kahn notifications@github.com wrote:

Because I always need the maps API for other functions (say, maybe a
geocode) whether or not a map (and therefore a google-map) may be needed.

In my case, this relates to restricted permissions to datasets based on
users and published state of the data.

On Tue, Jul 5, 2016, 12:25 Eric Bidelman notifications@github.com wrote:

But can you help me understand why there's an explicit separate load
call?
The components do that for you.

On Tue, Jul 5, 2016, 12:18 PM Philip Kahn notifications@github.com
wrote:

Because whether the map is needed or not is determined later, based on
user
permissions. It may not be used at all, and is therefore loaded lazily.

You'll also note it is loaded with the same api key.

On Tue, Jul 5, 2016, 07:24 Eric Bidelman notifications@github.com
wrote:

The google-maps elements load the maps API for you. Since you're
trying
to
load it separately, you'll need to load the URL with the same
parameters
(api key, lang, etc) that the components are using. Is there a reason
you
need to load it again? The components load all libraries by default.
It
should be everything you need.


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<

#308 (comment)

,
or mute the thread
<

https://github.com/notifications/unsubscribe/AAKIMYxfRVKWPVAPRAppsGqLs5F8xL5-ks5qSmjsgaJpZM4JDvoE

.

-via Nexus 6P-


Philip Kahn
1314 Hopkins St
Berkeley CA
94702

510.859.3142


You are receiving this because you modified the open/close state.

Reply to this email directly, view it on GitHub
<

#308 (comment)

,
or mute the thread
<

https://github.com/notifications/unsubscribe/AAOigPnBMl7bHil4rymStZNds_Oj_89aks5qSpHwgaJpZM4JDvoE

.


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<
#308 (comment)
,
or mute the thread
<
https://github.com/notifications/unsubscribe/AAKIMeGWTeaXGITVlviqLvlVsvS7X1aiks5qSq_HgaJpZM4JDvoE

.

-via Nexus 6P-


Philip Kahn
1314 Hopkins St
Berkeley CA
94702

510.859.3142


You are receiving this because you modified the open/close state.

Reply to this email directly, view it on GitHub
#308 (comment),
or mute the thread
https://github.com/notifications/unsubscribe/AAOigPGRUKWEKQBkEh91X6XZMPYrAz13ks5qStCFgaJpZM4JDvoE
.

So you're suggesting just loading the <google-maps-api> component initially (essentially instead of the loadJS() call in the fiddle, append it to the <head>, for example), which will be respected by the <google-map> component and avoid this issue?

If so, then yes, I think geocoding is the big missing piece there.

Exactly. You wouldn't even need <google-maps-api>, because the map component loads it internally.

That said, it looks like the Geocode API is a bit different than loading the Maps API (+ it's libraries):
https://developers.google.com/maps/documentation/geolocation/intro

The API is a bit different, yes, but it's instanced with

geocoder = new google.maps.Geocoder

See https://developers.google.com/maps/documentation/javascript/geocoding#GeocodingRequests or https://developers.google.com/maps/documentation/javascript/geocoding#reverse-geocoding-by-location to see the google.maps call.

But, as I said, it is possible to need the maps API without an actual map element ;-) that's the whole point of this issue -- if that determination is made asynchronously, and the api is needed earlier, then it shouldn't be double-pulled. If the <google-maps-api> element would prevent that double pull, though, then that should be good enough.

Yep. If you always load via <google-maps-api>, it should be de-duped.