gpt4 book ai didi

java - 如何在onSensorChanged中使用GoogleMap.addMarker?

转载 作者:行者123 更新时间:2023-12-02 10:15:17 25 4
gpt4 key购买 nike

调用 onSensorChanged 方法时如何旋转标记?我尝试了这段代码,但出现了 NullPointerException。我将 mMap 声明为全局变量并将其加载到 onMapReady 中。我想在我的 onSensorChanged 方法中使用它。我该如何解决这个问题?

我收到此错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.model.Marker com.google.android.gms.maps.GoogleMap.addMarker(com.google.android.gms.maps.model.MarkerOptions)' on a null object reference

    @Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.getUiSettings().setZoomControlsEnabled(true);

latLng_dest = getIntent().getExtras().getParcelable("latLng_dest");
Log.d("LatLng_dest : ", latLng_dest.toString());

if (ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, LOCATION_REQUEST);
return;
}
mMap.setMyLocationEnabled(true);
mMap.getUiSettings().setMyLocationButtonEnabled(false);
// Call our direction function
getDeviceLocation();

}

@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor == mRotationSensor) {
if (event.values.length > 4) {
float[] truncatedRotationVector = new float[4];
System.arraycopy(event.values, 0, truncatedRotationVector, 0, 4);
updateSensor(truncatedRotationVector);
} else {
updateSensor(event.values);
}
}

}

private void updateSensor(float[] vectors) {
float[] rotationMatrix = new float[9];
SensorManager.getRotationMatrixFromVector(rotationMatrix, vectors);
int worldAxisX = SensorManager.AXIS_X;
int worldAxisZ = SensorManager.AXIS_Z;
float[] adjustedRotationMatrix = new float[9];
SensorManager.remapCoordinateSystem(rotationMatrix, worldAxisX, worldAxisZ, adjustedRotationMatrix);
float[] orientation = new float[3];
SensorManager.getOrientation(adjustedRotationMatrix, orientation);
azimuth = orientation[0]* FROM_RADS_TO_DEGS;
float pitch = orientation[1] * FROM_RADS_TO_DEGS;
float roll = orientation[2] * FROM_RADS_TO_DEGS;
Log.d("", "updateSensor: Pitch "+pitch);
Log.d("", "updateSensor: Roll "+roll);
Log.d("", " "+mMap);
markerOptions = new MarkerOptions().icon(bitmapDescriptorFromVector(MapsActivity.this, R.drawable.ic_navigation_black_24dp));
((TextView)findViewById(R.id.svtv2)).setText("Azimuth: "+azimuth+"\n"+"Pitch: "+pitch+"\n"+"Roll: "+roll);

mMap.addMarker(markerOptions.rotation(azimuth));

}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
initMap();
//markerOptions = new MarkerOptions().icon(bitmapDescriptorFromVector(MapsActivity.this, R.drawable.ic_navigation_black_24dp));
Button button = findViewById(R.id.but_dir);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView = findViewById(R.id.svtv);
textView.setText(instruction);
}
});


/*new Timer().scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
}
},0,1);*/

//Aquire a reference to the system Location Manager
LocationManager locationManager = (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) {

return;

}


final TextView live_inst = findViewById(R.id.svtv2);

// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {

@Override
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
//makeUseOfNewLocation(location);
updateDeviceLocation(location);
}

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

@Override
public void onProviderEnabled(String provider) {
}

@Override
public void onProviderDisabled(String provider) {
}
};
// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1, 0, locationListener);

try {
mSensorManager = (SensorManager) getSystemService(MapsActivity.SENSOR_SERVICE);
mRotationSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR);
mSensorManager.registerListener(this, mRotationSensor, SENSOR_DELAY);
} catch (Exception e) {
Toast.makeText(this, "Hardware compatibility issue", Toast.LENGTH_LONG).show();
}

}

最佳答案

通常,onSenorChanged 会在您启动应用程序屏幕时立即触发。

通过这种方式,在传感器值更改后,您将尝试旋转 map 对象上的标记,该标记将为空,因为它是异步调用。

在调用“rotateMarker”方法之前进行空检查。

示例:

If (mMap != null) {
// do rotate Map marker
}

注意:即使发生非常微小的变化,传感器值也会发生变化。只是为了增强性能,请使用 + 或 - 5 校准传感器值。

希望这些信息有帮助!

关于java - 如何在onSensorChanged中使用GoogleMap.addMarker?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54728209/

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