使用“限制离子应用”保护Google Maps API密钥

拉吉什·皮卡蒙德(Ragesh Pikalmunde)

Google API金钥

我正在开发离子应用程序,我想将用户从应用程序重定向到Google地图应用程序,以向用户显示路线,但是当我Key restrictionNONE设置为时它可以完美运行。

但是,当我设置限制Android apps并提供正确的软件包名称和SHA-1时,出现错误:Google Maps JavaScript API error: RefererNotAllowedMapError

我认为是因为:

ionic app is basically a webview. (which I am using)

如果这是我该如何保护我的API密钥的原因?

我使用代码在Android应用中打开Goog​​le地图是

showDirection(currentLocation, destLocation) {
    let destination = destLocation.latitude + ',' + destLocation.longitude;
      var directionsService = new google.maps.DirectionsService;
      directionsService.route({
        origin: currentLocation,
        destination: destination,
        travelMode: 'DRIVING'
      }, function (response, status) {
        if (status === 'OK') {
          let mapUrl = 'http://maps.google.com/?';
          var route = response.routes[0];
          route.legs.forEach((value, index) => {
            if (index == 0) {
              mapUrl += 'saddr' + value.start_address + '&daddr=' + value.end_address;
            } else {
              mapUrl += '+to:' + value.end_address;
            }
          });
          window.location.href = mapUrl;
        } else {
          window.alert('Directions request failed due to ' + status);
        }
      });
  }

有人可以帮忙吗?

Xomena

Android app restriction is only valid for Google Maps SDK for Android. In your case when you use Ionic with WebView and Google Maps JavaScript API the only supported restriction is HTTP referrer.

You can check yourself which type of restriction is supported by each API on this page

https://developers.google.com/maps/faq#keysystem

In order to set valid HTTP referrer restriction in your Ionic app you should check what value has window.location.href when you open a map in your WebView.

For example, if the window.location.href is something like file://some/path you should use this value as referrer.

Also note that URLs with a file:// protocol require special representation as explained in

https://developers.google.com/maps/documentation/javascript/get-api-key#restrict_key

注意: file://引用者需要特殊的表示形式才能添加到密钥限制中。在添加到密钥限制之前,应将“ file://”部分替换为“ __file_url__”。例如,“ file:/// path / to /”的格式应为“ __file_url __ // path / to / *”。启用file://引荐来源后,建议您定期检查使用情况,以确保其符合您的期望。

更新

我相信您可以避免使用DirectionsService并直接在路线图模式下使用Google Maps URL打开Goog​​le Maps

该代码段可能是

showDirection(currentLocation, destLocation) {
    const destination = destLocation.latitude + ',' + destLocation.longitude;
    let mapUrl = "https://www.google.com/maps/dir/?api=1";
    mapUrl += "&origin=" + currentLocation;
    mapUrl += "&destination=" + destination;
    mapUrl += "&travelmode=driving";
    mapUrl += "&dir_action=navigate";
    window.location.href = mapUrl;
}

在这种情况下,您不需要使用Google Maps JavaScript API,不需要API密钥,也无需为使用Maps JavaScript API服务付费。

我希望这有帮助!

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Google Maps Geocoding的API密钥

来自分类Dev

Google Maps API密钥错误

来自分类Dev

google maps api,提供的API密钥无效

来自分类Dev

Google Maps SDK无法验证API密钥

来自分类Dev

动态设置GMLib的Google Maps Api密钥

来自分类Dev

没有密钥的Google Maps API?

来自分类Dev

Wordpress ACF Google Maps Field API密钥

来自分类Dev

无法获取Google Maps JavaScript API密钥

来自分类Dev

难以获取Google Maps的API密钥

来自分类Dev

要使用Google Maps API,需要哪个API密钥?

来自分类Dev

Google Maps API密钥-街景视图禁止使用403

来自分类Dev

在Android Studio中使用Gradle管理Google Maps API密钥

来自分类Dev

Google Maps API密钥-街景视图禁止使用403

来自分类Dev

提供API密钥后,Google Maps将无法使用

来自分类Dev

使用Google Maps Geocode查找(API密钥)的奇怪响应

来自分类Dev

您是否需要为客户端应用程序使用Google Maps API的API密钥?

来自分类Dev

Maps API密钥生成

来自分类Dev

如何在Ionic / Cordova应用程序中保护我的Google Maps Javascript V3 API密钥?

来自分类Dev

如何在Ionic / Cordova应用程序中保护Google Maps Javascript V3 API密钥?

来自分类Dev

用于文件:///的Google Maps API密钥引荐来源

来自分类Dev

Google Maps Places:为什么需要API密钥?

来自分类Dev

Google Maps iOS API密钥不起作用

来自分类Dev

在GitHub上共享我的Google Maps API密钥是否安全?

来自分类Dev

如何为Google Maps v2设置api密钥

来自分类Dev

在GitHub上共享我的Google Maps API密钥是否安全?

来自分类Dev

Google Maps Places:为什么需要API密钥?

来自分类Dev

从 config.json 文件提供 google maps api 密钥

来自分类Dev

启动 Activity 时出现 Google Maps API 密钥错误

来自分类Dev

隐藏Google Maps密钥

Related 相关文章

热门标签

归档