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

问题描述 投票:4回答:3

我正在使用谷歌地图api来获取我的IBM Mobilefirst项目中的用户位置,并且它在除了china之外的所有国家都正常运行。我知道这是因为中国已经阻止了在他们国家访问谷歌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(); 
       }
javascript google-maps ibm-mobilefirst
3个回答
17
投票

为什么我无法从中国访问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);
        
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

1
投票

这个怎么样?

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/

所以没问题......


0
投票

没有呼叫位置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>
© www.soinside.com 2019 - 2024. All rights reserved.