简体   繁体   中英

How to launch Navigator in cordova in ios

I have an app where (AngularJs+cordova), I need to get current position of the user and navigate to destination using google maps.

I am unable to get it work in iOS:

I have tried:

 /* $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(); } } }*/ 

<--the above doesn't work for both iOS and Android-->

For android, following works:

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

the above doesn't work for both iOS and Android

Firstly, I would use cordova-plugin-device to determine the platform rather than user agent sniffing - it is more robust.

Secondly, it appears you have a function dependent on the deviceready event which is invoked on the successful outcome of a plugin result:

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

Cordova plugins are dynamically loaded before the deviceready event is invoked, so it's likely that the inner function is never invoked.

Thirdly, the success callback function for both cordova.plugins.diagnostic.isLocationEnabled() and cordova.plugins.locationAccuracy.request() appears to be onRequestSuccess() . Both of the former are invoked in serial synchrony but their callbacks are invoked asynchronously, so this is likely to lead to problems.

I would try something like this:

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

In general, I would highly recommend using Safari Remote Debugging and Chrome Remote Debugging tools to debug your app while running on iOS and Android devices respectively. Using the step-through debugger with appropriate breakpoints will highlight issues such as those present in your code snippet. See the Debugging Cordova apps section in the Cordova docs for details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM