当前位置: 首页 > 面试题库 >

从LocationListener中设置为double

林烨华
2023-03-14
问题内容

看来我有某种奇怪的变量设置问题。我有一个LocationListener,可以正确更新并正确设置当前位置。我尝试设置包含当前经度/纬度的全局变量,但它有点失败(在我发布一些代码之后,它会更多)。

我有一个片段

public class MyMap extends MapFragment {
    private LocationManager mLocManager;
    private LocationListener myLocationListener;
    protected volatile double lat;//Place to hold variable
    protected volatile double lon;//Place to hold variable

    public void onActivityCreated(Bundled savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this.getActivity().getApplicationContext());
    if (resultCode == ConnectionResult.SUCCESS){

        if(gmap == null){
            gmap = this.getMap();
            if(gmap != null){
                gmap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
                gmap.setOnMarkerDragListener(this);
                gmap.setMyLocationEnabled(true);
                gmap.setOnMapLongClickListener(this);
            }else{
                return;
            }
        }
    }else{
        GooglePlayServicesUtil.getErrorDialog(resultCode, this.getActivity(), RQS_GooglePlayServices);
    }
    distance = 100;
    myLocationListener = new MyLocationListener();
    mLocManager = (LocationManager) this.getActivity().getSystemService(Context.LOCATION_SERVICE);
}

@Override
public void onResume() {
    super.onResume();
    mLocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 500,10, myLocationListener);
    mLocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 500, 10, myLocationListener);
    mLocManager.getLastKnownLocation(Context.LOCATION_SERVICE);

}

@Override
public void onPause() {
    super.onPause();
    mLocManager.removeUpdates(myLocationListener);
}

//Listen for location changes and update the map with that information
private class MyLocationListener implements LocationListener {

    @Override
    public void onLocationChanged(Location location) {
        if (Math.abs(location.getLatitude()) > 0.0 && Math.abs(location.getLongitude()) > 0.0) {
            lat = location.getLatitude();
            lon = location.getLongitude();
        }
        //This reports correctly
        Log.v("Magic", "LOCATION CHANGED!" + "\nLat: " + Double.toString(lat) + "\nLon: " +  Double.toString(lon));
        if (!currentLocationSet) {
            gmap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lon), 15));
            currentLocationSet = true;
        }
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onProviderDisabled(String provider) {

    }
}
public void newPlace(final UpdatePlacesCallBack upc, Place p) {
    //This gives me 0.0 for both variables.
    Log.v("Magic", "Name: " + p.getName() + "\nLat: " + Double.toString(lat) + "\nLon:" + Double.toString(lon));
}

}

因此,发生的是,当我从LocationListener中看到日志时,它将记录正确的位置,但是如果我尝试检索double lat和double
lon的值,则两个变量都返回0.0。


问题答案:
// try this
public static LocationManager mlocManager;
public static LocationListner mListner;
private static String latitude;
public static String longitude;
private Location currentLocation;

public Location getCurrentLocation() {
  return currentLocation;
}

public void setCurrentLocation(Location currentLocation) {
   this.currentLocation = currentLocation;
}

try {
mlocManager = (LocationManager) getSystemService(Activity.LOCATION_SERVICE);
                mListner = new LocationListner();
                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        try {
                        try {
                            mlocManager.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER, 0, 0, mListner);
                        } catch (Throwable e) {
                            e.printStackTrace();
                        }

                            mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mListner);
                        } catch (Throwable e) {
                            e.printStackTrace();
                        }
                        try {
                            mlocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, mListner);
                        } catch (Throwable e) {
                            e.printStackTrace();
                        }

                    }
                });
} catch (Throwable e) {
    e.printStackTrace();
}

    public String getLatitude() {
        if (latitude != null) {
            return latitude;
        }
        Location loc = mlocManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if (loc == null) {
            loc = mlocManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
            if (loc == null) {
                loc = mlocManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
            }
            if (loc != null) {
                return "" + loc.getLatitude();
            }
        } else {
            return "" + loc.getLatitude();
        }
        return "0.0";
    }

    public void setLatitude(String latitide) {
        latitude = latitide;
    }

    public String getLongitude() {
        if (longitude != null) {
            return longitude;
        }
        Location loc = mlocManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if (loc == null) {
            loc = mlocManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
            if (loc == null) {
                loc = mlocManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
            }
            if (loc != null) {
                return "" + loc.getLongitude();
            }
        } else {
            return "" + loc.getLongitude();
        }
        return "0.0";
    }

    public void setLongitude(String longitude) {
        SmartActivity.longitude = longitude;
    }

    class LocationListner implements LocationListener {

        @Override
        public void onLocationChanged(Location location) {
            setLatitude("" + location.getLatitude());
            setLongitude("" + location.getLongitude());
            setCurrentLocation(location);
        }

        @Override
        public void onProviderDisabled(String provider) {
        }

        @Override
        public void onProviderEnabled(String provider) {
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
        }

    }


 类似资料:
  • 13.2.1.2.作为LocationListener的Activity 这就是我们唯一的Activity。我们将在这里初始化界面,连接到LocationManager,然后使用Geocoder检索我们的地理地址。LocationManager可以通过不同的位置信息源(比如GPS或者网络)来获得我们的当前位置,以经纬度的格式返回。Geocoder以此检索在线的位置数据库,即可获得当前位置的地理地址

  • 这一行是: 我在和上尝试了和,但仍然给出相同的错误

  • 问题内容: 我有一个bash.sh脚本: 其中config.json: 当我运行它时,我得到: ERR未知命令’}’ 如何从json文件正确设置json值? 问题答案: 如果你正在尝试设置 字符串 的值,关键看你的JSON文件的内容(或其他任何与此有关的,包括二进制),最简单的方法是使用选项来 阅读的最后一个参数 逐字的命令, 从。例如: 对于您的示例,它将存储: 要存储JSON数据的紧凑表示形式

  • 我正在使用ReactiveLocation库。基本上,我想在4秒内获得一个足够准确的位置。如果在此期间未收到足够精度的位置,但其他位置已返回最高精度的位置。 一旦接收到足够准确的位置,则返回该位置并完成可观测。 我会发布我正在尝试的代码。我可能,也可能,以错误的方式进行。

  • 问题内容: 我有一个Redis设置键’a’和值‘1’,‘2’,‘3’。是否可以为集合中的每个键值对设置不同的到期时间。 例如,(’a’,‘1’)应该在60秒后过期,而as(’a’,‘2’)应该在120秒后过期。 问题答案: 抱歉不行。Redis的“容器”(即列表,哈希,集合和排序集合)不支持按成员过期,尽管过去多次要求使用此功能。 但是,您可以实现自己的逻辑以实现该结果。解决此问题的方法有几种-

  • 问题内容: 我可以如下设置Firefox的代理设置。 但是我也需要设置Chrome。.有人可以帮助我怎么做吗? 谢谢拉吉 问题答案: 您可以尝试使用该类,如下所示: