Quantcast
Channel: Ionic Framework - Ionic Forum
Viewing all articles
Browse latest Browse all 49184

Get current location of device

$
0
0

@Giregar wrote:

Hello,
my app has a problem with geolocation stuff.

I want the app to get the current device position. Currently, I'm using $cordovaGeolocation. At the end, I just get the two following errors:

ionic.bundle.js:25642 TypeError: Cannot read property 'coords' of undefined at vendorSearchCtrl.js:61

ionic.bundle.js:25642 TypeError: Cannot read property 'geometry' of undefined at vendorSearchCtrl.js:57

function init() {
    $scope.getPosition();
    $scope.getAllVendors();
  };

  $scope.getPosition = function() {
    // Ermittelt die aktuelle Position
    var posOptions = {timeout: 10000, enableHighAccuracy: false};
    $cordovaGeolocation.getCurrentPosition(posOptions)
    .then(function (position) {
      // Für die Navigation
      $scope.PosLat  = position.coords.latitude
      $scope.PosLng = position.coords.longitude
      // Für das anzeigen der Entfernung
      $scope.position = position;
    }, function(err) {
      // error
    });
  }

  // Gibt alle Anbieter aus
  $scope.getAllVendors = function() {
    vendorService.getAllVendors()
      .success(function(data) {
        // Daten im View verfügbar machen
        $scope.vendors = data;

        // Adresse zu einem String zusammensetzen und dem Objekt hinzufügen
        for(var i = 0; i < $scope.vendors.length; i++) {
          $scope.vendors[i].adresse = $scope.vendors[i].strasse + " " + $scope.vendors[i].hausnummer + " " + $scope.vendors[i].ort;
        }

        // Konvertiert die Adresse in Längen- und Breitengrad
        angular.forEach($scope.vendors, function(value) {
          googleMapsService.convertAddress(value.adresse)
            .success(function (data) {

              // Schreibt die Lng/Lat in das vorgesehene Eigenschaft des Anbieter Objektes
              value.lat = data.results[0].geometry.location.lat;
              value.lng = data.results[0].geometry.location.lng;

              // Ermittelt die Distanz zwischen dem aktuellen Standort und den Zielen
              var from = new google.maps.LatLng($scope.position.coords.latitude, $scope.position.coords.longitude);
              var to = new google.maps.LatLng(value.lat, value.lng);
              var distance = google.maps.geometry.spherical.computeDistanceBetween(from, to).toFixed(2);
              value.distanz = distance/1000;
            });
        });

        // Nachdem gesucht wurde, wird die Tastatur geschlossen
        if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) {
          cordova.plugins.Keyboard.close();
        }

        // Auflistung aller Anbieter anzeigen
        $scope.showAll = true;

        // Auflistung gesuchter Anbieter ausblenden
        $scope.showSpecific = false;

        // Fehlermeldung ausblenden
        $scope.errorBox = false;

      })

      .error(function(data, status, headers, config) {
        // error handle
      });
  };

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 49184

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>