如何设法在科尔多瓦获取我的安卓手机的纬度和经度?
Posted
技术标签:
【中文标题】如何设法在科尔多瓦获取我的安卓手机的纬度和经度?【英文标题】:How to manage to take latitude and longitude of my android phone in cordova? 【发布时间】:2019-02-26 19:18:40 【问题描述】:请帮忙!如何通过“#btn_2”点击获取我手机的经纬度? 对于 index.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="css/bootstrap.css">
<link rel="stylesheet" type="text/css" href="css/index.css">
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/bootstrap.js"></script>
<script type="text/javascript" src="js/index.js"></script>
</head>
<body onload="Ready()">
<div class=".app">
<form style="padding: 15px" id="form_1" name="form_1">
<br>
<button type="Submit" id="btn_2">geo location</button>
</form>
<span id="result"></span>
</div>
</body>
</html>
对于 index.js:
function Ready()
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady()
$(document).ready(function()
$('#btn_2').click(function()
navigator.geolocation.getCurrentPosition(onSuccess, onError,
timeout: 5000, enableHighAccuracy: true );
);
);
var onSuccess = function(position)
alert('Latitude: ' + position.coords.latitude + '\n' +
'Longitude: ' + position.coords.longitude + '\n' +
'Altitude: ' + position.coords.altitude + '\n' +
'Accuracy: ' + position.coords.accuracy + '\n' +
'Altitude Accuracy: ' + position.coords.altitudeAccuracy + '\n' +
'Heading: ' + position.coords.heading + '\n' +
'Speed: ' + position.coords.speed + '\n' +
'Timestamp: ' + position.timestamp + '\n');
;
function onError(error)
alert('code: ' + error.code + '\n' +
'message: ' + error.message + '\n');
当尝试点击我手机中的按钮时,它没有显示任何警报,既没有成功也没有错误警报。请帮忙!
【问题讨论】:
【参考方案1】:当您按下按钮时,它会提交您的表单“form_1”,并且可能会重新加载页面。这可能是一个原因。尝试将按钮移出表单,或添加另一个不带 type="submit" 的按钮。
当您第一次尝试检查地理位置时,系统可能会询问您的权限。检查你是否没有否认我的错误。
您也可以尝试重新安装地理定位插件
cordova plugin add cordova-plugin-geolocation
这是我的工作代码
<div class="app">
<h1>Apache Cordova</h1>
<div id="deviceready" class="blink">
<p class="event listening">Connecting to Device</p>
<p class="event received">Device is Ready</p>
<button id="geo-button" class="button">Geo</div>
</div>
</div>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/index.js"></script>
index.js
var app =
// Application Constructor
initialize: function()
document.addEventListener('deviceready', this.onDeviceReady.bind(this), false);
,
// deviceready Event Handler
//
// Bind any cordova events here. Common events are:
// 'pause', 'resume', etc.
onDeviceReady: function()
this.receivedEvent('deviceready');
,
// Update DOM on a Received Event
receivedEvent: function(id)
var parentElement = document.getElementById(id);
var listeningElement = parentElement.querySelector('.listening');
var receivedElement = parentElement.querySelector('.received');
listeningElement.setAttribute('style', 'display:none;');
receivedElement.setAttribute('style', 'display:block;');
console.log('Received Event: ' + id);
const geoButton = document.getElementById('geo-button');
geoButton.addEventListener('click', function(e)
navigator.geolocation.getCurrentPosition(function(position)
alert('Latitude: ' + position.coords.latitude + '\n' +
'Longitude: ' + position.coords.longitude + '\n' +
'Altitude: ' + position.coords.altitude + '\n' +
'Accuracy: ' + position.coords.accuracy + '\n' +
'Altitude Accuracy: ' + position.coords.altitudeAccuracy + '\n' +
'Heading: ' + position.coords.heading + '\n' +
'Speed: ' + position.coords.speed + '\n' +
'Timestamp: ' + position.timestamp + '\n');
, function onError(error)
alert('code: ' + error.code + '\n' +
'message: ' + error.message + '\n');
);
);
别忘了为 ios 添加它
<edit-config file="*-Info.plist" mode="merge" target="NSLocationWhenInUseUsageDescription">
<string>need location access to find things nearby</string>
</edit-config>
【讨论】:
以上是关于如何设法在科尔多瓦获取我的安卓手机的纬度和经度?的主要内容,如果未能解决你的问题,请参考以下文章
手机上获取地图某个定位的经纬度坐标的方法 - 查询经度纬度 - 百度地图app高德地图appEarth地球