Android studio 如何获取当前位置并获取纬度和经度作为字符串?

Android studio 如何获取当前位置并获取纬度和经度作为字符串?,android-studio,google-maps,locationmanager,android-gps,locationlistener,Android Studio,Google Maps,Locationmanager,Android Gps,Locationlistener,我在遵循一个教程,并试图使用谷歌地图获取我的当前位置,但我在LatLng上遇到了一个错误,它说“试图调用虚拟方法和空对象引用”。我想得到我的当前位置,还想得到纬度和经度作为字符串。我想使用这些纬度和经度进行实时位置跟踪 公共类DriverCurrentLocationActivity扩展了FragmentActivity在MapreadyCallback上的实现{ 私有谷歌地图; 地点经理地点经理; LocationListener LocationListener; @凌驾 public v

我在遵循一个教程,并试图使用谷歌地图获取我的当前位置,但我在LatLng上遇到了一个错误,它说“试图调用虚拟方法和空对象引用”。我想得到我的当前位置,还想得到纬度和经度作为字符串。我想使用这些纬度和经度进行实时位置跟踪

公共类DriverCurrentLocationActivity扩展了FragmentActivity在MapreadyCallback上的实现{
私有谷歌地图;
地点经理地点经理;
LocationListener LocationListener;
@凌驾
public void onRequestPermissionsResult(int-requestCode、字符串[]权限、int[]grantResults){
super.onRequestPermissionsResult(请求代码、权限、授权结果);
if(requestCode==1){
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);
Location lastKnownLocation=locationManager.getLastKnownLocation(locationManager.GPS\U提供程序);
更新映射(lastKnownLocation);
}
}
}
}
公共无效更新映射(位置){
试一试{
LatLng userLocation=新LatLng(location.getLatitude(),location.getLongitude());
mMap.clear();
mMap.moveCamera(CameraUpdateFactory.newLatLng(userLocation));
mMap.addMarker(新的MarkerOptions().position(userLocation.title)(“您的位置”);
}捕获(例外e){
Toast.makeText(this,e.getMessage(),Toast.LENGTH_SHORT.show();
}
}
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u driver\u current\u location);
//获取SupportMapFragment,并在地图准备好使用时收到通知。
SupportMapFragment mapFragment=(SupportMapFragment)getSupportFragmentManager()
.findFragmentById(R.id.map);
getMapAsync(这个);
}
@凌驾
4月1日公开作废(谷歌地图谷歌地图){
mMap=谷歌地图;
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);
Location lastKnownLocation=locationManager.getLastKnownLocation(locationManager.GPS\U提供程序);
if(lastKnownLocation!=null){
更新映射(lastKnownLocation);
}
}
}
}

}
检查updateMap函数是否为null,因为getLastKnownLocation有时可能返回null,这样就不会出现崩溃或异常。也可以试试。检查updateMap函数是否为null,因为getLastKnownLocation有时可能返回null,这样就不会出现崩溃或异常。也可以试试。

在onCreate中创建someMethod()

        fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(getActivity());
        fetchLastLocation();

 private void someMethod() {

        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_CODE);
            return;
        }
    Task<Location> task = fusedLocationProviderClient.getLastLocation();
    task.addOnSuccessListener(new OnSuccessListener<Location>() {
        @Override
        public void onSuccess(Location location) {

            if (location != null){
                currentLocation = location;
                Toast.makeText(this,currentLocation.getLatitude() +""+ currentLocation.getLongitude(),Toast.LENGTH_LONG).show();
                SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
                mapFragment.getMapAsync(MapFragment.this);
            }
        }
    });
}
将板条转换为字符串:

 LatLng center = new LatLng(currentLocation.getLatitude(), currentLocation.getLongitude());
    MarkerOptions markerOptions = new MarkerOptions().position(center).title(center.latitude + ":" + center.longitude);
    mMap.clear();
    googleMap.animateCamera(CameraUpdateFactory.newLatLng(center));
    googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(center, 18));
    googleMap.addMarker(markerOptions);
String string_latlng = center.toString();
希望这将帮助您:)

在onCreate中创建someMethod()

        fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(getActivity());
        fetchLastLocation();

 private void someMethod() {

        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_CODE);
            return;
        }
    Task<Location> task = fusedLocationProviderClient.getLastLocation();
    task.addOnSuccessListener(new OnSuccessListener<Location>() {
        @Override
        public void onSuccess(Location location) {

            if (location != null){
                currentLocation = location;
                Toast.makeText(this,currentLocation.getLatitude() +""+ currentLocation.getLongitude(),Toast.LENGTH_LONG).show();
                SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
                mapFragment.getMapAsync(MapFragment.this);
            }
        }
    });
}
将板条转换为字符串:

 LatLng center = new LatLng(currentLocation.getLatitude(), currentLocation.getLongitude());
    MarkerOptions markerOptions = new MarkerOptions().position(center).title(center.latitude + ":" + center.longitude);
    mMap.clear();
    googleMap.animateCamera(CameraUpdateFactory.newLatLng(center));
    googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(center, 18));
    googleMap.addMarker(markerOptions);
String string_latlng = center.toString();
希望这对您有所帮助:)