Когда вы нажимаете кнопку, она отправляет вашу форму "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>