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

Gop*_*ath 4 javascript google-maps ibm-mobilefirst

我使用谷歌地图API在我的IBM Mobilefirst项目得到用户的位置,并不出所料做工精细,除了所有填报的国家知道,这是因为中国已经阻止访问谷歌在其country.Is有什么解决方法的API我可以使应用程序在中国工作.我已经在下面给出了代码片段以供参考.

这是我头脑中的剧本

 <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBaKx9iQrPQuOmAc2DkMRgjFdT0_XTdbmE&sensor=false&v=3&libraries=geometry"></script>
Run Code Online (Sandbox Code Playgroud)

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(); 
       }
Run Code Online (Sandbox Code Playgroud)

Ana*_*ngh 17

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

Google Maps API通过域maps.google.cn在中国境内投放.此域名不支持https.在向中国提出Google Maps API请求时,请使用http://maps.google.cn替换https://maps.googleapis.com.

例如:

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

参考:谷歌常见问题

更新
国家使用此:

$.getJSON("http://ip-api.com/json/", function (data) {
        var country = data.country_name;//your country
        alert(country);
        
    });
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Run Code Online (Sandbox Code Playgroud)

  • `$ .getJSON("http://freegeoip.net/json/",function(data){var country = data.country_name; alert(country); //检查并使用该代码.});` (2认同)