我正在尝试在webview中加载我当前的位置。
但 navigator.geolocation 正在返回undefined?也与 navigator.online 相同?
是android的webview中不支持的导航器对象吗?
如何获取当前的设备位置?
答案 0 :(得分:0)
我正在使用window.navigator.onLine
,这给了我真实的时间
我连接到互联网,否则它会给出错误;
并且不要忘记添加到config.xml
<feature name="NetworkStatus" >
<param
name="android-package"
value="org.apache.cordova.NetworkManager" />
</feature>
的AndroidManifest.xml
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
获取当前位置:
<script type="text/javascript"
src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script type="text/javascript" charset="utf-8">
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady(){
navigator.geolocation.getCurrentPosition(onSuccess, onError, {
maximumAge : 3000,
timeout : 5000,
enableHighAccuracy : true
});
}
var address;
function onSuccess(position) {
var reverseGeocoder = new google.maps.Geocoder();
var currentPosition = new google.maps.LatLng(position.coords.latitude,
position.coords.longitude);
reverseGeocoder.geocode({
'latLng' : currentPosition
}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[0]) {
address= results[0].formatted_address;
}else{
addess='address not found';
}
}
});
}
// onError Callback receives a PositionError object
//
function onError(error) {
//alert('code: ' + error.code + '\n' + 'message: ' + error.message + '\n');
}
console.log(address);
</script>
添加config.xml
<feature name="Geolocation" >
<param
name="android-package"
value="org.apache.cordova.GeoBroker" />
</feature>
添加androidmanifest.xml