I am using the native Ionic 2 Geolocation plugin and it does not work as expected. First, I add the plugin.
我使用的是原生的Ionic 2 Geolocation插件,它无法按预期工作。首先,我添加插件。
ionic plugin add cordova-plugin-geolocation
I then import the plugin and issue a call to get the current position.
然后我导入插件并发出调用以获取当前位置。
import { Geolocation } from 'ionic-native';
import { Geoposition, GeolocationOptions } from 'ionic-native/dist/plugins/geolocation';
export class MyComponent {
protected getLocation():Promise<Geoposition> {
let options:GeolocationOptions = {
maximumAge: 0, timeout: 5000, enableHighAccuracy: false
};
return Geolocation.getCurrentPosition(options)
.catch(error => { console.error(error.message) };
}
}
I've also checked my AndroidManifest.xml.
我还检查了我的AndroidManifest.xml。
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
I noticed that the timeout is always exceeded. If I leave it to the default (timeout is set to infinity by default), then around 20 minutes later, I do actually get a geo position. Of course, 20 minutes is not ideal.
我注意到总是超时超时。如果我把它保留为默认值(默认情况下超时设置为无穷大),那么大约20分钟后,我确实得到了一个地理位置。当然,20分钟并不理想。
I also tried using the navigator
as follows. But I get the exact same results.
我也尝试使用导航器如下。但我得到了完全相同的结果。
navigator.geolocation.getCurrentPosition(
position => {
console.info('using navigator');
console.info(position.coords.latitude);
console.info(position.coords.longitude);
},
error => {
console.warn('using navigator');
console.warn(error.code);
console.warn(error.message);
},
options
);
When testing on Chrome
, the code works as expected; the only difference is that I get a popup asking if I want to allow the page to access my location.
在Chrome上进行测试时,代码按预期工作;唯一的区别是我得到一个弹出窗口,询问我是否要允许该页面访问我的位置。
I saw a lot of people have the same problem on SO with Ionic too, but that is with version 1. I wanted to ask this question in the context of Ionic 2.
我看到很多人在Ionic上也有同样的问题,但也就是版本1.我想在Ionic 2的背景下提出这个问题。
By the way, I've also updated Cordova and the version is showing up as 6.0.2.
顺便说一句,我也更新了Cordova,版本显示为6.0.2。
npm update -g cordova
Version information
版本信息
- Ionic 2.0.0-beta.30
- Ionic 2.0.0-beta.30
- Cordova 6.0.2
- 科尔多瓦6.0.2
- Gulp CLI 1.2.1 and Local 3.9.1
- Gulp CLI 1.2.1和本地3.9.1
4 个解决方案
#1
4
Use this code:
使用此代码:
if (navigator.geolocation) {
var options = {
enableHighAccuracy: true
};
navigator.geolocation.getCurrentPosition(position=> {
console.info('using navigator');
console.info(position.coords.latitude);
console.info(position.coords.longitude);
}, error => {
console.log(error);
}, options);
}
Remember to import Geolocation
:
记得导入Geolocation:
import { Geolocation } from 'ionic-native';
And add plugin:
并添加插件:
ionic plugin add cordova-plugin-geolocation
#2
1
Probably I found a solution. The problem was with mine GPS module. Eventually it stopped find my location at any site. I reloaded my phone and now everything works.
可能我找到了解决方案。问题出在我的GPS模块上。最终它停止在任何网站找到我的位置。我重装了手机,现在一切正常。
So at first check if you can determine your location on site such maps.google.com. If it can't determine your location, then the problem is with your phone. Try to reload it or reflash it.
因此,首先请检查您是否可以在网站上确定您的位置,例如maps.google.com。如果无法确定您的位置,则问题出在手机上。尝试重新加载或重新刷新它。
#3
1
Install both cordova dependencies
安装两个cordova依赖项
cordova plugin add cordova-plugin-geolocation
cordova plugin add cordova-plugin-whitelist
Add this permissions to AndroidMainfest.xml
将此权限添加到AndroidMainfest.xml
<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" />
Keep in mind, when running ionic cordova run android -l geolocation will throw this unsecure error. Test it on ur browser or do a simple run with
请记住,当运行离子cordova运行android -l geolocation会抛出这个不安全的错误。在你的浏览器上测试它或用它做一个简单的运行
ionic cordova run adnroid
It solved the problem for me, hope it helps someone else.
它解决了我的问题,希望它能帮助别人。
#4
0
This is usually caused whey you have added only the ionic plugin and forgot to add the cordova plugin(not sure why it's not installed by ionic plugin add command):
这通常是因为你只添加了离子插件而忘记添加cordova插件(不确定为什么它没有通过ionic plugin add命令安装):
ionic plugin add cordova-plugin-geolocation
and then:
接着:
cordova plugin add cordova-plugin-geolocation
installing the cordova plugin can fix the error in some situations.
安装cordova插件可以在某些情况下修复错误。
This also happens when running the application on browser with ionic serve
as cordova plugins are not available.
当使用离子服务器在浏览器上运行应用程序时,也会发生这种情况,因为无法使用cordova插件。
#1
4
Use this code:
使用此代码:
if (navigator.geolocation) {
var options = {
enableHighAccuracy: true
};
navigator.geolocation.getCurrentPosition(position=> {
console.info('using navigator');
console.info(position.coords.latitude);
console.info(position.coords.longitude);
}, error => {
console.log(error);
}, options);
}
Remember to import Geolocation
:
记得导入Geolocation:
import { Geolocation } from 'ionic-native';
And add plugin:
并添加插件:
ionic plugin add cordova-plugin-geolocation
#2
1
Probably I found a solution. The problem was with mine GPS module. Eventually it stopped find my location at any site. I reloaded my phone and now everything works.
可能我找到了解决方案。问题出在我的GPS模块上。最终它停止在任何网站找到我的位置。我重装了手机,现在一切正常。
So at first check if you can determine your location on site such maps.google.com. If it can't determine your location, then the problem is with your phone. Try to reload it or reflash it.
因此,首先请检查您是否可以在网站上确定您的位置,例如maps.google.com。如果无法确定您的位置,则问题出在手机上。尝试重新加载或重新刷新它。
#3
1
Install both cordova dependencies
安装两个cordova依赖项
cordova plugin add cordova-plugin-geolocation
cordova plugin add cordova-plugin-whitelist
Add this permissions to AndroidMainfest.xml
将此权限添加到AndroidMainfest.xml
<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" />
Keep in mind, when running ionic cordova run android -l geolocation will throw this unsecure error. Test it on ur browser or do a simple run with
请记住,当运行离子cordova运行android -l geolocation会抛出这个不安全的错误。在你的浏览器上测试它或用它做一个简单的运行
ionic cordova run adnroid
It solved the problem for me, hope it helps someone else.
它解决了我的问题,希望它能帮助别人。
#4
0
This is usually caused whey you have added only the ionic plugin and forgot to add the cordova plugin(not sure why it's not installed by ionic plugin add command):
这通常是因为你只添加了离子插件而忘记添加cordova插件(不确定为什么它没有通过ionic plugin add命令安装):
ionic plugin add cordova-plugin-geolocation
and then:
接着:
cordova plugin add cordova-plugin-geolocation
installing the cordova plugin can fix the error in some situations.
安装cordova插件可以在某些情况下修复错误。
This also happens when running the application on browser with ionic serve
as cordova plugins are not available.
当使用离子服务器在浏览器上运行应用程序时,也会发生这种情况,因为无法使用cordova插件。