2015-08-27 87 views
0

我必须开发一个应用程序,创建一个地图,并在我的位置上标记一个标记。 我用这个代码来创建地图:地理位置与离子

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。 我该怎么做? (我知道我必须使用插件科尔多瓦地理位置,但我不知道如何)

回答

2

你可以找到详细的文档here 这里有一些突出的步骤,从文档

  1. 中加入插件,复制在科尔多瓦版本< 5的情况下使用命令cordova plugin add org.apache.cordova.geolocation, 否则cordova plugin add cordova-plugin-geolocation

    请在你的脚本这个调用,你需要lat和长 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); 

});