简体   繁体   English

地理位置离子可在浏览器测试中工作,但不能在android上工作

[英]geolocation ionic working in browser test but not on android

I'm writing an app which needs to get your location at a certain point. 我正在编写一个需要在特定位置获取您的位置的应用程序。 I used the ionic native geolocation for this which is send to the google API for reverse geocoding. 我为此使用了离子本机地理位置,该地理位置已发送到google API进行反向地理编码。

It works completely like I want it to when I just use ionic serve . 当我只使用ionic serve时,它的工作方式完全像我想要的那样。 But when I try to run it on android it doesn't seem to work at all. 但是当我尝试在android上运行它似乎根本不起作用。

if I run ionic cordova run android --device -c -l I get these console logs: 如果我运行ionic cordova run android --device -c -l则会得到以下控制台日志:

[20:23:58] console.log: Device Ready [20:23:58] console.log: [object Object] [20:23:58] console.log: No location Error

this is the snippet of code at that page. 这是该页面上的代码段。

 constructor(public navCtrl: NavController, public navParams: NavParams, private geo: Geolocation, private platform: Platform, public geocodeProvider: GeocodeProvider) { this.platform.ready().then(()=>{ console.log("Device Ready"); this.geo.getCurrentPosition(this.options).then(resp =>{ console.log(resp.coords.latitude); console.log(resp.coords.longitude); this.getCountry(resp.coords.latitude,resp.coords.longitude); }).catch((err)=>{ console.log(err) }); }); } getCountry(lat,long){ this.geocodeProvider.getCountry(lat,long).subscribe(result =>this.country = this.getName(result)); } getName(JSON:IRootObject){ var location = JSON.results[0].address_components[0].long_name; return JSON.results[0].address_components[0].long_name; } 

I can't seem to find out why it isn't working. 我似乎无法找出为什么它不起作用。

EDIT: 编辑:

My android manifest: 我的Android清单:

<?xml version='1.0' encoding='utf-8'?> <manifest android:hardwareAccelerated="true" android:versionCode="1" android:versionName="0.0.1" package="io.ionic.starter" xmlns:android="http://schemas.android.com/apk/res/android"> <supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:resizeable="true" android:smallScreens="true" android:xlargeScreens="true" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-sdk android:minSdkVersion="16" android:targetSdkVersion="26" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" /> <uses-feature android:glEsVersion="0x00020000" android:required="true" /> <uses-feature android:name="android.hardware.location" /> <uses-feature android:name="android.hardware.location.gps" /> <uses-permission android:name="android.permission.CALL_PHONE" /> <uses-feature android:name="android.hardware.telephony" android:required="false" /> <application android:hardwareAccelerated="true" android:icon="@mipmap/icon" android:label="@string/app_name" android:supportsRtl="true"> <activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale" android:label="@string/activity_name" android:launchMode="singleTop" android:name="MainActivity" android:theme="@android:style/Theme.DeviceDefault.NoActionBar" android:windowSoftInputMode="adjustResize"> <intent-filter android:label="@string/launcher_name"> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <meta-data android:name="com.google.android.geo.API_KEY" android:value="***" /> <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> </application> </manifest>

I think you are on Android 6 or above. 我认为您使用的是Android 6或更高版本。 There you have to request permissions at runtime. 在那里,您必须在运行时请求权限。

Install: 安装:

$ ionic cordova plugin add cordova-plugin-android-permissions
$ npm install --save @ionic-native/android-permissions

Use: 采用:

import { AndroidPermissions } from '@ionic-native/android-permissions';


constructor(private androidPermissions: AndroidPermissions) { 
    this.androidPermissions.checkPermission(this.androidPermissions.PERMISSION.LOCATION_COURSE)
     .then(
       result => console.log('Has permission?', result.hasPermission),
       err => this.androidPermissions.requestPermission(this.androidPermissions.PERMISSION.LOCATION_COURSE)
     );
}

You have to do this request for each permission you need. 您需要为每个所需的权限执行此请求。

See https://ionicframework.com/docs/native/android-permissions/ for details. 有关详细信息,请参见https://ionicframework.com/docs/native/android-permissions/

In the Guidelines by Google you find additional infos how to request permissions and handle the user response (explain why you need the permissions, don't ask to much, ...) 在Google指南中,您可以找到有关如何请求权限和处理用户响应的其他信息(解释为什么需要权限,要求不高,...)

https://developer.android.com/training/permissions/requesting.html https://developer.android.com/training/permissions/requesting.html

@fastr.de I added this: @ fastr.de我添加了这个:

 constructor(public navCtrl: NavController, public navParams: NavParams, private geo: Geolocation, private platform: Platform, public geocodeProvider: GeocodeProvider,private androidPermissions: AndroidPermissions) { this.androidPermissions.checkPermission(this.androidPermissions.PERMISSION.ACCESS_FINE_LOCATION).then( result => console.log('Has permission? ACCESS_FINE_LOCATION',result.hasPermission), err => this.androidPermissions.requestPermission(this.androidPermissions.PERMISSION.ACCESS_FINE_LOCATION) ); this.androidPermissions.checkPermission(this.androidPermissions.PERMISSION.ACCESS_LOCATION_EXTRA_COMMANDS).then( result => console.log('Has permission? ACCESS_LOCATION_EXTRA_COMMANDS',result.hasPermission), err => this.androidPermissions.requestPermission(this.androidPermissions.PERMISSION.ACCESS_LOCATION_EXTRA_COMMANDS) ); this.androidPermissions.checkPermission(this.androidPermissions.PERMISSION.ACCESS_COARSE_LOCATION).then( result => console.log('Has permission? ACCESS_COARSE_LOCATION',result.hasPermission), err => this.androidPermissions.requestPermission(this.androidPermissions.PERMISSION.ACCESS_COARSE_LOCATION) ); this.platform.ready().then(()=>{ console.log("Device Ready"); this.androidPermissions.requestPermissions([this.androidPermissions.PERMISSION.ACCESS_COARSE_LOCATION, this.androidPermissions.PERMISSION.ACCESS_FINE_LOCATION,this.androidPermissions.PERMISSION.ACCESS_LOCATION_EXTRA_COMMANDS]); this.geo.getCurrentPosition(this.options).then(resp =>{ console.log(resp.coords.latitude); console.log(resp.coords.longitude); this.getCountry(resp.coords.latitude,resp.coords.longitude); }).catch((err)=>{ console.log(err) }); }); } 

and the console output is: 控制台输出为:

[16:17:15]  console.log: Device Ready
[16:17:15]  console.log: Has permission? ACCESS_LOCATION_EXTRA_COMMANDS true
[16:17:15]  console.log: Has permission? ACCESS_FINE_LOCATION true
[16:17:15]  console.log: Has permission? ACCESS_COARSE_LOCATION true
[16:17:15]  console.log: [object Object]
[16:17:15]  console.log: No location Error

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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