Monaca/位置取得
提供: 初心者エンジニアの簡易メモ
インストール
公式参考:https://docs.monaca.io/ja/reference/cordova_9.0/geolocation/
位置情報の取得プラグインを有効に
設定/cordovaプラグイン管理/cordova-plugin-geolocation(がデフォで入っていると思うが、)を確認
なければ、設定/cordovaプラグイン管理/プラグインインポート/"cordova-plugin-geolocation"を入力欄にいれてインストール
サンプル
index.html
<script type="text/javascript" charset="utf-8" src="main.js"></script> <body onload="init();">
main.js
function init() {
console.log("init!");
document.addEventListener("deviceready", onDeviceReady, false);
}
function onDeviceReady() {
console.log("onDeviceReady!");
navigator.geolocation.getCurrentPosition(onSuccess, onError);
}
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');
}
ダイアログ内に位置情報が出れば成功。
