我必须开发一个应用程序来创建 Map 并在我的位置上添加标记 . 我使用此代码创建一个 Map :
HTML
....
<body ng-app="starter">
<ion-pane>
<ion-header-bar class="bar-stable">
<h1 class="title">Ionic Blank Starter</h1>
</ion-header-bar>
<ion-content ng-controller="MapCtrl">
<div id="map" data-tap-disabled="true"></div>
</ion-content>
</ion-pane>
...
JAVASCRIPT
angular.module('starter', ['ionic'])
.run(function($ionicPlatform) {
$ionicPlatform.ready(function() {
// Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
// for form inputs)
if(window.cordova && window.cordova.plugins.Keyboard) {
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
}
if(window.StatusBar) {
StatusBar.styleDefault();
}
});
})
.controller('MapCtrl', function($scope, $ionicLoading, $compile) {
function initialise() {
var myLatlng = new google.maps.LatLng(53.068165,-4.076803);
var mapOptions = {
zoom: 15,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP,
}
var map = new google.maps.Map(document.getElementById('map'), mapOptions);
var marker = new google.maps.Marker({
position: myLatlng,
map: map,
});
$scope.map = map;
}
google.maps.event.addDomListener(window, 'load', initialise);
});
现在纬度和经度都是静态的,我必须通过GPS获取mylat和mylng . 我怎样才能做到这一点? (我知道我必须使用cordova插件地理位置,但我不知道如何)
1 回答
您可以找到详细的文档here以下是从文档中复制的一些突出显示的步骤
在cordova版本<5的情况下
cordova plugin add org.apache.cordova.geolocation
添加插件,否则cordova plugin add cordova-plugin-geolocation
在您需要lat和long的脚本中进行此调用
navigator.geolocation.getCurrentPosition(onSuccess, onError);
整个例子都是这样的
你的控制器将是这样的: