How to access google maps api in China

烈酒焚心 提交于 2019-11-27 03:06:15

问题


I am using google maps api to get user location in my IBM Mobilefirst project and it is working fine as expected in all the countries except china.I know this is because china has blocked access to google apis in their country.Is there any workaround that I can make so that the application works even in china. I have given the code snippet below for reference.

This is the script I have in head

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

javascript code

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

回答1:


Why can't I access Google Maps APIs from China?

The Google Maps APIs are served within China from the domain maps.google.cn. This domain does not support https. When making requests to the Google Maps APIs from China, please replace https://maps.googleapis.com with http://maps.google.cn.

For example:

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

would become:

http://maps.google.cn/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA

reference :Google FAQ

Update
For country use this:

$.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>



回答2:


How about this?

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

As you can see...china is big country... but approximately lat lon is 37~47 , 110~123... http://www.latlong.net

It is not exactly check china or not but...if you are not china...you can access "http://maps.google.cn/"

So It is no problem...




回答3:


Whithout call position API or other external services:

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


来源:https://stackoverflow.com/questions/33802180/how-to-access-google-maps-api-in-china

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!