2016-07-18 3 views
0

Ich habe eine App, wo (AngularJs + Cordova), ich brauche die aktuelle Position des Benutzers und navigieren Sie zum Ziel mit Google Maps.Wie Navigator in Cordova in IOS starten

Ich bin nicht in der Lage, um es in iOS zu bekommen arbeiten:

Ich habe versucht:

/* $scope.launchNavigator = function() { 
 
    
 
    console.log("$scope.launchNavigator..."); 
 
    
 
     var deviceType = (navigator.userAgent.match(/iPad/i))  == "iPad" ? "iPad" : (navigator.userAgent.match(/iPhone/i))  == "iPhone" ? "iPhone" : (navigator.userAgent.match(/Android/i)) == "Android" ? "Android" : (navigator.userAgent.match(/BlackBerry/i)) == "BlackBerry" ? "BlackBerry" : "null"; 
 
     
 
     cordova.plugins.diagnostic.isLocationEnabled(onRequestSuccess, onRequestFailure); 
 
     
 
     if(deviceType === 'Android'){ 
 
      cordova.plugins.locationAccuracy.request(onRequestSuccess, onRequestFailure, cordova.plugins.locationAccuracy.REQUEST_PRIORITY_HIGH_ACCURACY); 
 
     } 
 
     else { 
 
      // vm.showGPSAlert = true; 
 
      // vm.GPSTextAlert = "Please enable Location Services in Settings and Try again" 
 
      //function onDeviceReady() { 
 
      cordova.plugins.diagnostic.switchToLocationSettings(); 
 
      //navigator.geolocation.getCurrentPosition(onSuccess, onError); 
 
      //} 
 
     } 
 
    } 
 

 
    function onRequestSuccess(success){ 
 
     function onDeviceReady() { 
 
     navigator.geolocation.getCurrentPosition(onSuccess, onError); 
 
     } 
 
    } 
 

 
     function onSuccess(position){ 
 
      console.log('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'); 
 
     
 
      latitudeStart = position.coords.latitude; 
 
      longitudeStart = position.coords.longitude; 
 

 
      launchnavigator.isAppAvailable(launchnavigator.APP.GOOGLE_MAPS, function(isAvailable){ 
 
       var app; 
 
       console.log("Location navigate .lat." +) 
 
       if(isAvailable){ 
 
        app = launchnavigator.APP.GOOGLE_MAPS; 
 
       }else{ 
 
        console.warn("Google Maps not available - falling back to user selection"); 
 
        app = launchnavigator.APP.USER_SELECT; 
 
       } 
 
       launchnavigator.navigate([vm.dest.latitude,vm.dest.longitude], { 
 
        app: app, 
 
        start: [latitudeStart,longitudeStart] 
 
       }); 
 
      }); 
 
     } 
 
     
 
     function onError(){ 
 
      
 
     } 
 

 
     function onRequestFailure(error){ 
 
      console.error("Accuracy request failed: error code="+error.code+"; error message="+error.message); 
 
      if(error.code !== cordova.plugins.locationAccuracy.ERROR_USER_DISAGREED){ 
 
       if(window.confirm("Failed to automatically set Location Mode to 'High Accuracy'. Would you like to switch to the Location Settings page and do this manually?")){ 
 
        cordova.plugins.diagnostic.switchToLocationSettings(); 
 
       } 
 
      } 
 
     }*/

< --die oben funktioniert nicht für beide iOS und Android- ->

