Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change angular-google-maps language dynamically

Is it possible to change map language dynamically when language is changed ? Or at least have the language map changed the next time I access it (after language is changed).

I can set default language on map loading using this code (in mymap.module.ts) :

@NgModule({ imports: [ 
  AgmCoreModule.forRoot({ apiKey: 'MY_KEY',
  language: 'es', }),
  ]
})

And I can get the current language using this.translate.currentLang (in mymap.component.ts).

But I don't know how I can combine both.

like image 682
Michel_ Avatar asked Mar 24 '18 09:03

Michel_


People also ask

Why is my Google Maps in the wrong language?

Here is how to change Google Maps navigation language: Open the Google Maps app, on your Android phone or tablet, Now tap on your profile picture or initial Account Circle and then Settings. Then tap on Navigation settings and then on Voice. Choose a voice and language.

Is Google Maps written in angular?

About. Angular Google Maps is a set of directives (part of angular-ui) written in CoffeeScript and Javascript which integrate Google Maps in an AngularJS applications.

Can you style Google Maps API?

With style options you can customize the presentation of the standard Google map styles, changing the visual display of features like roads, parks, businesses, and other points of interest.


2 Answers

In order to change map's language, a bunch of localized JS scripts need to be refetched anew. So, you can just try to refresh entire page (JS not Angular) providing wanted language via local storage for example:

@NgModule({ 
  imports: [ 
    AgmCoreModule.forRoot({ 
      apiKey: 'MY_KEY',
      language: localStorage && localStorage.gml || 'en'
    }),
  ]
})

to reload your page, use window.location.reload() method

StackBLITZ: https://stackblitz.com/edit/angular-google-maps-demo-f3xzhn?file=app%2Fapp.module.ts

like image 139
Andriy Avatar answered Oct 11 '22 19:10

Andriy


In app.component add the following make sure upon language change to update "lang" in local storage and reload the page using window.location.reload()

export class AppComponent implements OnInit {
  constructor() { }

  ngOnInit() {

    var script = document.createElement('script');
    script.src = `https://maps.googleapis.com/maps/api/js?v=quarterly&key=${KEY}&libraries=places&language=${localStorage && localStorage.lang || 'en'}`;
    document.head.appendChild(script);
  }
}

In your relevant module add:

@NgModule({ 
  imports: [ 
    AgmCoreModule.forRoot(),
  ]
})
like image 27
Gil Salomon Avatar answered Oct 11 '22 20:10

Gil Salomon