スポンサードリンク

Apple iPhone(アイフォン)で正確な地理的座標位置を取得するメソッドの比較。/Googel Maps API v3 Geolocation 

◆ navigator.geolocation.getCurrentPosition() と navigator.geolocation.watchPosition()メソッド

iPhone(アイフォン)の座標収得で使用するnavigator.geolocation.getCurrentPosition()、と navigator.geolocation.watchPosition()メソッドの大きな違いは、都度、現在位置をユーザが読み込んで位置情報を収得するのか、それとも連続的に収得するだけだと思っていたが、正確な座標制度を得るには、navigator.geolocation.watchPosition()を使った方が良いとの記事を見つけた。 <記事URL>

How to get an accurate Geo location from Apple IPhone using navigator.geolocation.watchPosition

◆記事要約

かなりはしょっています。また間違っていたらゴメンナサイ。原文を見て下さいね。

navigator.geolocation.watchPosition()は、IPhoneで位置を収得する際に、GSM (Global System for Mobile Communications;つまりケイタイ基地?)により徐々に精度をあげていく。 navigator.geolocation.getCurrentPosition()は、私の実験中、大抵は常にGSM接続の境界となるGSM基地局タワーの初期座標を読み取るため、ほとんどの状況においてほとんど役立たない。 navigator.geolocation.getCurrentPosition()より、むしろnavigator.geolocation.watchPosition()を使用して、IPhoneでは正確な座標を取得してください。

iPhone(アイフォン)で収得する座標があまり正確でないと思ったら、以上の記事が参考になるかもしれませんのでメモ。

◆サンプル地図-iPhone(アイフォン)用

Google Maps API v3の基本ページにあるサンプルをちょういと書き換え試してみた。 iPhone(アイフォン)で下記にアクセスしたら、確かにwatchPositionのサンプルの方が正確な位置を表示することが多い。 なお、下記のサンプルにおいて、watchPositionを指定した地図は、5~8秒毎に現在位置を収得する(地図を動かしても現在地へ戻る)。getCurrentPositionでは、最初に地図を読み込んだ時だけ、現在位置になり、ブラウザを再読み込みするまで、現在位置を収得しないようだ。 <サンプル>

// Try W3C Geolocation method (Preferred)
if(navigator.geolocation) {
browserSupportFlag = true;
navigator.geolocation.getCurrentPosition(function(position) {
initialLocation = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
contentString = “Location found using W3C standard”;
map.setCenter(initialLocation);
infowindow.setContent(contentString);
infowindow.setPosition(initialLocation);
infowindow.open(map);
}, function() {
handleNoGeolocation(browserSupportFlag);
});

// Try W3C Geolocation method (Preferred)

if(navigator.geolocation) {
browserSupportFlag = true;
navigator.geolocation.watchPosition(function(position) {
initialLocation = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
contentString = “Location found using W3C standard”;
map.setCenter(initialLocation);
infowindow.setContent(contentString);
infowindow.setPosition(initialLocation);
infowindow.open(map);
}, function() {
handleNoGeolocation(browserSupportFlag);

追記

ためしに、iPhone(アイフォン)を持ち歩いて、いろいろ試したが、getCurrentPosition()で、正確な座標を得るのはダメでした。正確な位置を収得する場合もあるのですが、watchPositionの方が断然正確。 なお、watchPositionでは、最初に収得した座標でほぼ正確ですが、その後も座標収得を継続するので、地図を見ていると現在位置へ勝手にマップが移動してしまいます。

この対策としては、watchPositionとgetCurrentPositionを切り替えればいいでしょう。デモソースはコチラの記事を参考にされてください。

Googel Maps API JavaScript API V3

スポンサードリンク

Related Posts

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <img localsrc="" alt="">