Für android, folgende Arbeiten:

 /*Get Direction*/ 
 

 
    function onDeviceReady() { 
 
     //window.open = cordova.InAppBrowser.open; 
 
     console.log("Hello... Device redy"); 
 

 
     var latitudeStart = ''; 
 
     var longitudeStart = ''; 
 

 
     $scope.launchNavigator = function() { 
 
     
 
     function onRequestSuccess(success){ 
 
      console.log("Successfully requested accuracy: "+success.message); 
 

 
      if(navigator.geolocation){ 
 
       console.log("navigator.geolocation works well"); 
 
      } 
 
      else{ 
 
      console.log("navigator.geolocation doesnt works well"); 
 
      } 
 

 
     console.log("Luanch navigate.."); 
 

 
     var onSuccess = function(position) { 
 
     console.log('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'); 
 
      
 
      latitudeStart = position.coords.latitude; 
 
      longitudeStart = position.coords.longitude; 
 

 
      launchnavigator.isAppAvailable(launchnavigator.APP.GOOGLE_MAPS, function(isAvailable){ 
 
       var app; 
 
       if(isAvailable){ 
 
        app = launchnavigator.APP.GOOGLE_MAPS; 
 
       }else{ 
 
        console.warn("Google Maps not available - falling back to user selection"); 
 
        app = launchnavigator.APP.USER_SELECT; 
 
       } 
 
       launchnavigator.navigate([vm.dest.latitude,vm.dest.longitude], { 
 
        app: app, 
 
        start: [latitudeStart,longitudeStart] 
 
       }); 
 
      }); 
 
     }; 
 

 
     // onError Callback receives a PositionError object 
 
     // 
 
     function onError(erro) { 
 
      console.log('code: ' + error.code + '\n' + 
 
        'message: ' + error.message + '\n'); 
 
     } 
 

 
     navigator.geolocation.getCurrentPosition(onSuccess, onError,{enableHighAccuracy:true}); 
 
     } 
 

 
     function onRequestFailure(error){ 
 
      console.error("Accuracy request failed: error code="+error.code+"; error message="+error.message); 
 
      if(error.code !== cordova.plugins.locationAccuracy.ERROR_USER_DISAGREED){ 
 
       if(window.confirm("Failed to automatically set Location Mode to 'High Accuracy'. Would you like to switch to the Location Settings page and do this manually?")){ 
 
        cordova.plugins.diagnostic.switchToLocationSettings(); 
 
       } 
 
      } 
 
     } 
 

 
     cordova.plugins.locationAccuracy.request(onRequestSuccess, onRequestFailure, cordova.plugins.locationAccuracy.REQUEST_PRIORITY_HIGH_ACCURACY); 
 

 

 
     
 
     }

Antwort

1

die oben nicht funktioniert für beide iOS und Android

Erstens würde ich cordova-plugin-device nutzen die Plattform, um zu bestimmen, anstatt User-Agent-Sniffing - es ist robuster .

Zweitens scheint es eine Funktion abhängig von der deviceready Veranstaltung haben, die auf das erfolgreiche Ergebnis eines Plugins Ergebnis aufgerufen wird:

function onRequestSuccess(success){ 
    function onDeviceReady() { 
    navigator.geolocation.getCurrentPosition(onSuccess, onError); 
    } 
} 

Cordova Plugins dynamisch geladen werden, bevor das deviceready Ereignis aufgerufen wird, so ist es wahrscheinlich, dass die innere Funktion nie aufgerufen wird.

Drittens scheint die Erfolgsrückruffunktion für beide cordova.plugins.diagnostic.isLocationEnabled() und cordova.plugins.locationAccuracy.request()onRequestSuccess() zu sein. Beide werden in serieller Synchronität aufgerufen, ihre Callbacks werden jedoch asynchron aufgerufen, was wahrscheinlich zu Problemen führt.

Ich würde so etwas wie dies versucht:

function onDeviceReady(){ 
    console.log("onDeviceReady..."); 

    $scope.launchNavigator = function() { 
     console.log("$scope.launchNavigator..."); 

     cordova.plugins.diagnostic.isLocationEnabled(function(enabled){ 
      if(!enabled){ 
       if(device.platform === 'Android'){ 
        cordova.plugins.locationAccuracy.request(getCurrentPosition, 
        function(error){ 
         console.error("Accuracy request failed: error code="+error.code+"; error message="+error.message); 
         if(error.code !== cordova.plugins.locationAccuracy.ERROR_USER_DISAGREED){ 
          if(window.confirm("Failed to automatically set Location Mode to 'High Accuracy'. Would you like to switch to the Location Settings page and do this manually?")){ 
           cordova.plugins.diagnostic.switchToLocationSettings(); 
          } 
         } 
        }, cordova.plugins.locationAccuracy.REQUEST_PRIORITY_HIGH_ACCURACY); 
       } 
       else { 
        // vm.showGPSAlert = true; 
        // vm.GPSTextAlert = "Please enable Location Services in Settings and Try again" 
        //function onDeviceReady() { 
        cordova.plugins.diagnostic.switchToLocationSettings(); 
        //navigator.geolocation.getCurrentPosition(onSuccess, onError); 
        //} 
       } 
      }else{ 
       getCurrentPosition(); 
      } 

     }, function onLocationEnabledFailure(error){ 
      console.error("Failed to check if location is enabled"); 
     }); 

     function getCurrentPosition(){ 
      navigator.geolocation.getCurrentPosition(onSuccess, onError); 
     } 

     function onSuccess(position){ 
      console.log('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'); 

      latitudeStart = position.coords.latitude; 
      longitudeStart = position.coords.longitude; 

      launchnavigator.isAppAvailable(launchnavigator.APP.GOOGLE_MAPS, function(isAvailable){ 
       var app; 
       console.log("Location navigate .lat." +) 
       if(isAvailable){ 
        app = launchnavigator.APP.GOOGLE_MAPS; 
       }else{ 
        console.warn("Google Maps not available - falling back to user selection"); 
        app = launchnavigator.APP.USER_SELECT; 
       } 
       launchnavigator.navigate([vm.dest.latitude,vm.dest.longitude], { 
        app: app, 
        start: [latitudeStart,longitudeStart] 
       }); 
      }); 
     } 

     function onError(positionError){ 
      // Handle Error 
     } 

    } //$scope.launchNavigator 
}// onDeviceReady 

Im Allgemeinen würde ich empfehlen, die jeweils mit Safari Remote Debugging und Chrome Remote-Debugging-Tool Ihre Anwendung zu debuggen, während des Laufens auf iOS und Android-Geräten . Wenn Sie den Schritt-zu-Schritt-Debugger mit den entsprechenden Haltepunkten verwenden, werden Probleme, wie sie in Ihrem Code-Snippet vorhanden sind, hervorgehoben. Weitere Informationen finden Sie im Abschnitt Debugging Cordova apps in den Cordova-Dokumenten.