如何在 Mobile Web App 中取得经纬度(转)
转载自ericsk ,地址如下:http://blog.ericsk.org/archives/1347/comment-page-1
以下为转载内容:
本篇文章提及的方法僅適用於 iPhone 3.0+ 及 Android-based 手機。
在寫 mobile web app 時,如果使用者是用 iPhone 3.0+ 或是 Android-based 手機來瀏覽網站時,其實還是有辦法取得手機的經緯度座標資料。以下先分別講述兩支手機各別的方法,然後再將兩個方法合成同一份 JavaScript 程式碼。
iPhone
iPhone 3.0 之後,iPhone Safari 直接支援 Geolocation API,所以你可以利用下面這段 JavaScript 程式碼來取得該 iPhone 的經緯度座標資料:
var getPosition = function(pos) { // pos.coords.latitude 及 pos.coords.longitude 即為座標資料};var errorCallback = function(error) { fallbackToIPGeoLocation();};var fallbackToIPGeoLocation = function() { if (google.loader.ClientLocation) { // google.loader.ClientLocation.latitude // google.loader.ClientLocation.longitude } else { // 投降了,真的無法定位 }};if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(getPosition, errorCallback, {enableHighAccuracy: true});} else { if (window.google && google.gears) { try { var geo = google.gears.factory.create('beta.location'); geo.getCurrentPosition(getPosition, errorCallback, {enableHighAccuracy: true}); } catch (e) { fallbackToIPGeoLocation(); } } else { fallbackToIPGeoLocation(); }}