最详细的geolocation

题目有点标题党,呵呵,别介意。

参考了Google,baidu还有街旁的GeoLocation代码


<!doctype html>
<html>
<head>
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0;" name="viewport" />
<title>test</title>
<style>

</style>
<script>
大部分浏览器位置服务调用的Google的一个服务,这个服务不仅可以IP定位,还有很多的其他定位方式,总之很强大

function initLocation() {
// We are already defined. Hooray!
if (window.google && google.gears) {
return;
}

var factory = null;

// Firefox
if (typeof GearsFactory != 'undefined') {
factory = new GearsFactory();
} else {
// IE
try {
factory = new ActiveXObject('Gears.Factory');
// privateSetGlobalObject is only required and supported on IE Mobile on
// WinCE.
if (factory.getBuildInfo().indexOf('ie_mobile') != -1) {
factory.privateSetGlobalObject(this);
}
} catch (e) {
// Safari
if ((typeof navigator.mimeTypes != 'undefined') && navigator.mimeTypes["application/x-googlegears"]) {
factory = document.createElement("object");
factory.style.display = "none";
factory.width = 0;
factory.height = 0;
factory.type = "application/x-googlegears";
document.documentElement.appendChild(factory);
if(factory && (typeof factory.create == 'undefined')) {
// If NP_Initialize() returns an error, factory will still be created.
// We need to make sure this case doesn't cause Gears to appear to
// have been initialized.
factory = null;
}
}
}
}

// *Do not* define any objects if Gears is not installed. This mimics the
// behavior of Gears defining the objects in the future.
if (!factory) {
return;
}

// Now set up the objects, being careful not to overwrite anything.
//
// Note: In Internet Explorer for Windows Mobile, you can't add properties to
// the window object. However, global objects are automatically added as
// properties of the window object in all browsers.
if (!window.google) {
google = {};
}

if (!google.gears) {
google.gears = {factory: factory};
}
};

function getGeoLocation(okCallback,errorCallback){
initLocation();
try {
if(navigator.geolocation) {
geo = navigator.geolocation;
} else {
geo = google.gears.factory.create('beta.geolocation');
}
}catch(e){}

if (geo) {
//watch会触发多次,以便随时监控ip改变,iphone在开始会调用两次,屏幕旋转和解锁也会调用,汗。。。
//navigator.geolocation.watchPosition(successCallback, errorCallback, options);
geo.getCurrentPosition(okCallback , errorCallback);
} else {
alert("不好意思,你不让我定位!");
}

}


function okCallback(d){
alert('当前位置(纬度,经度): ' + d.latitude + ',' + d.longitude);
//iphone
if(d.coords)
alert('当前位置(纬度,经度): ' + d.coords.latitude + ',' + d.coords.longitude);
if(d.gearsAddress)
alert(d.gearsAddress.city);

};
function errorCallback(err){
alert(err.message);
};

</script>
</head>
<body>
咔咔咔咔咔
<input onclick="getGeoLocation(okCallback,errorCallback)" type="button" value="errr">
</body>
</html>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值