PhoneGapAPI帮助文档翻译整理9Geolocation 定位.docx
- 文档编号:24597967
- 上传时间:2023-05-29
- 格式:DOCX
- 页数:20
- 大小:18.89KB
PhoneGapAPI帮助文档翻译整理9Geolocation 定位.docx
《PhoneGapAPI帮助文档翻译整理9Geolocation 定位.docx》由会员分享,可在线阅读,更多相关《PhoneGapAPI帮助文档翻译整理9Geolocation 定位.docx(20页珍藏版)》请在冰豆网上搜索。
PhoneGapAPI帮助文档翻译整理9Geolocation定位
Geolocation (定位)
Methods
geolocation.getCurrentPosition(获得当前位置)
返回Position对象作为当前设备的位置
navigator.geolocation.getCurrentPosition(geolocationSuccess,
[geolocationError],
[geolocationOptions]);
参数
∙geolocationSuccess:
使用当前位置作为参数的回调函数
∙geolocationError:
(可选)如果有错误发生则调用此回调函数
∙geolocationOptions:
(可选)地理位置选项
说明
geolocation.getCurrentPositon是一个异步函数.他通过回调携带以Position对象为参数的geolocationSuccess方法返回设备当前位置。
支持的平台
∙Android
∙BlackBerry(OS4.6)
∙BlackBerryWebWorks(OS5.0andhigher)
∙iPhone
简单的例子
//onSuccessCallback
//Thismethodacceptsa`Position`object,whichcontains
//thecurrentGPScoordinates
//
varonSuccess=function(position){
alert('Latitude:
'+position.coords.latitude+'\n'+
'Longitude:
'+position.coords.longitude+'\n'+
'Altitude:
'+position.coords.altitude+'\n'+
'Accuracy:
'+position.coords.accuracy+'\n'+
'AltitudeAccuracy:
'+position.coords.altitudeAccuracy+'\n'+
'Heading:
'+position.coords.heading+'\n'+
'Speed:
'+position.coords.speed+'\n'+
'Timestamp:
'+newDate(position.timestamp)+'\n');
};
//onErrorCallbackreceivesaPositionErrorobject
//onError :
本回调函数接收PositionError对象
functiononError(error){
alert('code:
'+error.code+'\n'+
'message:
'+error.message+'\n');
}
navigator.geolocation.getCurrentPosition(onSuccess,onError);
完整的例子
DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01//EN""http:
//www.w3.org/TR/html4/strict.dtd">
//WaitforPhoneGaptoload
//等待加载PhoneGap
functiononLoad(){
document.addEventListener("deviceready",onDeviceReady,false);
}
//PhoneGapisready
//PhoneGap加载完毕
functiononDeviceReady(){
navigator.geolocation.getCurrentPosition(onSuccess,onError);
}
//onSuccessGeolocation
//onSuccess定位
functiononSuccess(position){
varelement=document.getElementById('geolocation');
element.innerHTML='Latitude:
'+position.coords.latitude+'
'+
'Longitude:
'+position.coords.longitude+'
'+
'Altitude:
'+position.coords.altitude+'
'+
'Accuracy:
'+position.coords.accuracy+'
'+
'AltitudeAccuracy:
'+position.coords.altitudeAccuracy+'
'+
'Heading:
'+position.coords.heading+'
'+
'Speed:
'+position.coords.speed+'
'+
'Timestamp:
'+newDate(position.timestamp)+'
';
}
//onErrorCallbackreceivesaPositionErrorobject
//此回调函数接收PositionError对象
functiononError(error){
alert('code:
'+error.code+'\n'+
'message:
'+error.message+'\n');
}