手里的项目是webApp,用的 webViews。
第一个BETA版是在客户端做定位,我与安卓和IOS传参交互觉得很麻烦,但苦于没有时间,今天终于得空,研究一下浏览器端定位。
用的百度开发者地图API。
<div id="latitude"></div>
<div id="longitude"></div>
<div id="reverseSampleUrl"></div>
<div id="reverseNarrative"></div>
var geol;
var nowLo;
var nowLa;
try {
if (typeof(navigator.geolocation) == 'undefined') {
geol = google.gears.factory.create('beta.geolocation');
} else {
geol = navigator.geolocation;
}
}catch (error) {
//alert(error.message);
}
if (geol) {
geol.getCurrentPosition(function(position) {
var nowLatitude = position.coords.latitude;
var nowLongitude = position.coords.longitude;
//alert("纬度:" + nowLatitude + ", 经度:" + nowLongitude);
$('#latitude').html(nowLatitude);
$('#longitude').html(nowLongitude);
nowLa = nowLatitude;
nowLo = nowLongitude;
//alert(nowLa);
doReverse();
}, function(error) {
switch(error.code){
case error.TIMEOUT :
alert("连接超时,请重试");
break;
case error.PERMISSION_DENIED :
alert("您拒绝了使用位置共享服务,查询已取消");
break;
case error.POSITION_UNAVAILABLE :
alert("非常抱歉,我们暂时无法通过浏览器获取您的位置信息");
break;
}
}, {timeout:10000}); //设置十秒超时
}
function doReverse() {
document.getElementById('reverseNarrative').innerHTML = '';
var script = document.createElement('script');
script.type = 'text/javascript';
script.src =' http://api.map.baidu.com/geocoder/v2/?ak=你的密钥(百度注册一个开发者账号)&callback=renderReverse&location=' +nowLa+',' + nowLo +'&output=json&pois=0;'
document.body.appendChild(script);
};
function renderReverse(response) {
var html = '';
if (response.status ) {
//alert(response.status);
var text = "1234567:\n";
document.getElementById('reverseNarrative').innerHTML = text;
return;
}
var location = response.result.location;
html = "城市: "+ response.result.addressComponent.city;
document.getElementById('reverseNarrative').innerHTML = html;
return;
}