2016-11-25 71 views
1

我想獲取用戶的位置(經度和緯度)但由於某種原因它不起作用。它適用於瀏覽器,但不適用於手機或模擬器。這是我的代碼。地理位置問題 - 離子2

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"); 
    } 
});} 

在控制檯我得到Condition is true但這是唯一的控制檯日誌我什麼也沒有得到。

版本和插件

科爾多瓦版本6.4.0

離子2.1.4版

科爾多瓦,插件,地理位置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="[email protected]" 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> 

在此先感謝。

+0

是的,先生。它位於'ionic-native'的import {Geolocation,NativeStorage}頂部;' –

+0

感謝您的留言。是的,我做了那一個,但仍然無效。 –

+0

其Xolo與Android版本4.4.2模擬器我正在使用Android SDK與x86版本與Android版本7.1.1 –

回答

0

遵循的步驟:

  1. 離子運行的Android
  2. 關閉GPS
  3. 打開GPS
  4. 關閉App
  5. 打開應用

這是因爲應用程序在調試模式及其相關的應用程序的權限使用GPS

+0

謝謝你的答案,但它沒有奏效。我嘗試了仿真器和不同的物理設備,但仍然無法正常工作。 –

0

試試這段代碼對我有用。

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) { 
}); 
+0

謝謝,我用'Geolocation'替換了'$ cordovaGeolocation'並試過了,它不起作用,但我得到了超時錯誤。然後我從對象中移除了超時值,但仍然不起作用。 –

+0

嘗試遵循此http:// ngcordova。com/docs/plugins/geolocation/ 你在設備上運行你的項目嗎? – Agapitz

0

這就是我使用的。

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

+0

謝謝你的回答。這與其他人的建議和我所嘗試的完全相同,請再次檢查我的問題。我已經包含了www/index.html文件和config.xml文件。再次感謝! –

0

我的代碼講座敬愛的,你可以在我的github上找到一個例子https://github.com/prolland006/ionic2-geoloc/blob/master/src/services/background-geolocation-service.ts

Geolocation.getCurrentPosition(this.foreGroundOptions) 
     .then((position: Geoposition) => { 
      this.trace.info(`Foregrnd current ${position.coords.latitude},${position.coords.longitude}`); 
     }).catch((error)=>{ 
      if ((error.code == undefined) || (error.code != 3)) { //3=timeout 
       this.trace.error('BackgroundGeolocationService', 'refreshLocations', `error Foregrnd.getCurrentPos:${error.toString()}`); 
      } 
     }); 

也許你忘了選項

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

沒有工作。看起來像它的一個bug或什麼東西開始新項目和安裝地理位置插件,並試圖訪問經緯度,但仍然無法正常工作 –