我正在编写一个需要在某个位置获取您的位置的应用。 我使用离子原生地理定位将其发送到谷歌API以进行反向地理编码。
当我只使用ionic serve
时,它完全像我想要的那样。
但是当我尝试在Android上运行它时,它似乎根本不起作用。
如果我运行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
这是该页面的代码片段。
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;
}
我似乎无法找出它无法正常工作的原因。
编辑:
我的机器人清单:
<?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>
答案 0 :(得分:0)
我认为您使用的是Android 6或更高版本。你必须在运行时请求权限。
安装:
$ ionic cordova plugin add cordova-plugin-android-permissions
$ npm install --save @ionic-native/android-permissions
使用:
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)
);
}
您必须为您需要的每个权限执行此请求。
有关详细信息,请参阅https://ionicframework.com/docs/native/android-permissions/。
在Google的指南中,您可以找到其他信息,如何请求权限和处理用户响应(解释您需要权限的原因,不要求太多,......)
https://developer.android.com/training/permissions/requesting.html
答案 1 :(得分:0)
@ 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)
});
});
}
,控制台输出为:
[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