Cordova 地理定位问题-离子2

Cordova 地理定位问题-离子2,cordova,ionic-framework,ionic2,Cordova,Ionic Framework,Ionic2,我试图获取用户的位置(纬度和经度),但由于某些原因,它不起作用。它可以在浏览器中工作,但不能在手机或模拟器中工作。这是我的密码 constructor(public navCtrl: NavController, public _http:Http, public alertCtrl:AlertController, public zone:NgZone, public platform:Platform, public loadingCtrl:LoadingController){

我试图获取用户的位置(纬度和经度),但由于某些原因,它不起作用。它可以在浏览器中工作,但不能在手机或模拟器中工作。这是我的密码

constructor(public navCtrl: NavController, public _http:Http, public alertCtrl:AlertController, public zone:NgZone, public platform:Platform, public loadingCtrl:LoadingController){        
  this.platform.ready().then(() => {
    if(navigator.geolocation){
        console.log('Condition is true');
        Geolocation.getCurrentPosition().then((position) => {
          console.log(position);
          }, (err) => {
            console.error(err);
          });
    }else{
      console.error("Navigator is unavailable");
    }
});}
在控制台中,我得到的
条件为真
,但这是我唯一得到的控制台日志

版本和插件

Cordova版本6.4.0

离子版2.1.4

cordova插件地理定位2.4.0“地理定位”

下面是来自www/index.html的代码

<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
  <meta charset="UTF-8">


  <title>Ionic App</title>
    <meta http-equiv="Content-Security-Policy" content="">

  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
  <meta name="format-detection" content="telephone=no">
  <meta name="msapplication-tap-highlight" content="no">

  <link rel="icon" type="image/x-icon" href="assets/icon/favicon.ico">
  <link rel="manifest" href="manifest.json">
  <meta name="theme-color" content="#4e8ef7">
   <script src="https://maps.google.com/maps/api/js?key=AIzaSyDRu7tdUdnsl1-BbA-l-Qcctwa66LBt8Ww"></script>

  <!-- cordova.js required for cordova apps -->
  <script src="cordova.js"></script>

  <!-- un-comment this code to enable service worker
  <script>
    if ('serviceWorker' in navigator) {
      navigator.serviceWorker.register('service-worker.js')
        .then(() => console.log('service worker installed'))
        .catch(err => console.log('Error', err));
    }
  </script>-->
  <link href="build/main.css" rel="stylesheet">

</head>
<body>

  <!-- Ionic's root component and where the app will load -->
  <ion-app></ion-app>

  <!-- The polyfills js is generated during the build process -->
  <script src="build/polyfills.js"></script>

  <!-- The bundle js is generated during the build process -->
  <script src="build/main.js"></script>

</body>
</html>

离子应用
下面的代码来自config.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<widget id="com.ionicframework.miyabiairapp257144" version="0.0.1" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
  <name>MiyabiAir</name>
  <description>An awesome Ionic/Cordova app.</description>
  <author email="hi@ionicframework" href="http://ionicframework.com/">Ionic Framework Team</author>
  <content src="index.html"/>
  <access origin="*"/>
  <access origin="*"/>
  <access origin="tel:*" launch-external="yes"/>
  <access origin="*"/>
  <allow-navigation href="http://localhost:8000*"/>
  <allow-navigation href="*"/>
  <allow-navigation href="http://*/*"/>
  <allow-navigation href="https://*/*"/>
  <allow-navigation href="data:*"/>
  <allow-intent href="http://*/*"/>
  <allow-intent href="https://*/*"/>
  <allow-intent href="tel:*"/>
  <allow-intent href="sms:*"/>
  <allow-intent href="mailto:*"/>
  <allow-intent href="geo:*"/>
  <allow-intent href="*"/>
  <platform name="android">
    <allow-intent href="market:*"/>
    <icon src="resources\android\icon\drawable-ldpi-icon.png" density="ldpi"/>
    <icon src="resources\android\icon\drawable-mdpi-icon.png" density="mdpi"/>
    <icon src="resources\android\icon\drawable-hdpi-icon.png" density="hdpi"/>
    <icon src="resources\android\icon\drawable-xhdpi-icon.png" density="xhdpi"/>
    <icon src="resources\android\icon\drawable-xxhdpi-icon.png" density="xxhdpi"/>
    <icon src="resources\android\icon\drawable-xxxhdpi-icon.png" density="xxxhdpi"/>
    <splash src="resources\android\splash\drawable-land-ldpi-screen.png" density="land-ldpi"/>
    <splash src="resources\android\splash\drawable-land-mdpi-screen.png" density="land-mdpi"/>
    <splash src="resources\android\splash\drawable-land-hdpi-screen.png" density="land-hdpi"/>
    <splash src="resources\android\splash\drawable-land-xhdpi-screen.png" density="land-xhdpi"/>
    <splash src="resources\android\splash\drawable-land-xxhdpi-screen.png" density="land-xxhdpi"/>
    <splash src="resources\android\splash\drawable-land-xxxhdpi-screen.png" density="land-xxxhdpi"/>
    <splash src="resources\android\splash\drawable-port-ldpi-screen.png" density="port-ldpi"/>
    <splash src="resources\android\splash\drawable-port-mdpi-screen.png" density="port-mdpi"/>
    <splash src="resources\android\splash\drawable-port-hdpi-screen.png" density="port-hdpi"/>
    <splash src="resources\android\splash\drawable-port-xhdpi-screen.png" density="port-xhdpi"/>
    <splash src="resources\android\splash\drawable-port-xxhdpi-screen.png" density="port-xxhdpi"/>
    <splash src="resources\android\splash\drawable-port-xxxhdpi-screen.png" density="port-xxxhdpi"/>
  </platform>
  <platform name="ios">
    <allow-intent href="itms:*"/>
    <allow-intent href="itms-apps:*"/>
  </platform>
  <preference name="webviewbounce" value="false"/>
  <preference name="UIWebViewBounce" value="false"/>
  <preference name="DisallowOverscroll" value="true"/>
  <preference name="android-minSdkVersion" value="16"/>
  <preference name="BackupWebStorage" value="none"/>
  <preference name="SplashMaintainAspectRatio" value="true"/>
  <preference name="FadeSplashScreenDuration" value="300"/>
  <preference name="SplashScreen" value="screen"/>
  <preference name="SplashScreenDelay" value="3000"/>
  <feature name="StatusBar">
    <param name="ios-package" onload="true" value="CDVStatusBar"/>
  </feature>
  <plugin name="cordova-plugin-device" spec="~1.1.3"/>
  <plugin name="cordova-plugin-console" spec="~1.0.4"/>
  <plugin name="cordova-plugin-whitelist" spec="~1.3.0"/>
  <plugin name="cordova-plugin-splashscreen" spec="~4.0.0"/>
  <plugin name="cordova-plugin-statusbar" spec="~2.2.0"/>
  <plugin name="ionic-plugin-keyboard" spec="~2.2.1"/>
  <icon src="resources\android\icon\drawable-xhdpi-icon.png"/>
</widget>

宫崎骏
一款很棒的Ionic/Cordova应用程序。
离子框架小组
提前感谢。

遵循以下步骤:

  • 爱奥尼亚运行安卓系统
  • 近距离GPS
  • 开放式GPS
  • 关闭应用程序
  • 开放应用程序
  • 发生这种情况是因为应用程序处于调试模式,并且与应用程序使用GPS的权限相关。请执行以下步骤:

  • 爱奥尼亚运行安卓系统
  • 近距离GPS
  • 开放式GPS
  • 关闭应用程序
  • 开放应用程序

  • 发生这种情况是因为应用程序处于调试模式,并且与应用程序使用GPS的权限相关。请尝试此代码,它对我有效

    var posOptions = {timeout: 5000, enableHighAccuracy: true};
    $cordovaGeolocation.getCurrentPosition(posOptions).then(function (position) {
      var fromlat = position.coords.latitude;
      var fromlng = position.coords.longitude;
      var latlng = new google.maps.LatLng(fromlat, fromlng);
      var geocoder = new google.maps.Geocoder();
      geocoder.geocode({'latLng': latlng}, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
          console.log(results);
        }
      });
    }, function (err) {
    });
    

    试试这个代码,它对我有用

    var posOptions = {timeout: 5000, enableHighAccuracy: true};
    $cordovaGeolocation.getCurrentPosition(posOptions).then(function (position) {
      var fromlat = position.coords.latitude;
      var fromlng = position.coords.longitude;
      var latlng = new google.maps.LatLng(fromlat, fromlng);
      var geocoder = new google.maps.Geocoder();
      geocoder.geocode({'latLng': latlng}, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
          console.log(results);
        }
      });
    }, function (err) {
    });
    
    这是我用的

    import {Geolocation} from 'ionic-native';
    
    export class someThing {
       data: any;
    
    constructor(public navCtrl: NavController, public alertCtrl: AlertController) {
        Geolocation.getCurrentPosition().then((resp) => {
            let lat = resp.coords.latitude;
            let lng = resp.coords.longitude;
            this.data.lat = lat;
            this.data.lng = lng;
        });
    }
    
    } 你能发布你的index.html文件和config.xml吗?这是我使用的

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <widget id="com.ionicframework.miyabiairapp257144" version="0.0.1" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
      <name>MiyabiAir</name>
      <description>An awesome Ionic/Cordova app.</description>
      <author email="hi@ionicframework" href="http://ionicframework.com/">Ionic Framework Team</author>
      <content src="index.html"/>
      <access origin="*"/>
      <access origin="*"/>
      <access origin="tel:*" launch-external="yes"/>
      <access origin="*"/>
      <allow-navigation href="http://localhost:8000*"/>
      <allow-navigation href="*"/>
      <allow-navigation href="http://*/*"/>
      <allow-navigation href="https://*/*"/>
      <allow-navigation href="data:*"/>
      <allow-intent href="http://*/*"/>
      <allow-intent href="https://*/*"/>
      <allow-intent href="tel:*"/>
      <allow-intent href="sms:*"/>
      <allow-intent href="mailto:*"/>
      <allow-intent href="geo:*"/>
      <allow-intent href="*"/>
      <platform name="android">
        <allow-intent href="market:*"/>
        <icon src="resources\android\icon\drawable-ldpi-icon.png" density="ldpi"/>
        <icon src="resources\android\icon\drawable-mdpi-icon.png" density="mdpi"/>
        <icon src="resources\android\icon\drawable-hdpi-icon.png" density="hdpi"/>
        <icon src="resources\android\icon\drawable-xhdpi-icon.png" density="xhdpi"/>
        <icon src="resources\android\icon\drawable-xxhdpi-icon.png" density="xxhdpi"/>
        <icon src="resources\android\icon\drawable-xxxhdpi-icon.png" density="xxxhdpi"/>
        <splash src="resources\android\splash\drawable-land-ldpi-screen.png" density="land-ldpi"/>
        <splash src="resources\android\splash\drawable-land-mdpi-screen.png" density="land-mdpi"/>
        <splash src="resources\android\splash\drawable-land-hdpi-screen.png" density="land-hdpi"/>
        <splash src="resources\android\splash\drawable-land-xhdpi-screen.png" density="land-xhdpi"/>
        <splash src="resources\android\splash\drawable-land-xxhdpi-screen.png" density="land-xxhdpi"/>
        <splash src="resources\android\splash\drawable-land-xxxhdpi-screen.png" density="land-xxxhdpi"/>
        <splash src="resources\android\splash\drawable-port-ldpi-screen.png" density="port-ldpi"/>
        <splash src="resources\android\splash\drawable-port-mdpi-screen.png" density="port-mdpi"/>
        <splash src="resources\android\splash\drawable-port-hdpi-screen.png" density="port-hdpi"/>
        <splash src="resources\android\splash\drawable-port-xhdpi-screen.png" density="port-xhdpi"/>
        <splash src="resources\android\splash\drawable-port-xxhdpi-screen.png" density="port-xxhdpi"/>
        <splash src="resources\android\splash\drawable-port-xxxhdpi-screen.png" density="port-xxxhdpi"/>
      </platform>
      <platform name="ios">
        <allow-intent href="itms:*"/>
        <allow-intent href="itms-apps:*"/>
      </platform>
      <preference name="webviewbounce" value="false"/>
      <preference name="UIWebViewBounce" value="false"/>
      <preference name="DisallowOverscroll" value="true"/>
      <preference name="android-minSdkVersion" value="16"/>
      <preference name="BackupWebStorage" value="none"/>
      <preference name="SplashMaintainAspectRatio" value="true"/>
      <preference name="FadeSplashScreenDuration" value="300"/>
      <preference name="SplashScreen" value="screen"/>
      <preference name="SplashScreenDelay" value="3000"/>
      <feature name="StatusBar">
        <param name="ios-package" onload="true" value="CDVStatusBar"/>
      </feature>
      <plugin name="cordova-plugin-device" spec="~1.1.3"/>
      <plugin name="cordova-plugin-console" spec="~1.0.4"/>
      <plugin name="cordova-plugin-whitelist" spec="~1.3.0"/>
      <plugin name="cordova-plugin-splashscreen" spec="~4.0.0"/>
      <plugin name="cordova-plugin-statusbar" spec="~2.2.0"/>
      <plugin name="ionic-plugin-keyboard" spec="~2.2.1"/>
      <icon src="resources\android\icon\drawable-xhdpi-icon.png"/>
    </widget>
    
    import {Geolocation} from 'ionic-native';
    
    export class someThing {
       data: any;
    
    constructor(public navCtrl: NavController, public alertCtrl: AlertController) {
        Geolocation.getCurrentPosition().then((resp) => {
            let lat = resp.coords.latitude;
            let lng = resp.coords.longitude;
            this.data.lat = lat;
            this.data.lng = lng;
        });
    }
    
    }
    你能发布你的index.html文件和config.xml吗?我做了下面的代码,你可以在我的github中找到一个例子

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <widget id="com.ionicframework.miyabiairapp257144" version="0.0.1" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
      <name>MiyabiAir</name>
      <description>An awesome Ionic/Cordova app.</description>
      <author email="hi@ionicframework" href="http://ionicframework.com/">Ionic Framework Team</author>
      <content src="index.html"/>
      <access origin="*"/>
      <access origin="*"/>
      <access origin="tel:*" launch-external="yes"/>
      <access origin="*"/>
      <allow-navigation href="http://localhost:8000*"/>
      <allow-navigation href="*"/>
      <allow-navigation href="http://*/*"/>
      <allow-navigation href="https://*/*"/>
      <allow-navigation href="data:*"/>
      <allow-intent href="http://*/*"/>
      <allow-intent href="https://*/*"/>
      <allow-intent href="tel:*"/>
      <allow-intent href="sms:*"/>
      <allow-intent href="mailto:*"/>
      <allow-intent href="geo:*"/>
      <allow-intent href="*"/>
      <platform name="android">
        <allow-intent href="market:*"/>
        <icon src="resources\android\icon\drawable-ldpi-icon.png" density="ldpi"/>
        <icon src="resources\android\icon\drawable-mdpi-icon.png" density="mdpi"/>
        <icon src="resources\android\icon\drawable-hdpi-icon.png" density="hdpi"/>
        <icon src="resources\android\icon\drawable-xhdpi-icon.png" density="xhdpi"/>
        <icon src="resources\android\icon\drawable-xxhdpi-icon.png" density="xxhdpi"/>
        <icon src="resources\android\icon\drawable-xxxhdpi-icon.png" density="xxxhdpi"/>
        <splash src="resources\android\splash\drawable-land-ldpi-screen.png" density="land-ldpi"/>
        <splash src="resources\android\splash\drawable-land-mdpi-screen.png" density="land-mdpi"/>
        <splash src="resources\android\splash\drawable-land-hdpi-screen.png" density="land-hdpi"/>
        <splash src="resources\android\splash\drawable-land-xhdpi-screen.png" density="land-xhdpi"/>
        <splash src="resources\android\splash\drawable-land-xxhdpi-screen.png" density="land-xxhdpi"/>
        <splash src="resources\android\splash\drawable-land-xxxhdpi-screen.png" density="land-xxxhdpi"/>
        <splash src="resources\android\splash\drawable-port-ldpi-screen.png" density="port-ldpi"/>
        <splash src="resources\android\splash\drawable-port-mdpi-screen.png" density="port-mdpi"/>
        <splash src="resources\android\splash\drawable-port-hdpi-screen.png" density="port-hdpi"/>
        <splash src="resources\android\splash\drawable-port-xhdpi-screen.png" density="port-xhdpi"/>
        <splash src="resources\android\splash\drawable-port-xxhdpi-screen.png" density="port-xxhdpi"/>
        <splash src="resources\android\splash\drawable-port-xxxhdpi-screen.png" density="port-xxxhdpi"/>
      </platform>
      <platform name="ios">
        <allow-intent href="itms:*"/>
        <allow-intent href="itms-apps:*"/>
      </platform>
      <preference name="webviewbounce" value="false"/>
      <preference name="UIWebViewBounce" value="false"/>
      <preference name="DisallowOverscroll" value="true"/>
      <preference name="android-minSdkVersion" value="16"/>
      <preference name="BackupWebStorage" value="none"/>
      <preference name="SplashMaintainAspectRatio" value="true"/>
      <preference name="FadeSplashScreenDuration" value="300"/>
      <preference name="SplashScreen" value="screen"/>
      <preference name="SplashScreenDelay" value="3000"/>
      <feature name="StatusBar">
        <param name="ios-package" onload="true" value="CDVStatusBar"/>
      </feature>
      <plugin name="cordova-plugin-device" spec="~1.1.3"/>
      <plugin name="cordova-plugin-console" spec="~1.0.4"/>
      <plugin name="cordova-plugin-whitelist" spec="~1.3.0"/>
      <plugin name="cordova-plugin-splashscreen" spec="~4.0.0"/>
      <plugin name="cordova-plugin-statusbar" spec="~2.2.0"/>
      <plugin name="ionic-plugin-keyboard" spec="~2.2.1"/>
      <icon src="resources\android\icon\drawable-xhdpi-icon.png"/>
    </widget>
    
    也许你忘了选择

      // Foreground Tracking
    foreGroundOptions = {
        frequency: 2000,
        enableHighAccuracy: true,
        maximumAge: Infinity,
        timeout: 2000
    };
    

    我做了下面的代码,你可以在我的github中找到一个例子

    也许你忘了选择

      // Foreground Tracking
    foreGroundOptions = {
        frequency: 2000,
        enableHighAccuracy: true,
        maximumAge: Infinity,
        timeout: 2000
    };
    

    是的,先生。它位于顶部
    import{Geolocation,NativeStorage}来自“ionic native”谢谢你的留言。是的,我做了那个,但它仍然不工作。它是Xolo的,Android版本为4.4.2,用于emulator。我正在使用Android SDK构建,用于x86,Android版本为7.1.1。是的,先生。它位于顶部
    import{Geolocation,NativeStorage}来自“ionic native”谢谢你的留言。是的,我做了那个,但仍然不起作用。它是Xolo,Android版本4.4.2,用于emulator我正在使用Android SDK build for x86,Android版本7.1.1谢谢你的回答,但它不起作用。我在emulator和不同的物理设备上进行了尝试,但仍然不起作用。谢谢您的回答,但它不起作用。我在emulator和不同的物理设备上进行了尝试,但仍然无法工作。谢谢,我将
    $cordovaGeolocation
    替换为
    Geolocation
    ,并尝试了,但仍然无法工作,但出现超时错误。然后,我从对象中删除了超时,但它仍然不起作用。尝试执行此操作。是否在设备中运行项目?谢谢,我将
    $cordovaGeolocation
    替换为
    Geolocation
    ,并尝试了此操作,但它不起作用,但我得到了超时错误。然后我从对象中删除了超时,但它仍然不工作。请尝试按照此操作。您是否在设备中运行项目?谢谢您的回答。这与其他人的建议和我的尝试完全相同,请再次检查我的问题。我已经包括了www/index.html文件和config.xml文件。再次感谢!谢谢你的回答。这与其他人的建议和我的尝试完全相同,请再次检查我的问题。我已经包括了www/index.html文件和config.xml文件。再次感谢!没用。看起来这是一个bug或者什么东西启动了一个新项目,安装了地理定位插件,并试图访问lat lng,但仍然不起作用。看起来这是一个bug或者什么东西启动了一个新项目,安装了地理定位插件,并试图访问lat lng,但仍然不起作用