PS1 Posted April 24, 2025 Posted April 24, 2025 Hello, Is it possible to set ApiKey for Google layer in UnimMap in runtime ? I want to get it from database and set it. Different users will have different keys. I tried setting it in ServerModule in OnCreate, OnBeforeInit, OnServerStartup but it didn't work I'm using: uniGUI Complete -Professional Edition Build 1.95.0.1599
Sherzod Posted April 24, 2025 Posted April 24, 2025 Hello @PS1 Instead of using CustomFiles, you can load the script directly by assigning: UnimHTMLFrame.BeforeScript -> Ext.Loader.loadScript('https://maps.googleapis.com/maps/api/js?sensor=false&key=your_key');
Sherzod Posted April 25, 2025 Posted April 25, 2025 Hello, You can use this method: https://www.unigui.com/doc/online_help/api/uniMap_TUniMap_AddGoogleJSLibrary@String.html
PS1 Posted April 28, 2025 Author Posted April 28, 2025 Hello, I tested your method, but... if i have login form it dosen't seem to work... I attached test project. There is a login form and it won't work, but if you remove it from the project it starts to work somehow. Can you look what is happening and why? Mapa.zip
Sherzod Posted April 30, 2025 Posted April 30, 2025 @PS1 Sorry, could you please provide a working UniGUI project where the Google Maps API key is assigned at design time (not at runtime)? That would help clarify your approach.
PS1 Posted April 30, 2025 Author Posted April 30, 2025 Here you go, i have only added ApiKey in UniMapKeys1 in servremodule Mapa_v2.zip
Sherzod Posted April 30, 2025 Posted April 30, 2025 22 minutes ago, PS1 said: Mapa_v2.zip 9.71 MB · 0 downloads Thank you. But, it seems I didn’t notice any difference in behavior between the two test projects.
PS1 Posted April 30, 2025 Author Posted April 30, 2025 In first project i only set ApiKey in runtime and as you can see the map is NOT showing correctly. In second project (uploaded today) i set ApiKey in design time and the map is showing correctly. But... In first project if you delete the login form, the map suddenly starts to work.
PS1 Posted May 7, 2025 Author Posted May 7, 2025 It looks like a bug for me. Is there any walkaround ?
Hayri ASLAN Posted May 7, 2025 Posted May 7, 2025 5 hours ago, PS1 said: It looks like a bug for me. Is there any walkaround ? Hello We are looking into it 1
Hayri ASLAN Posted May 7, 2025 Posted May 7, 2025 6 hours ago, PS1 said: It looks like a bug for me. Is there any walkaround ? Leave the GoogleApiKey property blank on the UniServerModule.UniMapKeys component. In your MainForm.OnCreate event, register the Maps API with your key: procedure TMainForm.UniFormCreate(Sender: TObject); begin UniMap1.AddGoogleJSLibrary('YOUR_API_KEY'); end;
PS1 Posted May 7, 2025 Author Posted May 7, 2025 Thanks for the response. But it didn't help. You can check it with project i uploaded in previous post. keep in mind that it happens only if i have login form in my application
Hayri ASLAN Posted May 7, 2025 Posted May 7, 2025 3 hours ago, PS1 said: Thanks for the response. But it didn't help. You can check it with project i uploaded in previous post. keep in mind that it happens only if i have login form in my application LoginForm -> Script function replaceMapsApi(key) { const old = document.querySelector( 'script[src^="https://maps.googleapis.com/maps/api/js"]' ); if (old) old.remove(); if (window.google && window.google.maps) { delete window.google.maps; delete window.google; } const s = document.createElement('script'); s.src = `https://maps.googleapis.com/maps/api/js?key=${key}`; s.async = true; s.defer = true; document.head.appendChild(s); } When you have the key, call UniSession.AddJS('replaceMapsApi("YOUR_API_KEY");'); 1
Recommended Posts