【问题标题】:Geolocation with Ionic使用 Ionic 进行地理定位
【发布时间】:2015-08-27 15:04:14
【问题描述】:

我必须开发一个应用程序来创建地图并在我的位置上添加一个标记。 我使用此代码创建地图:

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插件地理定位,但我不知道如何)

【问题讨论】:

    标签: javascript android cordova google-maps ionic-framework


    【解决方案1】:

    你可以找到详细的文档here 以下是从文档中复制的一些突出显示的步骤

    1. 使用命令cordova plugin add org.apache.cordova.geolocation添加插件,如果cordova版本cordova plugin add cordova-plugin-geolocation

      在您需要 lat 和 long 的脚本中进行此调用 navigator.geolocation.getCurrentPosition(onSuccess, onError);

    整个例子都是这样的

    // onSuccess Callback
    // This method accepts a Position object, which contains the
    // current GPS coordinates
    //
    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');
    };
    
    // onError Callback receives a PositionError object
    //
    function onError(error) {
        alert('code: '    + error.code    + '\n' +
              'message: ' + error.message + '\n');
    }
    
    navigator.geolocation.getCurrentPosition(onSuccess, onError);
    

    你的控制器会是这样的:

    .controller('MapCtrl', function($scope, $ionicLoading, $compile) {
      function initialise() {   
        function onSuccess(position){
          var myLatlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
          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; 
        }
        function onError(error){
          alert('code: '    + error.code    + '\n' +
                  'message: ' + error.message + '\n');
        }
        navigator.geolocation.getCurrentPosition(onSuccess, onError);   
      }
      google.maps.event.addDomListener(window, 'load', initialise);
    
     });
    

    【讨论】:

      猜你喜欢
      • 2012-01-04
      • 2010-11-12
      • 2011-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-26
      相关资源
      最近更新 更多