【问题标题】:How to access google maps api in China如何在中国访问谷歌地图api
【发布时间】:2016-02-21 11:52:06
【问题描述】:

我正在使用 google maps api 在我的 IBM Mobilefirst 项目中获取用户位置,它在除中国以外的所有国家/地区都按预期工作。我知道这是因为中国已阻止访问他们国家/地区的 google api。有吗我可以采取的任何解决方法,以便该应用程序即使在中国也能正常工作。下面我给出了sn-p的代码供参考。

这是我脑子里的脚本

 <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(); 
       }

【问题讨论】:

    标签: javascript google-maps ibm-mobilefirst


    【解决方案1】:

    为什么我无法从中国访问 Google Maps API?

    Google Maps API 在中国境内通过 maps.google.cn 域提供。此域不支持 https。从中国向 Google Maps API 发出请求时,请将https://maps.googleapis.com 替换为http://maps.google.cn

    例如:

    https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA

    会变成:

    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);
            
        });
    &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"&gt;&lt;/script&gt;

    【讨论】:

    • 但是在获取位置之前我怎么知道用户是来自中国还是来自世界其他地方?
    • $.getJSON("http://freegeoip.net/json/", function (data) { var country = data.country_name; alert(country);//check this and use that code. });
    • 谢谢。让我试试这个。
    • 确定我正在处理它会评论状态..感谢您的及时回复。我想如果我能够从这项服务中获取国家/地区,我就不需要点击 google api。仅适用于印度我需要获取其他国家的城市我只需要国家名称和国家代码
    • 不幸的是,我也无法使用 URL 无法使用:maps.google.cn/maps/api/place/autocomplete/… Google_API_Key --------URL 可以使用maps.googleapis.com/maps/api/place/autocomplete/… Google_API_Key
    【解决方案2】:

    这个怎么样?

    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;
    }

    如您所见...中国是一个大国...但大约 lat lon 是 37~47 , 110~123...http://www.latlong.net

    不完全检查中国,但...如果您不是中国...您可以访问“http://maps.google.cn/

    所以没问题……

    【讨论】:

      【解决方案3】:

      无需调用位置 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>
      

      【讨论】:

        【解决方案4】:

        由于谷歌中国停止支持中文谷歌地图 API (https://maps.googleapis.cn/maps/api/js),已接受的答案从 2020 年 2 月起停止工作。

        我发现了一个 hack,它对我来说运行良好。

        基本上,

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

        带有演示和截图的详细博文:https://dingyu.me/blog/google-maps-api-china

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-04-25
          • 1970-01-01
          • 2017-12-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多