onLocationChanged()从来没有所谓的从来没有、onLocationChanged

2023-09-06 03:11:16 作者:感觉男神的身上有wifi

我写一个code,带来当前的位置,但因为它永远不会调用 onLocationChanged()。就是没有找到任何方式,它是不是给我的位置的位置。

mobileLocation即将0这种情况下,因此,执行前进到别的块

我的code是

 公共类FindLocation {
私人LocationManager locManager;
私人LocationListener的locListener;
私人的位置mobileLocation;
私人字符串提供商;

公共FindLocation(上下文CTX){
    locManager =(LocationManager)ctx.getSystemService(Context.LOCATION_SERVICE);
    locListener =新LocationListener的(){
        @覆盖
        公共无效onStatusChanged(字符串商,INT地位,
                捆绑演员){
        }
        @覆盖
        公共无效onProviderEnabled(字符串提供商){
        }
        @覆盖
        公共无效onProviderDisabled(字符串提供商){
        }
        @覆盖
        公共无效onLocationChanged(位置定位){
            的System.out.println(移动的位置是在监听器=+位置);
            mobileLocation =位置;
        }
    };
    locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000,1,locListener);
    如果(mobileLocation!= NULL){
        locManager.removeUpdates(locListener);
        串londitude =Londitude:+ mobileLocation.getLongitude();
        字符串纬度=纬度:+ mobileLocation.getLatitude();
        串altitiude =Altitiude:+ mobileLocation.getAltitude();
        字符串的准确性=精度:+ mobileLocation.getAccuracy();
        字符串时间=时间:+ mobileLocation.getTime();
        Toast.makeText(CTX,纬度=+纬度+经度是=+ londitude,Toast.LENGTH_LONG).show();
    } 其他 {
        的System.out.println(中找到位置4);
        Toast.makeText(CTX,对不起位置没有确定,Toast.LENGTH_LONG).show();
    }
}
   }
 

解决方案

你永远不会得到一个位置,或者仅作为您在您的评论写有时它​​得到的位置,但不是在点击的时候。

可能是您的code比LocationManager,它可能还没有呼吁onLocationChanged快()。改变你的code,以获得最后的已知位置,或者等到locListener被称为:

  locManager.requestLocationUpdates(
  LocationManager.GPS_PROVIDER,1000,1,locListener);
mobileLocation = locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
如果(mobileLocation!= NULL){
 

没有所谓的无路可走,只要你愿意走,踩过的都是路

I am writing a code that brings current location but it is not giving me the location because it never calls onLocationChanged() .Is there any way to find the location.

mobileLocation is coming 0 for this case, so the execution goes to the else block.

My code is

public class FindLocation {
private LocationManager locManager;
private LocationListener locListener;
private Location mobileLocation;
private String provider;

public FindLocation(Context ctx){
    locManager = (LocationManager) ctx.getSystemService(Context.LOCATION_SERVICE);
    locListener = new LocationListener() {
        @Override
        public void onStatusChanged(String provider, int status,
                Bundle extras) {
        }
        @Override
        public void onProviderEnabled(String provider) {
        }
        @Override
        public void onProviderDisabled(String provider) {
        }
        @Override
        public void onLocationChanged(Location location) {
            System.out.println("mobile location is in listener="+location);
            mobileLocation = location;
        }
    };
    locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, locListener);              
    if (mobileLocation != null) {
        locManager.removeUpdates(locListener); 
        String londitude = "Londitude: " + mobileLocation.getLongitude();
        String latitude = "Latitude: " + mobileLocation.getLatitude();
        String altitiude = "Altitiude: " + mobileLocation.getAltitude();
        String accuracy = "Accuracy: " + mobileLocation.getAccuracy();
        String time = "Time: " + mobileLocation.getTime();
        Toast.makeText(ctx, "Latitude is = "+latitude +"Longitude is ="+londitude, Toast.LENGTH_LONG).show();
    } else {
        System.out.println("in find location 4");
        Toast.makeText(ctx, "Sorry location is not determined", Toast.LENGTH_LONG).show();
    }
}
   }

解决方案

Do you never get a location or only as you write in your comment "sometimes it gets location but not at the time of click."?

Might be that your code is faster than LocationManager, which might not yet have called onLocationChanged(). Change your code in order to get the last known location, or wait until your locListener was called:

locManager.requestLocationUpdates(
  LocationManager.GPS_PROVIDER, 1000, 1, locListener);
mobileLocation = locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (mobileLocation != null) {