有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Google play服务查找我的位置很慢

我在我的安卓应用程序中添加了GooglePlayAPI来获取我的当前位置,因为我需要在我的项目中使用它

在添加必要的代码以获取我的当前位置后,它工作正常,但速度较慢,因此在单击按钮获取我的位置后,它会在20-25秒后做出响应

有没有办法加快速度

这是我的密码:

   ....
   LocationManager myManager;
    MyLocListener loc;
    loc = new MyLocListener();
    myManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
    if (ActivityCompat.checkSelfPermission(this, 安卓.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, 安卓.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.
        return;
    }
    myManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, loc);
}

public class MyLocListener implements LocationListener {

    @Override
    public void onLocationChanged(Location location)
    {
        if(location != null)
        {
            // it logs after 20-25 s
            Log.d("Latitude :", "" + location.getLatitude());
            Log.d("Latitude :", "" + location.getLongitude());
        }
    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onProviderDisabled(String provider) {
    }

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

}

共 (1) 个答案

  1. # 1 楼答案

    使用GPS_PROVIDER需要严格的GPS定位,通常需要10-30秒,具体取决于硬件

    您应该考虑使用FusedLocationProviderApi,如Receiving location updates training page,它结合了来自WiFi、蓝牙和GPS等多个源的位置信息,并提供了更快的位置固定,甚至对于高精度请求。p>