Javascript 如何在中国访问谷歌地图api

Javascript 如何在中国访问谷歌地图api,javascript,google-maps,ibm-mobilefirst,Javascript,Google Maps,Ibm Mobilefirst,在我的IBM Mobilefirst项目中,我正在使用google maps api获取用户位置,它在除中国以外的所有国家都像预期的那样工作良好。我知道这是因为中国已经阻止了他们国家对google api的访问。我能做些什么解决方法,使该应用程序即使在中国也能正常工作。我已经给出了下面的代码片段以供参考 这是我脑子里的剧本 <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=AIz

在我的IBM Mobilefirst项目中,我正在使用google maps api获取用户位置,它在除中国以外的所有国家都像预期的那样工作良好。我知道这是因为中国已经阻止了他们国家对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(); 
       }
函数getLocation(){ busy=新的WL.BusyIndicator(); 忙。显示(); if(导航器.地理位置){ navigator.geolocation.getCurrentPosition(显示位置、淋浴ROR、选项); }否则{ 警报(“不支持地理位置”);} } 功能显示位置(pos){ var geocoder=new google.maps.geocoder(); var latlng=新的google.maps.latlng(位置坐标纬度,位置坐标经度); 纬度=位置坐标纬度; 经度=位置坐标经度; geocoder.geocode({'latLng':latLng},函数(结果,状态){ if(status==google.maps.GeocoderStatus.OK){ var addresscomponent=results[0]。地址\组件; var addresslength=addresscomponent.length; 对于(var i=0;i 为什么我不能从中国访问谷歌地图API

Google Maps API由Maps.Google.cn域在中国境内提供。该域不支持https。当从中国向Google Maps API发出请求时,请替换为

例如:

将成为:

参考:

更新 对于国家/地区使用:

$.getJSON(“http://ip-api.com/json/,函数(数据){
var country=data.country\u name;//您的国家
警报(国家);
});
这个怎么样


如果((pos.coords.latitude>=37&&pos.coords.latitude=110&&pos.coords.latitude不调用位置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>

//谷歌地图api的回退
window.google | | document.write('\x3C/script>');

被接受的答案从2020年2月起停止工作,因为谷歌中国停止支持中文谷歌地图API()

我发现了一个黑客,它一直在为我工作良好

基本上

  • 作为Javascript文件下载
  • 打开它并将
    maps.google.cn
    替换为
    google.cn
    。 在您的网站上托管修改后的Javascript,并正常使用API
  • 这一点非常重要:一段时间后,Javascript将过期并停止工作,因此您必须再次从步骤1开始。我最终在我的Rails应用程序中实现了该过程的自动化
  • 可选但也很重要:您可能想找到一种方法来限制对修改后的Javascript的访问。否则,有人可能会滥用它,而您需要支付账单

  • 带有演示和屏幕截图的详细博客帖子:

    但在获取位置之前,我如何知道用户是来自中国还是世界其他地区?
    $.getJSON(“http://freegeoip.net/json/,函数(数据){var country=data.country_name;alert(country);//检查此项并使用该代码。})
    谢谢。让我试试这个。当然,我正在处理它,它会对状态进行回复。谢谢你的及时回复。我想如果我能够从这项服务中获取国家信息,我就不需要点击谷歌api。只有印度需要获取其他国家的城市信息,我只需要国家名称和国家代码。不幸的是,不需要URL不起作用:Google\u API\u键------URL起作用Google\u API\u键