Rakesh Ranjan
Rakesh Ranjan

Reputation: 73

How can I retrieve the latitude and longitude using Ionic?

How can I get the latitude/longitude using the Ionic Framework. In Android it sometimes provides it as intended and sometimes it doesn't give latitude/longitude using the following:

navigator.geolocation.getCurrentPosition(onSuccess, onError, options);  

Upvotes: 1

Views: 1776

Answers (3)

Joseph Hansen
Joseph Hansen

Reputation: 13249

You could use a call to a URL API which returns a JSON object, and then get the latitude and longitude. The following example calls http://freegeoip.net/json/ and prints out the results. To access the latitude and longitude, you would use the result.latitude and result.longitude fields.

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("button").click(function(){
        $.getJSON("http://freegeoip.net/json/", function(result){
            $.each(result, function(i, field){
                $("div").append(field + " ");
            });
        });
    });
});
</script>
</head>
<body>

<button>Get JSON data</button>

<div></div>

</body>
</html>

Upvotes: 2

Ben Elghali Beyram
Ben Elghali Beyram

Reputation: 45

this is an example how to get Lat,Long using https://github.com/apache/cordova-plugin-geolocation

.controller('MyCtrl', function($scope, $cordovaGeolocation) {
   var posOptions = {timeout: 10000, enableHighAccuracy: false};
   $cordovaGeolocation
   .getCurrentPosition(posOptions)

   .then(function (position) {
      var lat  = position.coords.latitude
      var long = position.coords.longitude
      console.log(lat + '   ' + long)
   }, function(err) {
      console.log(err)
   });

   var watchOptions = {timeout : 3000, enableHighAccuracy: false};
   var watch = $cordovaGeolocation.watchPosition(watchOptions);

   watch.then(
      null,

      function(err) {
         console.log(err)
      },

      function(position) {
         var lat  = position.coords.latitude
         var long = position.coords.longitude
         console.log(lat + '' + long)
      }
   );

   watch.clearWatch();

})

You also noticed posOptions and watchOptions objects. We are using timeout to adjust maximum length of time that is allowed to pass in milliseconds and enableHighAccuracy is set to false. It can be set to true to get the best possible results but sometimes it can lead to some errors. There is also maximumAge option that can be used to show how old position is accepted. It is using milliseconds, the same as timeout option.

When we start our app and open the console it will log the latitude and longitude of the device. When our position is changed, the lat and long values will change.

Upvotes: 1

iCediCe
iCediCe

Reputation: 1722

Not quite sure what you are asking here, but Ionic can't give you geolocations, cordova however can. If you want to make it easy for yourself you should look into ngCordova, it's a angular wrapper for cordova plugins. Have a look here: http://ngcordova.com/docs/plugins/geolocation/

Upvotes: 0

Related Questions