149 lines
5.9 KiB
Java
149 lines
5.9 KiB
Java
package ee.rasks.vabaloodus.vabaloodustest;
|
|
|
|
|
|
import android.Manifest;
|
|
import android.content.Context;
|
|
import android.content.pm.PackageManager;
|
|
import android.hardware.Sensor;
|
|
import android.hardware.SensorEvent;
|
|
import android.hardware.SensorEventListener;
|
|
import android.hardware.SensorManager;
|
|
import android.support.constraint.ConstraintLayout;
|
|
import android.support.v4.app.FragmentActivity;
|
|
import android.os.Bundle;
|
|
import android.support.v4.content.ContextCompat;
|
|
import android.util.Log;
|
|
import android.view.Gravity;
|
|
import android.view.LayoutInflater;
|
|
import android.view.View;
|
|
import android.view.ViewGroup;
|
|
import android.widget.PopupWindow;
|
|
import android.widget.Toast;
|
|
|
|
import com.google.android.gms.location.LocationServices;
|
|
import com.google.android.gms.maps.CameraUpdateFactory;
|
|
import com.google.android.gms.maps.GoogleMap;
|
|
import com.google.android.gms.maps.SupportMapFragment;
|
|
import com.google.android.gms.maps.OnMapReadyCallback;
|
|
import com.google.android.gms.maps.model.LatLng;
|
|
import com.google.android.gms.maps.model.PolylineOptions;
|
|
|
|
|
|
public class ExploreMapActivity extends FragmentActivity implements OnMapReadyCallback {
|
|
private static final String TAG = "ExploreMapActivity";
|
|
private GoogleMap mMap;
|
|
|
|
private SensorManager mSensorManager;
|
|
private float mAccel; // acceleration apart from gravity
|
|
private float mAccelCurrent; // current acceleration including gravity
|
|
private float mAccelLast; // last acceleration including gravity
|
|
|
|
private PopupWindow mPopupWindow;
|
|
|
|
@Override
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
super.onCreate(savedInstanceState);
|
|
setContentView(R.layout.activity_explore_map);
|
|
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
|
|
.findFragmentById(R.id.map);
|
|
mapFragment.getMapAsync(this);
|
|
|
|
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
|
|
mSensorManager.registerListener(mSensorListener, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
|
|
mAccel = 0.00f;
|
|
mAccelCurrent = SensorManager.GRAVITY_EARTH;
|
|
mAccelLast = SensorManager.GRAVITY_EARTH;
|
|
}
|
|
|
|
@Override
|
|
public void onMapReady(GoogleMap googleMap) {
|
|
mMap = googleMap;
|
|
Log.d(TAG, "Map Ready");
|
|
PolylineOptions lineOpts = new PolylineOptions();
|
|
String lineStr = "24.6600768,59.3952051,0\n" +
|
|
" 24.6610813,59.3950262,0\n" +
|
|
" 24.6616566,59.3949129,0\n" +
|
|
" 24.6617364,59.3948689,0\n" +
|
|
" 24.6617787,59.3948166,0\n" +
|
|
" 24.6618846,59.3946193,0\n" +
|
|
" 24.6616969,59.3945742,0\n" +
|
|
" 24.6613455,59.3945319,0\n" +
|
|
" 24.6612006,59.3946712,0\n" +
|
|
" 24.6606266,59.3946043,0\n" +
|
|
" 24.6603477,59.3946528,0\n" +
|
|
" 24.6601063,59.3947395,0\n" +
|
|
" 24.6599896,59.3948712,0\n" +
|
|
" 24.6599481,59.395003,0\n" +
|
|
" 24.6600768,59.3952051,0";
|
|
for (String line : lineStr.split("\n")) {
|
|
String[] point = line.split(",");
|
|
lineOpts.add(new LatLng(Double.parseDouble(point[1]), Double.parseDouble(point[0])));
|
|
}
|
|
|
|
//Polyline polyLine = googleMap.addPolyline(lineOpts);
|
|
mMap.addPolyline(lineOpts);
|
|
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(lineOpts.getPoints().get(7), 17));
|
|
|
|
if (ContextCompat.checkSelfPermission(this,
|
|
Manifest.permission.ACCESS_FINE_LOCATION)
|
|
== PackageManager.PERMISSION_GRANTED) {
|
|
mMap.setMyLocationEnabled(true);
|
|
}
|
|
|
|
}
|
|
|
|
private final SensorEventListener mSensorListener = new SensorEventListener() {
|
|
|
|
public void onSensorChanged(SensorEvent se) {
|
|
float x = se.values[0];
|
|
float y = se.values[1];
|
|
float z = se.values[2];
|
|
mAccelLast = mAccelCurrent;
|
|
mAccelCurrent = (float) Math.sqrt((double) (x*x + y*y + z*z));
|
|
float delta = mAccelCurrent - mAccelLast;
|
|
mAccel = mAccel * 0.9f + delta; // perform low-cut filter
|
|
Log.d(TAG, "mAccel: "+mAccel);
|
|
if (mAccel > 12) {
|
|
|
|
if (mPopupWindow == null || !mPopupWindow.isShowing()){
|
|
LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
|
|
View customView = inflater.inflate(R.layout.popup_creature_near,null);
|
|
mPopupWindow = new PopupWindow(
|
|
customView,
|
|
ViewGroup.LayoutParams.WRAP_CONTENT,
|
|
ViewGroup.LayoutParams.WRAP_CONTENT
|
|
);
|
|
mPopupWindow.setElevation(5.0f);
|
|
}
|
|
|
|
if (mPopupWindow != null) {
|
|
if (!mPopupWindow.isShowing()) {
|
|
mPopupWindow.showAtLocation((ConstraintLayout) findViewById(R.id.activity_explore_map_layout), Gravity.CENTER, 0, 0);
|
|
} else {
|
|
mPopupWindow.dismiss();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public void onAccuracyChanged(Sensor sensor, int accuracy) {
|
|
}
|
|
};
|
|
|
|
@Override
|
|
protected void onResume() {
|
|
super.onResume();
|
|
mSensorManager.registerListener(mSensorListener, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
|
|
}
|
|
|
|
@Override
|
|
protected void onPause() {
|
|
mSensorManager.unregisterListener(mSensorListener);
|
|
if (mPopupWindow != null && mPopupWindow.isShowing()) {
|
|
mPopupWindow.dismiss();
|
|
}
|
|
super.onPause();
|
|
}
|
|
|
|
}
|