我如何从网络配置文件中读取谷歌 API key 并将其附加到谷歌 API 引用。
我在外部文件中有 jquery 函数,我在 asp.net 页面中添加对该文件的引用。我认为在页面引用上添加 api key 不是一个好的做法。
aspx 页面
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script src="../scripts/GeoCode.js" type="text/javascript"></script>
外部js文件
function GetGeoCode() {
var txtLat = $('[id$=txtLat]');
var txtLng = $('[id$=txtLng]');
var geocoder = new google.maps.Geocoder();
var address = "my address";
geocoder.geocode({ 'address': address }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var latitude = results[0].geometry.location.lat();
var longitude = results[0].geometry.location.lng();
txtLat.val(latitude);
txtLng.val(longitude);
}
});
最佳答案
我知道您可能担心有人“偷”了您的 key -
然而,您将在客户端 JS 中需要它,无论您多么努力地试图混淆或隐藏它,它在某些时候都会在客户端,所以真正想要掌握它的人会。
解决方法: 您可以设置允许的推荐人,这样您的 key 就不能被其他人使用。
https://developers.google.com/maps/documentation/business/places/auth#limiting_referrers
关于javascript - 如何从网络配置文件中获取谷歌地图 api key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26284461/