2010年12月28日火曜日

formatted_addressの取得サンプル

<script type="text/javascript"><!--
google_ad_client = "ca-pub-7939613401472758";
/* getAddressByLatLng top */
google_ad_slot = "2186761428";
google_ad_width = 678;
google_ad_height = 15;
//--></script></div>
<div id="map" style="width: 680px; height: 340px;">&nbsp;</div>
<script type='text/javascript'><!--
    var map = new google.maps.Map(document.getElementById("map"), {
        zoom: 12,
        center: new google.maps.LatLng(35.712036115726725, 139.60383224487302),
        mapTypeId: google.maps.MapTypeId.ROADMAP
    });
    google.maps.event.addListener(map, 'click', function(mouseEvent) {
        getAddress(mouseEvent.latLng);
    });

    function getAddress(latlng) {
        var geocoder = new google.maps.Geocoder();
        geocoder.geocode({
            latLng: latlng
        }, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                if (results[0].geometry) {
                    var address = results[0].formatted_address.replace(/^日本, /, '');
                    address = replace(/^日本, /, '');
                    new google.maps.InfoWindow({
                        content: address + "<br>(Lat, Lng) = " + latlng
                    }).open(map, new google.maps.Marker({
                        position: latlng,
                        map: map
                    }));
                }
            } else if (status == google.maps.GeocoderStatus.ERROR) {
                alert("サーバとの通信時に何らかのエラーが発生!");
            } else if (status == google.maps.GeocoderStatus.INVALID_REQUEST) {
                alert("リクエストに問題アリ!geocode()に渡すGeocoderRequestを確認せよ!!");
            } else if (status == google.maps.GeocoderStatus.OVER_QUERY_LIMIT) {
                alert("短時間にクエリを送りすぎ!落ち着いて!!");
            } else if (status == google.maps.GeocoderStatus.REQUEST_DENIED) {
                alert("このページではジオコーダの利用が許可されていない!・・・なぜ!?");
            } else if (status == google.maps.GeocoderStatus.UNKNOWN_ERROR) {
                alert("サーバ側でなんらかのトラブルが発生した模様。再挑戦されたし。");
            } else if (status == google.maps.GeocoderStatus.ZERO_RESULTS) {
                alert("見つかりません");
            } else {
                alert("えぇ~っと・・、バージョンアップ?");
            }
        });
    }
//--></script>

0 件のコメント:

コメントを投稿