位置并不总是出现 [英] Location not always appearing

查看:239
本文介绍了位置并不总是出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图打通LocationManager.NETWORK_PROVIDER用户的位置,但它始终返回一个空字符串。只是有时我得到的有效数据。有什么办法让这个方法重新迭代,直到它给了我一个位置?我想那个位置就赶快来,就没有必要为这个方法。我试图重新调用它,直到它不会显示一个空字符串,但没有出现。

I am trying to get the location of a user through LocationManager.NETWORK_PROVIDER but it always returns a blank string. Only sometimes I get the valid data. Is there any way to make this method re-iterate until it gives me a location? I would think that location would come quickly and there would be no need for this method. I have tried re-calling it until it doesn't display a blank string, but nothing comes up.

下面是我所说的方法

   public String getLocation(Locale locale, Context context, boolean city, boolean postal, boolean state_prov) throws IOException{
       LocationManager locMan = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
       LocationListener locList = new MyLocList();
       Geocoder gC = new Geocoder(context,locale);
       Location gpsLocation = locMan.getLastKnownLocation(locMan.GPS_PROVIDER);
       locMan.requestLocationUpdates(locMan.NETWORK_PROVIDER, 500, 200, locList);
       Location networkLocation = locMan.getLastKnownLocation(locMan.NETWORK_PROVIDER);
       if (city)
           return (gC.getFromLocation(networkLocation.getLatitude(), networkLocation.getLongitude(), 1).get(0).getSubAdminArea());
       else if (postal)
           return (gC.getFromLocation(networkLocation.getLatitude(), networkLocation.getLongitude(), 1).get(0).getPostalCode());
       else if (state_prov)
           return (gC.getFromLocation(networkLocation.getLatitude(), networkLocation.getLongitude(), 1).get(0).getAdminArea());
       else
           return "Nothing";
    }

我通常会问的城市或邮政code,但大部分时间都谈到了空。使用

I usually ask for the city or postal code, but it most of the time it comes up empty. Using the

    Location gpsLocation = locMan.getLastKnownLocation(locMan.GPS_PROVIDER);

导致了空指针,所以我现在用的是NETWORK_PROVIDER

results in a NullPointer, so I am using the NETWORK_PROVIDER

我已经设置这些权限:

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>

这就是我所说的它

And this is what I call it with

    String data = getLocation(Locale.getDefault(),getBaseContext(),true,false,false);

我一直得到一个空字符串作为结果。

I keep on getting a blank string as the result.

我问了一下没有收到城前一个问题,但现在看来没有数据就要来了,如果是这样,它不能始终保持可用。

I asked a question before about not receiving the city, but now it seems that no data is coming, and if it is, its not consistently available.

推荐答案

在您的code,你所要求的最后已知位置,而不是当前位置。其他一些应用程序要求的位置之前,你所得到的是相同的结果的应用程序。如果启动了应用程序之前是位置没有要求,最后已知位置将是空的。

In your code, you are asking for "last known location", not for "current location". Some other application asked for location before and you are getting the same result as that application. If there was no request for location prior to launching your application, "last known location" will be null.

正确的方法是注册听众和等待位置更新的到来。然后将结果保存为最后已知位置。

Correct way is to register listeners and wait for location update to arrive. The result is then stored as "last known location".

这答案应该帮助你:什么是让Android中用户的当前位置的最简单,最可靠的方法

编辑:为了澄清,这里是上面的链接提到MyLocation类的正确用法。你不能有位置立即,而不是把你的code回调方法,它会被调用时,位置数据变为可用。

To clarify, here is proper usage of MyLocation class mentioned in the link above. You can't have the location immediately, instead place your code in the callback method and it will get called when the location data become available.

LocationResult locationResult = new LocationResult(){
    @Override
    public void gotLocation(Location location){
        //Got the location!
        //Here goes your code
        //This method will be called when location data arrive
    }
};
MyLocation myLocation = new MyLocation();
myLocation.getLocation(this, locationResult);

这篇关于位置并不总是出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