Jump to content

Recommended Posts

Posted

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

Posted

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');

Posted

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

Posted

@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.

Posted
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.

Posted

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.

Posted
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;

 

Posted

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

Posted
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");');

 

  • Thanks 1
×
×
  • Create New...