Open Aarbel opened 2 years ago
I believe this is already implemented https://react-google-maps-api-docs.netlify.app/#loadscript
@Aarbel @EthanEFung Yes it is implemented in
@JustFly1984 I could help with that if you need
@EthanEFung Cool, just make PR into react-18 branch
@JustFly1984 While researching I came across an official google maps demo that I'm assuming encapsulates @Aarbel 's feature request. https://developers-dot-devsite-v2-prod.appspot.com/maps/documentation/javascript/demos/localization
Digging into the source code, the demo is doing a clever thing where each time either the language or the region is changed a query search parameter is added to the url and the entire page is re-rendered with the language or region that was specified.
and not possible to implement with official google script loader
The way I'm interpreting this statement is that developers might have an expectation that they could write something like
function Example() {
const [language, setLanguage] = React.useState('en');
return (
<>
<select onChange={(event) => setLanguage(event.currentTarget.value)}>
<option value="en">English</option>
<option value="fr">French</option>
{/* ... */}
</select>
<LoadScript language={language}>
{/*...*/}
</LoadScript>
</>
)
}
// or LoadScriptNext or hook flavored script loader
But this isn't the case because this library's load script implementations add the script straight to the document head and the official script loader currently doesn't support clean up of the dozens of elements it appends to the DOM. It doesn't seem straight forward to support a feature like this.
@Aarbel - It could be however, this was not your intention when proposing this feature. If you were intending to set the language or region onLoad only, then I still believe that the current implementations of LoadScript
, LoadScriptNext
, useLoadScript
, and useJsApiLoader
all support this, because this is how the official google maps api designed it to work.
@EthanEFung The idea to refresh the page is going against the SPA convention. You do not really want to reload an SPA, cos you will loose a state, and sometimes you need to re-login, it takes time to render the page. I'm sure this proposed behavior will mess up the code of many applications in production as it will be unexpected.
PS current work in going in react-18 branch
Thanks a lot @JustFly1984, would be amazing to be able to pass a locale to the react component, so that google maps is displayed in the chosen user language.
Do you plan to implement it ?
Thanks a lot !