安卓LocationManager.getLastKnownLocation()返回在Android 4.1.1空getLastKnownLocation、LocationManager、Androi

2023-09-04 07:34:59 作者:快刀斩乱麻

我工作的android应用。我陷在问题。 getLastKnownLocation(提供者)返回为Android 4.1.1版本,而其他版本中,它是罚款。供应商已启用,其余的就可以了。我不知道问题出在哪里。这里是code。

  LocationManager locationManager =(LocationManager)getSystemService(LOCATION_SERVICE);
    标准标准=新标准();
    字符串商= locationManager.getBestProvider(标准,真正的);
    位置= locationManager.getLastKnownLocation(供应商);
    双纬度= location.getLatitude();
    双经度= location.getLongitude();
    经纬度经纬度=新的经纬度(纬度,经度);
    mMap.clear();
    的System.out.println(当前位置=+经纬度);
 

解决方案

试试这个它的工作对我来说...

  mGoogleMap.setOnMyLocationButtonClickListener(新OnMyLocationButtonClickListener(){

                    @覆盖
                    公共布尔onMyLocationButtonClick()
                        {

                            位置myLocation = mGoogleMap.getMyLocation();
                            onLocationChanged(myLocation);
                            返回false;
                        }
                });
 

android studio 使用Mocklocation虚拟定位总结

I am working on android application. I am stuck in problem. getLastKnownLocation(provider) returns null for Android version 4.1.1, while for other versions it is fine. Provider is enabled and the rest is ok. I don't know where is the problem. Here is the code.

LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    String provider = locationManager.getBestProvider(criteria, true);
    location = locationManager.getLastKnownLocation(provider);
    double latitude = location.getLatitude();
    double longitude = location.getLongitude();
    LatLng latLng = new LatLng(latitude, longitude);
    mMap.clear();
    System.out.println("Current Location = "+latLng); 

解决方案

try this its working for me...

  mGoogleMap.setOnMyLocationButtonClickListener(new OnMyLocationButtonClickListener() {

                    @Override
                    public boolean onMyLocationButtonClick()
                        {

                            Location myLocation = mGoogleMap.getMyLocation();
                            onLocationChanged(myLocation);
                            return false;
                        }
                });