调用onSensorChanged
方法时如何旋转标记?我尝试了这段代码,但是得到了NullPointerException。我将mMap
声明为全局变量,并将其加载到onMapReady中。我想在我的onSensorChanged
方法中使用它。我该如何解决?
我收到此错误:
java.lang.NullPointerException:尝试调用虚拟方法 'com.google.android.gms.maps.model.Marker com.google.android.gms.maps.GoogleMap.addMarker(com.google.android.gms.maps.model.MarkerOptions)' 在空对象引用上
@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();
}
}
答案 0 :(得分:1)
通常,一旦启动应用程序屏幕,onSenorChanged就会触发。
通过这种方式,在更改传感器值之后,您尝试旋转地图对象上的标记(该标记为null)作为其异步调用。
在调用“ rotateMarker”方法之前放入空检查。
示例:
If (mMap != null) {
// do rotate Map marker
}
注:即使非常微小的变化,传感器值也会改变。只是为了提高性能,请使用+或-5校准传感器值。
希望此信息有所帮助!
答案 1 :(得分:0)
我解决了。 解决方案是避免在OnMapReady之前调用mMap.addMarker。我首先使用它来避免mMap上的null,也避免了markMarker位置为null的addMarker错误。
if (save) {
temp_marker.remove();
temp_marker = mMap.addMarker(markerOptions.rotation(azimuth));
}
在我的updateSensor中,并在标记中添加第一个位置后将其添加到我的getDeviceLocation中:
save = true;
将保存定义为全局,并初始化为false。
if (mMap != null)
没有用。这就是为什么我感到困惑。我没注意到它是使用相同语法的double nullPointerException。