Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/453.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何显示基于地理位置的动态图像?_Javascript_Ajax_Api_Geolocation - Fatal编程技术网

Javascript 如何显示基于地理位置的动态图像?

Javascript 如何显示基于地理位置的动态图像?,javascript,ajax,api,geolocation,Javascript,Ajax,Api,Geolocation,我试图根据用户访问我的网站的国家显示特定的图像。我已经设法使用ajax和捕获位置信息 如果我从美国或任何其他国家检查此项,我可以输出该国(使用),但我的目标是根据输出国显示不同的图像 我错过了什么 //获取ip、城市、州和国家 $.ajax({ url:“https://geolocation-db.com/jsonp", jsonpCallback:“回调”, 数据类型:“jsonp”, 成功:功能(位置){ $(“#country”).html(location.country_name)

我试图根据用户访问我的网站的国家显示特定的图像。我已经设法使用ajax和捕获位置信息

如果我从美国或任何其他国家检查此项,我可以输出该国(使用),但我的目标是根据输出国显示不同的图像

我错过了什么

//获取ip、城市、州和国家
$.ajax({
url:“https://geolocation-db.com/jsonp",
jsonpCallback:“回调”,
数据类型:“jsonp”,
成功:功能(位置){
$(“#country”).html(location.country_name);
},
});
让getCountry=location.country\u name;
如果(getCountry==“美国”){
bg.innerHTML=`;
}else if(getCountry==‘英国’){
bg.innerHTML=`;
}否则{
bg.innerHTML=`这不起作用!`;
}

您需要在成功方法中检查国家/地区

//获取ip、城市、州和国家
$.ajax({
url:“https://geolocation-db.com/jsonp",
jsonpCallback:“回调”,
数据类型:“jsonp”,
成功:功能(位置){
$(“#country”).html(location.country_name);
让getCountry=location.country\u name;
如果(getCountry==“美国”){
bg.innerHTML=`;
}else if(getCountry==‘英国’){
bg.innerHTML=`;
}否则{
bg.innerHTML=`这不起作用!`;
}
},
});


您可以在不同的国家/地区使用开关盒,但这需要大量工作。顺便说一句,当比较国家代码不是此API返回数据的一部分时,最好使用
country\u code
。您还有其他API建议吗?您无法在ajax调用之外获取
位置
对象。您需要在
success
方法中写入最后的代码块