Мой OnLocationChanged работал правильно, пока я не добавил OnSensoredChanged.Проблема в том, что мой маркер не двигается сейчас.Я проверил, вызывается ли мой OnLocationChanged путем добавления нового местоположения latlng в textView.Ответ - да.Но когда я перемещаюсь, местоположение остается тем же.Это мой код:
@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;
azimuth = (azimuth + 360) % 360;
azimuth = 360 - azimuth;
float pitch = orientation[1] * FROM_RADS_TO_DEGS;
float roll = orientation[2] * FROM_RADS_TO_DEGS;
((TextView) findViewById(R.id.svtv2)).setText("Azimuth: " + azimuth + "\n" + "Pitch: " + pitch + "\n" + "Roll: " + roll);
if (save == true && upLatLng != null) {
temp_marker.remove();
((TextView) findViewById(R.id.svtv2)).setText("Latitude: "+upLatLng.latitude+"\n"+"Longitude: "+upLatLng.longitude);
boss_markerOptions = new MarkerOptions().icon(bitmapDescriptorFromVector(MapsActivity.this, R.drawable.ic_navigation_black_24dp));
Log.d("ICI", "LUL ");
temp_marker = mMap.addMarker(boss_markerOptions.position(upLatLng).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();
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, 0, 0, locationListener);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,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();
}
}
private void updateDeviceLocation(Location location) {
upLatLng = new LatLng(location.getLatitude(), location.getLongitude());
Log.d("XD", "YOUHOU");
}
Обратите внимание, что upLatLng определен как глобальная переменная.Пожалуйста, кто-нибудь объяснит мне, что происходит.