ReverseGeocodingTest yd_niku Follow 2010-09-02 17:27:35 License: MIT License Fork3 Fav0 View2000 Play Stop Reload Fullscreen Smart Phone Fork tree Readme JavaScript 91 lines HTML 4 lines CSS 2 lines ReverseGeocodingTest jQuery v1.4.2 /* 「新宿」とか「自由が丘」のような地名を取得するために 地図の中心逆ジオコーディングすると 地名リストがすべて郵便番号レベルでしか取れないケースがあります。 便番号のLocationで再度逆ジオコーディングするといいと書いてあるものもありますが どうやらすべてがうまくいくわけではないようです。 特に東京駅がうまくいかないんですが、なにか確実に地名を取る方法がありますでしょうか? */ // まずはGoogleMapsを生成 var latlng = new google.maps.LatLng(35.6816935,139.7690112); var myOptions = { zoom: 15, center: latlng, navigationControl:false, mapTypeControl:false, scaleControl:false, streetViewControl:false, scrollwheel:false, disableDoubleClickZoom:true, mapTypeId: google.maps.MapTypeId.ROADMAP }; map = new google.maps.Map(document.getElementById("map-container"),myOptions); // ボタン押すと逆ジオコーディング開始 $('#getReverseGeocoding').click(function(){ geocoding(); }); // 逆ジオコーディング処理本体 var geocoder = new google.maps.Geocoder(); function geocoding() { geocoder.geocode({ latLng:map.getCenter() }, function(results, status) { if (status == google.maps.GeocoderStatus.OK) { //取得成功した場合 var newAddress; var priorityMap = []; //結果の中からなるべく好ましいのを選ぶよ for(i=0; i<results.length; i++){ switch(results[i].types[0]){ case 'locality': priorityMap[0] = results[i]; break; case 'sublocality_level_1': priorityMap[1] = results[i]; break; case 'sublocality_level_2': priorityMap[2] = results[i]; break; case 'sublocality_level_3': priorityMap[3] = results[i]; break; case 'sublocality_level_4': priorityMap[4] = results[i]; break; case 'sublocality_level_4': priorityMap[4] = results[i]; break; } } //一番いいのを抽出 for( i=0; i< priorityMap.length; ++i ) { if( priorityMap[i] ) { newAddress = priorityMap[i].formatted_address; break; } } if(newAddress){ //有効な地名が含まれていた場合 $('#place').text(newAddress); } else{ //郵便番号しか含まれてなかった場合場合 alert("Reverse Geocoding is failed."); if(console)console.log(results); } } else { //取得失敗した場合 alert("Geocoder failed due to: " + status); } }); } <script src="http://maps.google.com/maps/api/js?sensor=true&language=ja"></script> <div id="map-container" style="width:400px;height:350px"></div> <p id="getReverseGeocoding">地名取得</p> <p id="place"></p> ReverseGeocodingTest body { background-color: #DDDDDD; font: 12px sans-serif; } #getPos, #getReverseGeocoding{ border:1px solid #333; background-color:#fff; width:120px; } /* 「新宿」とか「自由が丘」のような地名を取得するために 地図の中心逆ジオコーディングすると 地名リストがすべて郵便番号レベルでしか取れないケースがあります。 便番号のLocationで再度逆ジオコーディングするといいと書いてあるものもありますが どうやらすべてがうまくいくわけではないようです。 特に東京駅がうまくいかないんですが、なにか確実に地名を取る方法がありますでしょうか? */ // まずはGoogleMapsを生成 var latlng = new google.maps.LatLng(35.6816935,139.7690112); var myOptions = { zoom: 15, center: latlng, navigationControl:false, mapTypeControl:false, scaleControl:false, streetViewControl:false, scrollwheel:false, disableDoubleClickZoom:true, mapTypeId: google.maps.MapTypeId.ROADMAP }; map = new google.maps.Map(document.getElementById("map-container"),myOptions); // ボタン押すと逆ジオコーディング開始 $('#getReverseGeocoding').click(function(){ geocoding(); }); // 逆ジオコーディング処理本体 var geocoder = new google.maps.Geocoder(); function geocoding() { geocoder.geocode({ latLng:map.getCenter() }, function(results, status) { if (status == google.maps.GeocoderStatus.OK) { //取得成功した場合 var newAddress; var priorityMap = []; //結果の中からなるべく好ましいのを選ぶよ for(i=0; i<results.length; i++){ switch(results[i].types[0]){ case 'locality': priorityMap[0] = results[i]; break; case 'sublocality_level_1': priorityMap[1] = results[i]; break; case 'sublocality_level_2': priorityMap[2] = results[i]; break; case 'sublocality_level_3': priorityMap[3] = results[i]; break; case 'sublocality_level_4': priorityMap[4] = results[i]; break; case 'sublocality_level_4': priorityMap[4] = results[i]; break; } } //一番いいのを抽出 for( i=0; i< priorityMap.length; ++i ) { if( priorityMap[i] ) { newAddress = priorityMap[i].formatted_address; break; } } if(newAddress){ //有効な地名が含まれていた場合 $('#place').text(newAddress); } else{ //郵便番号しか含まれてなかった場合場合 alert("Reverse Geocoding is failed."); if(console)console.log(results); } } else { //取得失敗した場合 alert("Geocoder failed due to: " + status); } }); } <script src="http://maps.google.com/maps/api/js?sensor=true&language=ja"></script> <div id="map-container" style="width:400px;height:350px"></div> <p id="getReverseGeocoding">地名取得</p> <p id="place"></p> body { background-color: #DDDDDD; font: 12px sans-serif; } #getPos, #getReverseGeocoding{ border:1px solid #333; background-color:#fff; width:120px; } use an iframe compat browser, deer Tweet QR code Embed Design view Code view <script type="text/javascript" src="http://jsdo.it/blogparts/sLQV/js?view=design"></script><p class="ttlBpJsdoit" style="width: 465px; margin: 0; text-align: right; font-size: 11px;"><a href="http://jsdo.it/yd_niku/sLQV" title="ReverseGeocodingTest">ReverseGeocodingTest - jsdo.it - share JavaScript, HTML5 and CSS</a></p> zip tags question Tweet twitter Tags question Forked sort new page view favorite forked forked from: ReverseGeocodingT.. holydesign 00 414views 95/4/2 forked from: ReverseGeocodingT.. knokura 00 298views 92/4/2 use bounds args - ReverseGeoco.. mash 11 1086views 98/4/2 google-maps