Michael
Michael

Reputation: 324

Geolocation with Ionic

I have to develop an app that create a map and ad a marker on my position. I use this code to create a 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);

 });

Now latitude and longitude are static, I have to get mylat and mylng by GPS. How can I do this? (I know that I have to use cordova plugin geolocation, but I don't know how)

Upvotes: 1

Views: 975

Answers (1)

Mudasser Ajaz
Mudasser Ajaz

Reputation: 6257

You can find detailed documentation here Here are some highlighted steps, copied from documentation

  1. add plugin using command cordova plugin add org.apache.cordova.geolocation in case of cordova version < 5 , otherwise cordova plugin add cordova-plugin-geolocation

    Make this call in your script where you need lat and long navigator.geolocation.getCurrentPosition(onSuccess, onError);

Whole example will be like this

// 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);

Your controller will be like this :

.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);

 });

Upvotes: 2

Related Questions