iPhoneのGPSを使用して現在位置の住所を表示する方法
iPhoneのGPSを使用して現在の住所を表示する方法の紹介です。
結構簡単にできたので紹介です。
こんな感じでっす。
iPhoneな方はこちらからアクセスできます。
http://moeten.info/maidcafe/?m=iphone&type=gps2
#ただ、ネイティブアプリに比べるとかなり重いです汗
今回、緯度経度から住所に変換するということでグーグルマップAPIの逆ジオコーディングを使用します。
使い方は簡単でして
geocoder.getLocations( latlng , showAddress );
こんな感じで緯度経度情報を渡してコールバック関数を後ろに指定するだけです。
ソースコードはこちら
<!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" xmlns:v="urn:schemas-microsoft-com:vml"> <head> <meta name="viewport" content="width=320;initial-scale=1.0;maximum-scale=1.0;user-scalable=no"> <script src="http://maps.google.co.jp/maps?hl=ja&file=api&v=2&key=APIKEY"></script> <script type="text/javascript"> var map; var geocoder; function initialize() { if (GBrowserIsCompatible()) { navigator.geolocation.getCurrentPosition( callback, handleError ); } } function handleError(a) { var d = document.getElementById("d"); d.innerHTML = log = log + "<p> error: " + a.code + "</p>"; } function callback(a){ map = new GMap2(document.getElementById("map_canvas")); map.setCenter(new GLatLng( a.coords.latitude, a.coords.longitude ), 18); map.addControl(new GSmallMapControl()); map.addControl(new GMapTypeControl()); geocoder = new GClientGeocoder(); getAddress( a ); } function getAddress( a ) { if ( a != null) { geocoder.getLocations( new GLatLng( a.coords.latitude, a.coords.longitude ) , showAddress ); } } function showAddress(response) { map.clearOverlays(); if (!response || response.Status.code != 200) { alert("Status Code:" + response.Status.code); } else { place = response.Placemark[0]; point = new GLatLng(place.Point.coordinates[1],place.Point.coordinates[0]); marker = new GMarker(point); map.addOverlay(marker); marker.openInfoWindowHtml( '<b>orig latlng:</b>' + response.name + '<br/>' + '<b>latlng:</b>' + place.Point.coordinates[0] + "," + place.Point.coordinates[1] + '<br>' + '<b>Status Code:</b>' + response.Status.code + '<br>' + '<b>Status Request:</b>' + response.Status.request + '<br>' + '<b>Address:</b>' + place.address + '<br>' + '<b>Accuracy:</b>' + place.AddressDetails.Accuracy + '<br>' + '<b>Country code:</b> ' + place.AddressDetails.Country.CountryNameCode); } } </script> <meta http-equiv="content-type" content="text/html; charset=utf-8"/> </head> <body onload="initialize()" onunload="GUnload()"> <div id="map_canvas" style="width: 320px; height: 320px"></div> </body> </html>
参考リンク