【发布时间】:2013-07-05 12:39:07
【问题描述】:
你能告诉我如何使用phonegap在jquery mobile中找到地理位置(经度和纬度)。然后我需要在地图上显示位置。
【问题讨论】:
标签: android jquery-mobile cordova
你能告诉我如何使用phonegap在jquery mobile中找到地理位置(经度和纬度)。然后我需要在地图上显示位置。
【问题讨论】:
标签: android jquery-mobile cordova
我假设您已推荐 Phonegap's Getting Started Guide 创建基本项目。
这是获取纬度和经度的脚本
/*
* This file contains script to get lattitude and longitude
*
*/
var lat = 0;
var lng = 0;
//A button click will call this function
function getLocation() {
navigator.geolocation.getCurrentPosition(onSuccess, onError, { enableHighAccuracy: true });
}
// onSuccess Geolocation
//
function onSuccess(position) {
//Lat long will be fetched and stored in session variables
//These variables will be used while storing data in local database
lat = position.coords.latitude;
lng = position.coords.longitude;
alert('Lattitude: ' + lat + ' Longitude: ' + long);
sessionStorage.setItem('lattitude', lat);
sessionStorage.setItem('longitude', lng);
}
// onError Callback receives a PositionError object
//
function onError(error) {
alert('code: ' + error.code + '\n' +
'message: ' + error.message + '\n');
}
您必须在 manifest 文件中添加必要的权限。
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
调用getLocation() function onclick 按钮或任何您希望获取地理位置的事件。
更多详情请咨询Phonegap Geolocation Documentation
要显示谷歌地图,请参考Google MAP API
希望对您有所帮助。
【讨论】: