Android的 - 麻烦越来越位置,只采用GPS提供的坐标 [英] Android - Trouble in getting location coordinates by only using GPS provider
问题描述
我是新来的机器人。所以,请原谅,如果我的问题很简单,帮我。
I am new to android. So please apologize if my question is simple and help me.
我开发一个Android应用程序中,我试图让用户位置的仅在使用GPS服务(正如我开发一个应用程序需要在Android设备甚至是没有互联网运行)。
I am developing an android app in which I try to get users location only using GPS service (As I am developing an app which needs to run in android devices even with no internet).
我的code为如下:
我的活动:
public class MyView extends Activity implements OnClickListener, Runnable
{
LocationManager itsLocationManager;
LocationListener itsLocationListener;
@Override
protected void onCreate(Bundle savedInstanceState)
{
itsLocationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
itsLocationListener = new MyLocationListener(this, itsLocationManager);
getLocationAndSendMessage();
}
private void getLocationAndSendMessage()
{
try
{
itsLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, itsLocationListener);
}
catch (Exception theException)
{
theException.printStackTrace();
ToastMsgUtil.showErrorMessage("Problem in retrieving your current location! Please try again.", this);
}
}
MyLocationListener.java:
public class MyLocationListener implements LocationListener
{
Context itsContext;
LocationManager itsLocationManager;
public MyLocationListener(Context theContext, LocationManager theLocationManager)
{
itsContext = theContext;
itsLocationManager = theLocationManager;
}
@Override
public void onLocationChanged(final Location theLocation)
{
//My Location processing code
itsLocationManager.removeUpdates(SafemateLocationListener.this);
}
}
AndroidManifest.xml中
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
正如我前面所说,我只需要通过GPS获得的位置(如在用户手机没有网络)。
As I said above, I need to get location only through GPS (as no internet in users mobile).
- 在调试时,我发现我的没有收到任何位置更新在 onLocationChanged 方式,当我使用的 LocationManager.GPS_PROVIDER 。
- 然而,当我试着用 LocationManager.NETWORK_PROVIDER ,我收到位置更新。
- On debugging, I found that I am not receiving any location updates in onLocationChanged method when I am using LocationManager.GPS_PROVIDER.
- However, when I tried with LocationManager.NETWORK_PROVIDER, I receive location updates.
任何人都可以请说我做错了什么,在上述code?我错过了什么东西?
Can anyone please say what I did wrong in the above code? Have I missed any thing?
请帮助。谢谢你。
推荐答案
获取使用在一个稳定的全球定位系统,工作和兼容的方式是一个烂摊子正确的坐标。我记得有一个帖子在这里某处stackoverflow.com,但无法找到它的时刻。所以,我建议采取GPS相关code OUT的<一个href="http://sourceforge.net/p/libwlocate/$c$c/ci/641b5fa038398364c7fe3fc996744ae094f0cc49/tree/master/android/LocDemo/src/com/vwp/libwlocate/WLocate.java"相对=nofollow> WLocate.java 该工程,并使用所有已知的Android版本。或者使用 libwlocate ,它封装了你要寻找的功能。
Getting correct coordinates using GPS in a stable, working and compatible way is a mess. I remember a posting somewhere here at stackoverflow.com but can't find it at the moment. So I'd suggest to take the GPS-related code out of WLocate.java which works using all known Android versions. Or use the libwlocate, it encapsulates the functionality you're looking for.
这篇关于Android的 - 麻烦越来越位置,只采用GPS提供的坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!