简体   繁体   English

动态设置GMLib的Google Maps Api Key

[英]Dynamic setting of Google Maps Api Key for GMLib component

In the past it was a question about setting Google Maps Api Key for the gmlib component. 过去,这是有关为gmlib组件设置Google Maps Api Key的问题。 The provided solution was to change the 'map.html' head element then recompile the resources (rc.cmd) and recompile the component. 提供的解决方案是更改'map.html'头元素,然后重新编译资源(rc.cmd)并重新编译组件。 This is a quite static solution and sometimes it would be more convenient to set the api key dynamically at runtime eg when someone want to use different keys for different projects (api keys can be stored in some ini/property file of the projects). 这是一个非常静态的解决方案,有时在运行时动态设置api密钥会更方便,例如,当某人想为不同的项目使用不同的密钥时(api密钥可以存储在项目的ini / property文件中)。 I think the best way would be a property for the GMMap component and a new parameter for the constructor. 我认为最好的方法是GMMap组件的属性和构造函数的新参数。 I searched google maps javascript api for some functionality like changing/setting api key but I didn't find any. 我在google maps javascript api中搜索了一些功能,例如更改/设置api键,但没有找到任何功能。 It seems that api key has to be 'hardcoded' into the head tag of the html file and there is no javascript function to change it. 看来api键必须“硬编码”到html文件的head标签中,并且没有JavaScript函数可以对其进行更改。 (If some javascript function existed then an ExecuteScript would call it.) (如果存在某些javascript函数,则ExecuteScript会调用它。)

Any idea how to set api key at runtime? 任何想法如何在运行时设置api密钥?

For example I found this link that changes the document itself: Can I change/set the Google Maps API Key dynamically from JavaScript? 例如,我发现此链接可以更改文档本身:是否可以通过JavaScript动态更改/设置Google Maps API密钥?

How to utilize this in gmlib? 如何在gmlib中利用它?

Thanks in advance. 提前致谢。

Really, this solution is not necessary into GMLib because when you (he components) loads the HTML map from the resource file, you can changes this line (you can adds the key) by code before load it into the TWebBrowser. 确实,此解决方案对于GMLib来说不是必需的,因为当您(他的组件)从资源文件加载HTML映射时,可以在加载到TWebBrowser之前通过代码更改此行(可以添加键)。

You needs to add a key property in TGMMap and, into the GetBaseHTMLCode method, take into account this property. 您需要在TGMMap中添加键属性,并在GetBaseHTMLCode方法中考虑此属性。

To easy change this key, you can changes this line from the HTML code 为了轻松更改此键,您可以从HTML代码更改此行

<script type="text/javascript" src="https://maps.google.com/maps/api/js?sensor=false&libraries=weather,panoramio,geometry,drawing"></script>

by this other 被这个

<script type="text/javascript" src="https://maps.google.com/maps/api/js?%ssensor=false&libraries=weather,panoramio,geometry,drawing"></script>

And change the GetBaseHTMLCode method for this 并为此更改GetBaseHTMLCode方法

function TCustomGMMap.GetBaseHTMLCode: string;
var
  List: TStringList;
  Stream: TResourceStream;
begin
  Result := '';

  List := TStringList.Create;
  try
    try
      Stream := TResourceStream.Create(HInstance, RES_MAPA_CODE, RT_RCDATA);
      List.LoadFromStream(Stream);
      Result := List.Text;
      Result := Format(Result, [KeyProerty]); // <== add this line
    finally
      if Assigned(Stream) then FreeAndNil(Stream);
      if Assigned(List) then FreeAndNil(List);
    end;
  except
    raise Exception.Create(GetTranslateText('No se ha podido cargar el recurso', Language));
  end;
end;

This change is not tested but this is the idea :-) 此更改尚未测试,但这是想法:-)

I put it into my TODO list for this week ;-) 我将其放入本周的待办事项列表中;-)

Regards 问候

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM