2019年10月14日 星期一

google api 在中國無法使用的問題

1.
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.
所以要改用 http://maps.google.cn

<script src="https://maps.googleapis.com/maps/api/js?key=[key]"></script>
<script>
    window.google || document.write('<script src="http://maps.google.cn/maps/api/js?key=[key]">\x3C/script>');
</script>

2.偵測ip&國家
$.getJSON("http://ip-api.com/json/", function (data) {
        var country = data.country_name;
        alert(country);        
    });
$.getJSON("http://freegeoip.net/json/", function (data) { 
     var country = data.country_name; 
     alert(country);
});