gpt4 book ai didi

java - LatLng GPS 位置的 NullPointerException

转载 作者:行者123 更新时间:2023-12-01 19:49:17 24 4
gpt4 key购买 nike

我正在使用 Google map 获取我的当前位置并在我的位置上显示红色标记。到目前为止,只有 mMap.setMyLocation(true); 适用于我的位置。我在代码末尾收到对象 userLocation 的 NullPointerException,并且 userLocation 控制台的输出也为 null。

如何在加载 map 后立即设置 userLocation?

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;

LocationManager locationManager;

LocationListener locationListener;
LatLng userLocation;
private FusedLocationProviderClient mFusedLocationClient;
public float DEFAULT_ZOOM = 15f;
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);

if (requestCode == 1) {

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);

}

}

}

}

}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}

@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {

userLocation = new LatLng(location.getLatitude(), location.getLongitude());

mMap.clear();
mMap.addMarker(new MarkerOptions().position(userLocation).title("Your Location"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(userLocation));
System.out.println("the latitude onLocationChanged is "+ userLocation.latitude);


}

@Override
public void onStatusChanged(String s, int i, Bundle bundle) {

}

@Override
public void onProviderEnabled(String s) {

}

@Override
public void onProviderDisabled(String s) {

}
};



if (Build.VERSION.SDK_INT < 23) {
try {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}
catch (SecurityException e){
e.printStackTrace();
}
} 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, 1, 1, locationListener);
// LatLng latLng = new LatLng(40.741895,-73.989308);
mMap.setMyLocationEnabled(true);
//outputs the latitude is null
System.out.println("the latitude is "+userLocation);
mMap.addMarker(new MarkerOptions().position(userLocation).title("Your Location"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(userLocation));


}


}


}


}

最佳答案

你不能。获得位置需要时间。在调用回调之前它不可用,这意味着在此之前您无法加载它。您可以尝试调用 getLastKnownLocation,但大多数情况下它也会返回 null。最好的解决方案是在回调完成之前不要依赖它。

关于java - LatLng GPS 位置的 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52010468/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com