{"id":67,"date":"2017-04-26T14:05:23","date_gmt":"2017-04-26T05:05:23","guid":{"rendered":"https:\/\/www.studio-jt.co.kr\/google-map-api-not-work-in-china\/"},"modified":"2017-04-26T14:05:23","modified_gmt":"2017-04-26T05:05:23","slug":"google-map-api-not-work-in-china","status":"publish","type":"post","link":"https:\/\/www.studio-jt.co.kr\/en\/google-map-api-not-work-in-china\/","title":{"rendered":"Google maps api doesn&#8217;t work in China &#8211; Problem and solution"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-689904\" src=\"https:\/\/www.studio-jt.co.kr\/wp-content\/uploads\/2017\/04\/jt-blog-googleapi-eng2.jpg\" alt=\"\" width=\"850\" height=\"290\"><\/p>\n<h2>THE&nbsp;PROBLEM<\/h2>\n<p>The reason is not clear but the fact is the domain maps.google.com we commonly use to display google maps is block within china, but don&#8217;t worry google is aware about that and provide an alternative solution.<\/p>\n<h2>THE SOLUTION<\/h2>\n<p>The tricks is you have to replace the default API domain google.com to the google.cn , this domain use a google server base in china and it is not https.<\/p>\n<p>So for example :<br \/>\n<strong><code>https:\/\/maps.google.com\/maps\/api\/js?region=KR<\/code><\/strong><\/p>\n<p>will become<br \/>\n<strong><code><span style=\"color: #ff0000;\">http<\/span>:\/\/maps.google<span style=\"color: #ff0000;\">.cn<\/span>\/maps\/api\/js?region=KR<\/code><\/strong><\/p>\n<p>don\u2019t forget the region parameter set to KR to display Dokdo name and East Sea the right way.<\/p>\n<p>if you visit the demo outside china the both maps will appear, but if you check the demo using a browser base in china only the right one will be display (see below how to test your page remotely)<\/p>\n<h2>IMPROVE THE CODE<\/h2>\n<p>Because the google map Chinese server will be slow outside the great wall, you maybe want to optimize your code so only users based in china will use google.cn.<br \/>\nSo you can use PHP library like <a href=\"https:\/\/github.com\/maxmind\/GeoIP2-php\" target=\"_blank\" rel=\"noopener noreferrer\">maxmind geoip php api<\/a> and use the right server depending of the user IP.<br \/>\nIf you can not use geoip library, an alternative solution will be to check the user browser language to display Chinese google map API&nbsp;for Chinese speaker only.<br \/>\nForget use the html5 geolocation api since it requiered the user permission.<\/p>\n<h3>A PHP example :<\/h3>\n<p>[php]<br \/>\n$lang = substr($_SERVER[&#8216;HTTP_ACCEPT_LANGUAGE&#8217;], 0, 2);<\/p>\n<p>if($lang === &#8216;zh&#8217;){<br \/>\n$google_map_url = &#8216;http:\/\/maps.google.cn\/maps\/api\/js?region=KR&#8217;;<br \/>\n}else{<br \/>\n$google_map_url = &#8216;https:\/\/maps.google.com\/maps\/api\/js?region=KR&#8217;;<br \/>\n}<br \/>\necho &#8216;&amp;amp;lt;script src=&quot;&#8217;.$google_map_url.&#8217;&quot;&amp;amp;gt;&amp;amp;lt;\/script&amp;amp;gt;&#8217;;<br \/>\n[\/php]<\/p>\n<h3>A Javascript example :<\/h3>\n<p>In Javascript you have to load the script asynchronously<br \/>\nHere a jQuery example using <a href=\"https:\/\/api.jquery.com\/jquery.getscript\/\" target=\"_blank\" rel=\"noopener noreferrer\">$.getScript<\/a><\/p>\n<p>[js]<br \/>\nvar google_map_url = &#8221;;<br \/>\nvar lang = navigator.language || navigator.userLanguage;<\/p>\n<p>if(lang === &#8216;zh&#8217;){<br \/>\ngoogle_map_url = &#8216;http:\/\/maps.google.cn\/maps\/api\/js?region=KR&#8217;;<br \/>\n}else{<br \/>\ngoogle_map_url = &#8216;https:\/\/maps.google.com\/maps\/api\/js?region=KR&#8217;;<br \/>\n}<\/p>\n<p>$.getScript( google_map_url ).done(function( script, textStatus ) {<br \/>\n\/\/ Do your google map stuff<br \/>\n}<br \/>\n[\/js]<\/p>\n<h2>TEST IF IT WORKS<\/h2>\n<p>Finally to check if every work as except you need to connect to your site via&nbsp;a computer base in China.<del> The simplest way is to use a service like <a href=\"https:\/\/www.webpagetest.org\" target=\"_blank\" rel=\"noopener noreferrer\">webpagetest.org<\/a> which will give you a screenshot of your page from a comptuer base in china:<\/del><\/p>\n<p><del>Go to <a href=\"https:\/\/www.webpagetest.org\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/www.webpagetest.org<\/a> , select &#8220;Yangzhou, China (Chrome,Firefox)&#8221; &nbsp;and run a test<\/del><\/p>\n<p><del>After the test finish you will get a result page, so click on &#8220;screenshot&#8221; and you will see what a user based in china see (Note that it can take some time depending how many people waiting on the queue)<\/del><\/p>\n<p><strong>Update :<\/strong> As Massimiliano notice in the comments&nbsp; webpagetest.org do not provide chinese loaction anymore,&nbsp; we found an alternative call&nbsp;<a href=\"https:\/\/www.geoscreenshot.com\/country\/china\" target=\"_blank\" rel=\"noopener noreferrer\">geoscreenshot.com\/country\/china<\/a>&nbsp; with this service you can get a screenshot of your site from Hangzhou, China for free.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-691297\" src=\"https:\/\/www.studio-jt.co.kr\/wp-content\/uploads\/2017\/04\/geoscreenshot-com-jt-china-google-map.png\" alt=\"\" width=\"1024\" height=\"760\"><\/p>\n<p>&nbsp;<\/p>\n<h2>REFERENCE LINK<\/h2>\n<p>Google offical FAQ :&nbsp;<a href=\"https:\/\/developers.google.com\/maps\/faq#china_ws_access\">https:\/\/developers.google.com\/maps\/faq#china_ws_access<\/a><\/p>\n<p>&nbsp;<\/p>\n<h2>ADDITIONAL NOTE<\/h2>\n<p>In the demo and code example we intentionally omit to add the google api key, but in production it strongly recommend to add yours in order to avoid any problem<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Did you know that google map API do not work in china by default.<br \/>\nAt Studio-JT we had to deal with this problem in a recent project.<br \/>\nHere is how we solved this issue.<\/p>\n","protected":false},"author":1,"featured_media":138,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[13],"class_list":["post-67","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-dev"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.studio-jt.co.kr\/en\/wp-json\/wp\/v2\/posts\/67","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.studio-jt.co.kr\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.studio-jt.co.kr\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.studio-jt.co.kr\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.studio-jt.co.kr\/en\/wp-json\/wp\/v2\/comments?post=67"}],"version-history":[{"count":0,"href":"https:\/\/www.studio-jt.co.kr\/en\/wp-json\/wp\/v2\/posts\/67\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.studio-jt.co.kr\/en\/wp-json\/wp\/v2\/media\/138"}],"wp:attachment":[{"href":"https:\/\/www.studio-jt.co.kr\/en\/wp-json\/wp\/v2\/media?parent=67"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.studio-jt.co.kr\/en\/wp-json\/wp\/v2\/categories?post=67"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}