ブラウザから位置情報を取得する
Geolocation APIを利用して取得。実装していないブラウザの為にGoogle Gearsを使用しても取得できる。
■Gears
使う準備をする。以下を書くだけで良い。
<script type="text/javascript" src="http://code.google.com/intl/ja/apis/gears/gears_init.js"></script>
■ソースコード
if(navigator.geolocation) {
alert('geolocation');
navigator.geolocation.getCurrentPosition(
function(position){
alert(position.coords.latitude + ',' + position.coords.longitude)
},
function(error){
switch(error.code){
case error.PERMISSION_DENIED :
alert('Forbidden to use the Geolocation API.');
break;
case error.POSITION_UNAVAILABLE :
alert('Location providers reported an internal error.');
break;
case error.TIMEOUT :
alert('Timeout.');
break;
case error.UNKNOWN_ERROR :
default :
alert('Failed due to an unknown error.');
break;
}
}
);
}
else if(typeof google != 'undefined' && typeof google.gears != 'undefined'){
alert('gears');
google.gears.factory.create('beta.geolocation').getCurrentPosition(
function(position){
alert(position.coords.latitude + ',' + position.coords.longitude)
},
function(error){
alert('error : ' + error.message);
}
);
}
else{
alert('sorry');
}
Chrome LiteはGearsを搭載しているのでandroidでも位置情報が取得できる。
TrackBack URL :
Comments (0)