2016-08-25 2 views
1

Ich habe die folgenden Schritte durchgeführt, eine App zu erstellen:Ionic App fragt nicht Erlaubnis Standort zuzugreifen, während der Installation von

ionic start ionic-maps blank 
cd ionic-maps 
ionic setup sass 
ionic io init 
ionic platform add android 
bower install ngCordova 

addierten die folgenden Zeilen zu index.html:

<script src="lib/ngCordova/dist/ng-cordova.js"></script> 
<script src="cordova.js"></script> 

geänderte app.js ngCordova enthalten:

angular.module('starter', ['ionic', 'starter.controllers', 'ngCordova']) 

installiert das Geolocation Plugin:

cordova plugin add cordova-plugin-geolocation 

app.js:

.state('app.location', { 
    url: '/location', 
    views: { 
    'menuContent': { 
     templateUrl: 'templates/location.html', 
     controller: 'LocationCtrl' 
    } 
    } 
}) 

location.html:

<ion-view view-title="Search"> 
    <ion-content> 
    <h1>Location: {{location}}</h1> 
    </ion-content> 
</ion-view> 

controllers.js:

.controller('LocationCtrl', function($scope, $state, $cordovaGeolocation) { 
    $scope.location = 'Waiting'; 

    var options = {timeout: 10000, enableHighAccuracy: true}; 
    $cordovaGeolocation.getCurrentPosition(options).then(function(position) { 
    var lat = position.coords.latitude; 
    var lng = position.coords.longitude; 
    console.log(lat, lng); 
    $scope.location = lat + ' ' + lng; 
    }, function(error) { 
    console.log('Could not get location: ', error); 
    $scope.location = 'Could not get location: ' + error + ' :: ' + JSON.stringify(error); 
    }); 
}) 

Wenn öffne ich den/Standort Endpunkt in Browser meines Telefons (mit ionic serve) Ich habe meinen aktuellen Standort richtig angezeigt. Bis jetzt funktioniert alles wie erwartet.

/platforms/android/AndroidManifest.xml hat die folgenden Zeilen in ihm:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 

Ich baue die App des neuen Cloud-Paket Builder verwendet ionic package build android verwenden. ionic package list zeigt, dass der Build erfolgreich war.

Ich erwartete, dass ich um Erlaubnis gefragt werde, während ich die App von apk installiere. Aber ich werde nur nach gefragt. Beim Besuch der /locations Bildschirm erhalte ich einen Fehler [object PositionError. json.stringify(error) ist {} und Object.keys(error).length ist 0.

Die Verwendung von navigator.geolocation.getCurrentPosition anstelle von $cordovaGeolocation.getCurrentPosition hilft nicht.

alert(error.code) ist 2 und alert(error.message) ist application does not have sufficient geolocation permisions.

PS: Ich habe dafür gesorgt, dass GPS aktiviert ist und es funktioniert auf andere Anwendungen.

+0

Ich habe das gleiche Problem auch, stellt sich heraus, etwas mit dem neuesten Cordova Update und Android 6.0. Soweit von dem, was ich herausgefunden habe, müssen Sie auf die API zugreifen, obwohl Sie eine https: // * -Adresse haben. Wie auch immer, ionic dient/läuft auf http. Ich versuche einen Weg zu finden, um eine SSL auf das Handy zu "pinnen". Entweder das oder die Anwendung über die Cloud hosten. Anscheinend können Sie ein nicht vertrauenswürdiges selbstsigniertes Zertifikat anheften, aber ich hatte nicht viel Glück. Ich bin jetzt seit 4 Tagen dabei. – Deciantis

Antwort

4

Führen Sie cordova plugin add cordova-plugin-geolocation mit --save Option d. H. cordova plugin add cordova-plugin-geolocation --save, so dass das Plugin config.xml hinzugefügt wird.

Alt: Hinzufügen von "cordova-plugin-geolocation" zu "cordovaPlugins" in package.json löst auch das Problem, ist aber veraltet.

Verwandte Themen