Android 地理定位服务需要时间来获取经度和纬度

Android 地理定位服务需要时间来获取经度和纬度,android,Android,我正在开发一个应用程序,获取用户的位置并将其发送到数据库。我的问题是,当用户打开活动时,地理定位大约需要一分钟来获取经度和纬度。有并没有一种方法可以立即得到这个位置 我的代码 @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsRe

我正在开发一个应用程序,获取用户的位置并将其发送到数据库。我的问题是,当用户打开活动时,地理定位大约需要一分钟来获取经度和纬度。有并没有一种方法可以立即得到这个位置

我的代码

  @Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);


    if(grantResults.length >0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){

        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)  == PackageManager.PERMISSION_GRANTED){
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,0, locationListener);
        }
    }
}

   private void latAndLong(final String modelSelected, final String email, final String likelyProblem){
    ActivityCompat.requestPermissions(LocationActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
    GeoLocation geoLocation = new GeoLocation(getApplicationContext());
    Location l = geoLocation.getLocation();
    if (l != null){
        double lat = l.getLatitude();
        double lng = l.getLongitude();
        Toast.makeText(getApplicationContext(), "Lat: " + lat + "\n Lon: " + lng, Toast.LENGTH_LONG).show();
        LocationSender locationSender = new LocationSender(lat, lng, email, modelSelected, likelyProblem);
        sendNetworkRequest(locationSender);
    }

    locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
    locationListener = new LocationListener() {

        @Override
        public void onLocationChanged(Location location) {

        }

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

        }

        @Override
        public void onProviderEnabled(String provider) {

        }

        @Override
        public void onProviderDisabled(String provider) {

        }
    };
    if (Build.VERSION.SDK_INT < 23){
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
    }else{

        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)  != PackageManager.PERMISSION_GRANTED){
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
        }else{
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
        }
    }
}
@覆盖
public void onRequestPermissionsResult(int-requestCode,@NonNull-String[]permissions,@NonNull-int[]grantResults){
super.onRequestPermissionsResult(请求代码、权限、授权结果);
if(grantResults.length>0&&grantResults[0]==PackageManager.PERMISSION\u已授予){
if(ContextCompat.checkSelfPermission(此,Manifest.permission.ACCESS\u FINE\u位置)==PackageManager.permission\u已授予){
locationManager.RequestLocationUpdate(locationManager.GPS_提供程序,0,0,locationListener);
}
}
}
private void latAndLong(选择最终字符串模型、最终字符串电子邮件、最终字符串类似问题){
ActivityCompat.requestPermissions(LocationActivity.this,新字符串[]{Manifest.permission.ACCESS\u FINE\u LOCATION},1);
GeoLocation GeoLocation=新的地理位置(getApplicationContext());
Location l=geoLocation.getLocation();
如果(l!=null){
双纬度=l.getLatitude();
双lng=l.Get经度();
Toast.makeText(getApplicationContext(),“Lat:+Lat+”\n Lon:+lng,Toast.LENGTH\u LONG.show();
LocationSender LocationSender=新的LocationSender(lat、lng、电子邮件、所选型号,如问题);
sendNetworkRequest(locationSender);
}
locationManager=(locationManager)this.getSystemService(Context.LOCATION\u服务);
locationListener=新locationListener(){
@凌驾
已更改位置上的公共无效(位置){
}
@凌驾
public void onStatusChanged(字符串提供程序、int状态、Bundle extra){
}
@凌驾
公共无效onProviderEnabled(字符串提供程序){
}
@凌驾
公共无效onProviderDisabled(字符串提供程序){
}
};
如果(Build.VERSION.SDK_INT<23){
locationManager.RequestLocationUpdate(locationManager.GPS\提供程序,0,0,locationListener);
}否则{
if(ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS\u FINE\u LOCATION)!=PackageManager.permission\u已授予){
ActivityCompat.requestPermissions(这个新字符串[]{Manifest.permission.ACCESS\u FINE\u LOCATION},1);
}否则{
locationManager.RequestLocationUpdate(locationManager.GPS\提供程序,0,0,locationListener);
}
}
}

我不知道为什么要使用地理定位来获得简单的纬度和经度。据我所知,地理定位旨在将地址转换为GPS坐标,或者反过来。它的工作原理是向GooglePlaces服务器发出请求,因此会有网络延迟,并且会受到一些使用限制

快速获取位置的最佳方法是使用
location lastnownlocation=locationManager.getlastnownlocation(locationProvider);
用于获取初始位置。 此后的任何位置更新都会触发
LocationManager
中的
onLocationChanged(location-location)
方法。在那里编写自定义逻辑将有助于跟踪进一步的位置更改


Android开发者网站有一个很好的平台。这可能是关于在Android上使用位置服务的最新、最全面的文章,因此我建议阅读这篇。

谢谢。我感谢