当我使用ons.navigator.pushPage并导航到searchNearby html页面时,它应该显示当前的地理位置坐标。但它不会这样做。当我直接加载searchNearby页面时,它会完美地显示坐标。任何想法
的index.html
<body ng-controller="courseController">
<ons-navigator title="Class Finder">
<ons-page>
<ons-row>
<ons-col size="80" style="padding: 8px">
<ons-search-input ng-model="searchText" placeholder="Search..." >
</ons-search-input>
</ons-col>
<ons-col size="20" >
<ons-button type="large--quiet" ng-click="ons.navigator.pushPage('searchNearby/index.html',{title:'Search Nearby'})">Search nearby
</ons-button>
</ons-col>
</ons-row>
</ons-page>
</ons-navigator>
</body>
searchNearby / index.html中
<body>
<p id="geolocation">Finding geolocation...</p>
<div id="t01"> </div>
<script type="text/javascript" charset="utf-8">
var lat,lon;
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
navigator.geolocation.getCurrentPosition(onSuccess, onError);
}
function onSuccess(position) {
var element = document.getElementById('geolocation');
element.innerHTML = 'Latitude: ' + position.coords.latitude + '<br />' +
'Longitude: ' + position.coords.longitude + '<br />' +
'Altitude: ' + position.coords.altitude + '<br />' +
'Accuracy: ' + position.coords.accuracy + '<br />' +
'Altitude Accuracy: ' + position.coords.altitudeAccuracy + '<br />' +
'Heading: ' + position.coords.heading + '<br />' +
'Speed: ' + position.coords.speed + '<br />' +
'Timestamp: ' + position.timestamp + '<br />';
}
function onError(error){
alert('code: ' + error.code + '\n' +
'message: ' + error.message + '\n');
}
</script>
</body>
答案 0 :(得分:0)
您的JS不会被搜索到searchNearby / index.html,因为您的页面是异步加载的。
我建议在页面完全加载时设置某种回调,这样就可以执行getCurrentPosition方法了。
或者,您可以预先获取该位置,然后将成功对象传递给searchNearby页面。