如何在 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(); }}
如此一便能在 mobile web app 中取得手的度座料了!