Google Maps APIのメーリングリストで見つけた質問に英語で回答にチャレンジ

工業高校出身ということで、英語は得意ではありませんが、Google Maps APIメーリングリスト?に回答できそうな質問があったので、頑張って英語で質問に答えてみました。

この質問の本質は以下の通りだと私は解釈しました。間違っていたらすみません。

ある2つの異なるURLを持つサイトで、Google Maps APIを使いたい時、それぞれのサイト用にGoogle Maps API keyを発行する必要があります。そこで、keyの扱いが問題になります。というのもkeyをGoogle Mapを利用するJavaScriptにハードコードしてしまうと、そのJavaScriptを他のURLの異なるサイトに配置した時に、そのサイト用にkeyだけを再度書き直さなければならないからです。localhostで開発して、グローバルにアクセスできるドメインにアップしなければならないとしたら(ほとんどがそうだと思います)、とてもじゃありませんが開発を円滑にできません。

そこで、以下のような返答を書き込みました。ちゃんと通じるかちょっと心配。

Hello.
I use the following script to solve the problem you faced.
I hope that your problem can be solved by the script.

*** Script A: gmaps_util.js ***
function include_gmaps_api()
{
	var host, key;
	
	host = location.host;
	path = location.pathname;
	
	// Do you know that Google Maps API can be used in 'localhost' context?
	// It is very useful for every developer. Try it.
	if(host == 'localhost') {
		if (path.match(/\/my_project\/trunk\/foo/)) {
			// Google Maps API key for http://localhost/my_project/trunk/foo/
			key = 'xxxxxxxx';
		}
	} else if (host == 'www.example.com') {
		if (path.match(/\/my_project\/trunk\/foo/)) {
			// Google Maps API key for http://www.example.com/my_project/trunk/foo/
			key = 'yyyyyyyy';
		}
	}
	
	// Include Google Maps API from maps.google.com
	document.write('<scr' + 'ipt src="http://maps.google.com/maps?file=api&v=1&key=' + 
					key + '" type="text/javascript"></scr' + 'ipt>');
					
}

// Entry point.
include_gmaps_api();



*** Script B: the map page ***
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<head>
	<!-- Include the gmaps_util.js -->
	<script src="lib/js/gmaps_util.js" type="text/javascript"></script>
[...]
// Now you can write any Google Maps API code.
// So, you don't have to rewrite the Google Maps API key for different sites.
// gmaps_util.js automatically manages the keys for you.

追記 10/06 06:30

あれから少し議論が続きました。私の説明不足でBrianさんに誤解を与えてしまったみたいなので、もう少し詳しく説明してみると、結局以下のようにBrainさんに納得してもらえました。ひと安心。幼稚な英語ですが、なんとか英語で議論できたので嬉しかったです。効果的かつ効率良くコミュニケーションするのって、やっぱり難しいなぁ。でも、高校時代のように、全く英語が書けなかった頃の自分と比べると、ちょっとは上達したのかな? これからもちょっとずつライティングの技術を磨いていきたいです。

ahhh...yes that code makes perfect sense..I was just to tired last
night to think..

thanks for the code explanation..it helped me see what you were
saying...

I'm sure by the time I get this working the way I want, google will
have fixed the problem.....

thanks,
Brian

Oct 5, 11:03 pm Brianさんより

追記 10/08 22:40

「Google Maps API keyの仕様が変更され、鍵管理の負担が大幅削減」のエントリをうけて、下記の内容を一応返信しておきました。もしかして、このスレッドで議論したことがGoogleに伝わったのでしょうか?もしもそうだとしたら嬉しい限りです。

Did you see the thread "Clearing API Key", which was posted on the
Google-Maps-API Groups? The thread's URL is
http://groups.google.com/group/Google-Maps-API/browse_thread/thread/6...
.
If you haven't checked it out, please see it.

Fortunately, the problem we discussed here is now solved by Google. So,
we don't have to rely on my quick hack code anymore. Thanks Google!