我正在开发离子应用程序,我想将用户从应用程序重定向到Google地图应用程序,以向用户显示路线,但是当我Key restriction
将NONE
其设置为时,它可以完美运行。
但是,当我设置限制Android apps
并提供正确的软件包名称和SHA-1时,出现错误:Google Maps JavaScript API error: RefererNotAllowedMapError
。
我认为是因为:
ionic app is basically a webview. (which I am using)
如果这是我该如何保护我的API密钥的原因?
我使用代码在Android应用中打开Google地图是
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);
}
});
}
有人可以帮忙吗?
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打开Google 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] 删除。
我来说两句