Skip to content Skip to sidebar Skip to footer

How To Access Google Maps Api In China

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

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

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


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

Solution 4:

The accepted answer stopped working from Feb 2020 as Google China stopped supporting the Chinese Google Maps API (https://maps.googleapis.cn/maps/api/js).

I have found a hack and it has been working fine for me.

Basically,

  1. Download https://google.cn/maps/api/js?key=YOUR_API_KEY as a Javascript file.
  2. Open it and replace maps.google.cn with google.cn. Host the modified Javascript on your website and use the API normally.
  3. THIS IS VERY IMPORTANT: after some time the Javascript will expire and stop working, so you have to start from step 1 again. I ended up automating the process in my Rails app.
  4. OPTIONAL BUT ALSO IMPORTANT: you may want to figure out a way to restrict the access to the modified Javascript. Otherwise someone may abuse it and it’s you who needs to pay the bill.

Detailed blog post with demo and screenshot: https://dingyu.me/blog/google-maps-api-china


Post a Comment for "How To Access Google Maps Api In China"