如何为谷歌地图制定谷歌Api内容安全策略?

问题描述 投票:1回答:1

我正在尝试使用Electron.js制作一个小程序,并试图在其中添加一个Google地图页面。我已经获得了一个API密钥。当我运行应用程序时,它短暂地显示了一个谷歌地图的图像,然后弹出错误:"Oops!Something went wrong.This page didn't load Google Maps correctly. 当我运行应用程序时,它短暂地显示了谷歌地图的图像,然后弹出错误:"Oops!Something went wrong.This page didn't load Google Maps correctly. 请参阅JavaScript控制台了解技术细节。"当我打开控制台时,我看到了错误。

"security-warnings.ts:179 Electron Security Warning (Insecure Content-Security-Policy) This renderer process has either no Content Security
    Policy set or a policy with "unsafe-eval" enabled. This exposes users of
    this app to unnecessary security risks.

For more information and help, consult
https://electronjs.org/docs/tutorial/security.
This warning will not show up
once the app is packaged.
(anonymous) @ security-warnings.ts:179
Promise.then (async)
warnAboutInsecureCSP @ security-warnings.ts:172
logSecurityWarnings @ security-warnings.ts:295
loadHandler @ security-warnings.ts:312
async function (async)
loadHandler @ security-warnings.ts:311
load (async)
securityWarnings @ security-warnings.ts:315
(anonymous) @ init.ts:216
./lib/renderer/init.ts @ init.ts:217
__webpack_require__ @ bootstrap:19
(anonymous) @ bootstrap:83
(anonymous) @ bootstrap:83
NativeModule.compile @ internal/bootstrap/loaders.js:287
NativeModule.compileForPublicLoader @ internal/bootstrap/loaders.js:222
loadNativeModule @ internal/modules/cjs/helpers.js:23
Module._load @ internal/modules/cjs/loader.js:699
Module._load @ electron/js2c/asar.js:717
Module._load @ electron/js2c/asar.js:717
Module.runMain @ internal/modules/cjs/loader.js:1038
(anonymous) @ internal/main/run_main_module.js:17
js?key=MY_API_KEY&callback=initMap:56 Google Maps JavaScript API error: ApiNotActivatedMapError
https://developers.google.com/maps/documentation/javascript/error-messages#api-not-activated-map-error
_.od @ js?key=MY_API_KEY&callback=initMap:56
(anonymous) @ common.js:73
(anonymous) @ common.js:149
c @ common.js:67
(anonymous) @ AuthenticationService.Authenticate?1sfile%3A%2F%2F%2FUsers%2Fisaiahbell%2FProjects%2Fgeo-app%2Fmap.html&MY_API_KEY&callback=_xdc_._m2aezz&key=YOUR_API_KEY&token=54562:1"

我当前的代码如下。

Index.html

 <!DOCTYPE html>
    <html>
      <head>
        </head>
      <body>


        <center>
          <a href="map.html">Open Maps</a>
        </center>








         <script src="./render.js"></script>
      </body>
    </html>




    <script async defer src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap"
    type="text/javascript"></script>

    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="UTF-8">


        <meta http-equiv="Content-Security-Policy" content="script-src 'self' 'unsafe-inline';" />
      </head>
      <body>

我想用Electron.js来制作一个小程序,并试图在其中添加一个Google地图页面。

<!DOCTYPE html>

<html>

  <head>



    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <title>Simple Markers</title>
    <style>
      /* Always set the map height explicitly to define the size of the div
       * element that contains the map. */
      #map {
        height: 100%;
      }
      /* Optional: Makes the sample page fill the window. */
      html, body {
        height: 100%;
        margin: 0;
        padding: 0;
      }
    </style>



  </head>
  <body>
    <button><a href="index.html">Go Back</a><</button>
    <div id="map"></div>
    <script>

      function initMap() {
        var myLatLng = {lat: -25.363, lng: 131.044};

        var map = new google.maps.Map(document.getElementById('map'), {
          zoom: 4,
          center: myLatLng
        });

        var marker = new google.maps.Marker({
          position: myLatLng,
          map: map,
          title: 'Hello World!'
        });
      }
    </script>
    <script async defer
    src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY">
    </script>
  </body>
</html>
javascript html google-maps google-maps-api-3 electron
1个回答
0
投票

注意,你得到这个错误。

谷歌地图的JavaScript API错误。ApiNotActivatedMapError(未激活地图错误) https:/developers.google.commapsdocumentationjavascripterror-messages#api-not-activated-map-error。

这意味着,你需要 启用 的JavaScript API。

如果启用后,您遇到了与CSP相关的错误,使您的地图无法加载,那么检查一下 如何允许内容安全策略从google api运行外部javascript?

希望能帮到你

© www.soinside.com 2019 - 2024. All rights reserved.