我有一个带有地图/地理位置的google maps API设置,并且我已将API密钥限制为我们的域(包括子域等)。但是,我正在使用的代码在关闭限制的情况下有效,但在打开限制的情况下不起作用,并且我不确定如何修复它。
我看过这个线程-API key Browser API keys cannot have referer restrictions when used with this API-但这绝对是javascript /客户端调用,所以不确定是什么问题吗?
这是我的代码:
<script>
var map;
function initMap(){
$.ajax({
type: "GET",
url: "https://maps.googleapis.com/maps/api/geocode/json?address=Australia&key=***API KEY***",
dataType: "json",
}).success(function(data){
processJSON(data);
});
function processJSON(json) {
console.log(json);
var latitude = json.results[0].geometry.location.lat;
var longitude = json.results[0].geometry.location.lng;
var latlng = new google.maps.LatLng(latitude,longitude);
var options = {
zoom:5,
center:latlng,
scrollwheel:false,
navigationControl:false,
mapTypeControl:false,
mapTypeId:google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById('locatorMap'), options);
var locations = [
['<strong>Blonde Robot (Distributor)</strong><br />1/8 Theobald Street<br />Thornbury, Vic 3071<br />Australia<br /><br /><strong>T:</strong> (03) 9023 9777<br /><strong>E:</strong> <a href="mailto:sales@blonde-robot.com.au">sales@blonde-robot.com.au</a><br /><strong>W:</strong> <a href="http://www.blonde-robot.com.au/" target="_blank">Visit Website</a>',-37.756359,145.027496,'/templates/home/images/google-pin.png'],
['<strong>Bluefish444 (Manufacturer)</strong><br />93 Howard Street<br />North Melbourne, Victoria 3051<br />Australia<br /><br /><strong>T:</strong> +61 3 9682 9477<br /><strong>F:</strong> +61 3 9328 2031<br /><strong>E:</strong> <a href="mailto:sales@bluefish444.com">sales@bluefish444.com</a><br /><strong>W:</strong> <a href="https://bluefish444.com" target="_blank">Visit Website</a>',-37.804852,144.953537,'/templates/home/images/google-pin.png'],
['<strong>Corsair Solutions</strong><br />Level 1, 108 Johnston St<br />Collingwood, VIC 3066<br />Australia<br /><br /><strong>T:</strong> 61 3 9005 9861<br /><strong>W:</strong> <a href="http://www.corsairsolutions.com.au" target="_blank">Visit Website</a>',-37.799187,144.988663,'/templates/home/images/google-pin.png'],
['<strong>Techtel Melbourne</strong><br />Level 3, Collins Street Tower 480 Collins Street<br />Melbourne, Victoria 3000<br />Australia<br /><br /><strong>T:</strong> +61 (0)3 9899 3032<br /><strong>W:</strong> <a href="http://www.techtel.com.au/" target="_blank">Visit Website</a>',-37.817650,144.957947,'/templates/home/images/google-pin.png']
];
var markers = new Array();
var marker,i;
var infowindow = new google.maps.InfoWindow();
for(i=0;i<locations.length;i++){
var imageIcon = new google.maps.MarkerImage(locations[i][3],
new google.maps.Size(50,35),
new google.maps.Point(0,0),
new google.maps.Point(25,35));
marker = new google.maps.Marker({
position:new google.maps.LatLng(locations[i][1],locations[i][2]),
map:map,
icon:imageIcon
});
markers.push(marker);
google.maps.event.addListener(marker,'click',(function(marker,i){
return function(){
infowindow.setContent(locations[i][0]);
infowindow.open(map,marker);
}
})(marker,i));
}
}
}
initMap();
</script>
我得到的错误是这样的:
error_message:“具有引用限制的API密钥不能与 该API。”