如何在中国访问 google 地图 api
How to access google maps api in China
我正在使用 google 地图 api 在我的 IBM Mobilefirst 项目中获取用户位置,它在所有国家/地区都按预期工作正常,除了 china.I 知道这是因为中国在他们的 country.Is 中阻止了对 google api 的访问,我可以采取任何解决方法,以便该应用程序即使在中国也能正常工作。我已经给出了下面的代码片段以供参考。
这是我头脑中的脚本
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBaKx9iQrPQuOmAc2DkMRgjFdT0_XTdbmE&sensor=false&v=3&libraries=geometry"></script>
javascript代码
function getLocation() {
busy = new WL.BusyIndicator ();
busy.show();
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition, showError,options);
} else {
alert( "Geolocation is not supported");}
}
function showPosition(pos) {
var geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(pos.coords.latitude,pos.coords.longitude);
latitude=pos.coords.latitude;
longitude=pos.coords.longitude;
geocoder.geocode({ 'latLng': latlng }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var addresscomponent=results[0].address_components;
var addresslength=addresscomponent.length;
for(var i=0;i<addresslength;i++){
if(addresscomponent[i].types[0]=='country'){
countryname=addresscomponent[i].short_name;
}
else if(addresscomponent[i].types[0]=='locality'){
cityname=addresscomponent[i].short_name;
}
}
}
function showError(error) {
alert(error);
busy.hide();
}
Why can't I access Google Maps APIs from China?
Google Maps API 在中国境内由域 maps.google.cn 提供。此域不支持 https。从中国向 Google Maps API 发出请求时,请替换 https://maps.googleapis.com with http://maps.google.cn.
例如:
会变成:
http://maps.google.cn/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA
参考:Google FAQ
更新
对于国家使用这个:
$.getJSON("http://ip-api.com/json/", function (data) {
var country = data.country_name;//your country
alert(country);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
这个怎么样?
if((pos.coords.latitude>=37 && pos.coords.latitude<=47)&& (pos.coords.latitude>=110 && pos.coords.latitude<=123))
{
//china
var googleGeocoding="http://maps.google.cn/maps/api/geocode/json?language=en&latlng=" + pos.coords.latitude + "," + pos.coords.longitude + "&key=" + Google_API_Key;
}else {
var googleGeocoding="https://maps.googleapis.com/maps/api/geocode/json?language=en&latlng=" + pos.coords.latitude + "," + pos.coords.longitude + "&key=" + Google_API_Key;
}
如你所见...中国是大国...但经纬度大约是37~47 , 110~123... http://www.latlong.net
它不完全检查中国与否,但是...如果您不是中国...您可以访问“http://maps.google.cn/”
所以没问题...
没有呼叫位置 API 或其他外部服务:
<script src="https://maps.googleapis.com/maps/api/js?key=[key]"></script>
<script>
//Fallback pour google Map api
window.google || document.write('<script src="http://maps.google.cn/maps/api/js?key=[key]">\x3C/script>');
</script>
接受的答案从 2020 年 2 月起停止工作,因为 Google 中国停止支持中文 Google 地图 API(https://maps.googleapis.cn/maps/api/js)。
我找到了一个 hack,它对我来说一直很好用。
基本上,
- 将 https://google.cn/maps/api/js?key=YOUR_API_KEY 下载为 Javascript 文件。
- 打开它并将
maps.google.cn
替换为 google.cn
。
在您的网站上托管修改后的 Javascript 并正常使用 API。
- 这非常重要:一段时间后 Javascript 将过期并停止工作,因此您必须重新从第 1 步开始。我最终在我的 Rails 应用程序中实现了流程自动化。
- 可选但也很重要:您可能想找出一种方法来限制对修改后 Javascript 的访问。否则有人可能会滥用它,需要买单的是你。
详细博客 post 带有演示和屏幕截图:https://dingyu.me/blog/google-maps-api-china
我正在使用 google 地图 api 在我的 IBM Mobilefirst 项目中获取用户位置,它在所有国家/地区都按预期工作正常,除了 china.I 知道这是因为中国在他们的 country.Is 中阻止了对 google api 的访问,我可以采取任何解决方法,以便该应用程序即使在中国也能正常工作。我已经给出了下面的代码片段以供参考。
这是我头脑中的脚本
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBaKx9iQrPQuOmAc2DkMRgjFdT0_XTdbmE&sensor=false&v=3&libraries=geometry"></script>
javascript代码
function getLocation() {
busy = new WL.BusyIndicator ();
busy.show();
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition, showError,options);
} else {
alert( "Geolocation is not supported");}
}
function showPosition(pos) {
var geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(pos.coords.latitude,pos.coords.longitude);
latitude=pos.coords.latitude;
longitude=pos.coords.longitude;
geocoder.geocode({ 'latLng': latlng }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var addresscomponent=results[0].address_components;
var addresslength=addresscomponent.length;
for(var i=0;i<addresslength;i++){
if(addresscomponent[i].types[0]=='country'){
countryname=addresscomponent[i].short_name;
}
else if(addresscomponent[i].types[0]=='locality'){
cityname=addresscomponent[i].short_name;
}
}
}
function showError(error) {
alert(error);
busy.hide();
}
Why can't I access Google Maps APIs from China?
Google Maps API 在中国境内由域 maps.google.cn 提供。此域不支持 https。从中国向 Google Maps API 发出请求时,请替换 https://maps.googleapis.com with http://maps.google.cn.
例如:
会变成:
http://maps.google.cn/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA
参考:Google FAQ
更新
对于国家使用这个:
$.getJSON("http://ip-api.com/json/", function (data) {
var country = data.country_name;//your country
alert(country);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
这个怎么样?
if((pos.coords.latitude>=37 && pos.coords.latitude<=47)&& (pos.coords.latitude>=110 && pos.coords.latitude<=123))
{
//china
var googleGeocoding="http://maps.google.cn/maps/api/geocode/json?language=en&latlng=" + pos.coords.latitude + "," + pos.coords.longitude + "&key=" + Google_API_Key;
}else {
var googleGeocoding="https://maps.googleapis.com/maps/api/geocode/json?language=en&latlng=" + pos.coords.latitude + "," + pos.coords.longitude + "&key=" + Google_API_Key;
}
如你所见...中国是大国...但经纬度大约是37~47 , 110~123... http://www.latlong.net
它不完全检查中国与否,但是...如果您不是中国...您可以访问“http://maps.google.cn/”
所以没问题...
没有呼叫位置 API 或其他外部服务:
<script src="https://maps.googleapis.com/maps/api/js?key=[key]"></script>
<script>
//Fallback pour google Map api
window.google || document.write('<script src="http://maps.google.cn/maps/api/js?key=[key]">\x3C/script>');
</script>
接受的答案从 2020 年 2 月起停止工作,因为 Google 中国停止支持中文 Google 地图 API(https://maps.googleapis.cn/maps/api/js)。
我找到了一个 hack,它对我来说一直很好用。
基本上,
- 将 https://google.cn/maps/api/js?key=YOUR_API_KEY 下载为 Javascript 文件。
- 打开它并将
maps.google.cn
替换为google.cn
。 在您的网站上托管修改后的 Javascript 并正常使用 API。 - 这非常重要:一段时间后 Javascript 将过期并停止工作,因此您必须重新从第 1 步开始。我最终在我的 Rails 应用程序中实现了流程自动化。
- 可选但也很重要:您可能想找出一种方法来限制对修改后 Javascript 的访问。否则有人可能会滥用它,需要买单的是你。
详细博客 post 带有演示和屏幕截图:https://dingyu.me/blog/google-maps-api-china