まらんさんのチラ裏

その日暮らしのおじさん

Google Map APIって便利だね

正しくはGeocodingって便利だね。

<!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"/>
    <title>Google Maps JavaScript API Example</title>
    <script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
      type="text/javascript"></script>
    <script type="text/javascript">

    //<![CDATA[

    var gMap;
    var gGeo;

    function load() {
      if (GBrowserIsCompatible()) {
        gMap = new GMap2(document.getElementById("map"));
        gGeo = new GClientGeocoder();
        gGeo.getLatLng("東京都港区芝公園4-2-8", onGeocoding);
        gMap.addControl(new GLargeMapControl());
        gMap.addControl(new GMapTypeControl());
        gMap.addControl(new GScaleControl());
      }
    }

    function onGeocoding(result){
      //取得した位置に移動
      gMap.setCenter(result,13,G_NORMAL_MAP);

      //取得した位置にマーカー作成
      setMarker(result);
    }

    function setMarker(result){
      //マーカーを作成
      var marker = new GMarker(result);
      gMap.addOverlay(marker);
    }

    //]]>
    </script>
  </head>
  <body onload="load()" onunload="GUnload()">
    <div id="map" style="width: 500px; height: 300px"></div>
  </body>

Google Map APIのキーを取得したときに表示されるサンプルに、
ぐぐって出てきたページのGeocodingサンプルをパクって適当に切り貼りしてたらできちゃったよ!
Geocodingの初期値はロマンチックでカップルが大好きな芝公園だよ!
死ねばいいのに。


参考サイト:Google Maps API(GClientGeocoder)でジオコーディングするサンプル