|
|
|
@ -3,10 +3,10 @@ package com.example.sixaunyi;
|
|
|
|
|
import static androidx.constraintlayout.motion.utils.Oscillator.TAG;
|
|
|
|
|
|
|
|
|
|
import static java.lang.Math.PI;
|
|
|
|
|
|
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
|
import androidx.appcompat.app.AppCompatActivity;
|
|
|
|
|
|
|
|
|
|
import android.app.AlertDialog;
|
|
|
|
|
import android.content.Context;
|
|
|
|
|
import android.Manifest;
|
|
|
|
|
import android.annotation.SuppressLint;
|
|
|
|
|
import android.content.Intent;
|
|
|
|
@ -21,7 +21,8 @@ import android.util.Log;
|
|
|
|
|
import android.view.View;
|
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
import android.widget.Toast;
|
|
|
|
|
|
|
|
|
|
import android.animation.Animator;
|
|
|
|
|
import android.animation.ValueAnimator;
|
|
|
|
|
import com.amap.api.location.AMapLocation;
|
|
|
|
|
import com.amap.api.location.AMapLocationClient;
|
|
|
|
|
import com.amap.api.location.AMapLocationClientOption;
|
|
|
|
@ -34,6 +35,8 @@ import com.amap.api.maps.model.BitmapDescriptorFactory;
|
|
|
|
|
import com.amap.api.maps.model.LatLng;
|
|
|
|
|
import com.amap.api.maps.model.Marker;
|
|
|
|
|
import com.amap.api.maps.model.MarkerOptions;
|
|
|
|
|
import com.amap.api.maps.model.animation.Animation;
|
|
|
|
|
import com.amap.api.maps.model.animation.TranslateAnimation;
|
|
|
|
|
import com.amap.api.services.core.AMapException;
|
|
|
|
|
import com.amap.api.services.core.LatLonPoint;
|
|
|
|
|
import com.amap.api.services.core.PoiItem;
|
|
|
|
@ -45,20 +48,23 @@ import com.amap.api.services.geocoder.RegeocodeResult;
|
|
|
|
|
import com.amap.api.services.poisearch.PoiResult;
|
|
|
|
|
import com.amap.api.services.poisearch.PoiSearch;
|
|
|
|
|
import com.google.android.material.floatingactionbutton.FloatingActionButton;
|
|
|
|
|
|
|
|
|
|
import android.view.animation.LinearInterpolator;
|
|
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.net.DatagramPacket;
|
|
|
|
|
import java.net.DatagramSocket;
|
|
|
|
|
import java.net.InetAddress;
|
|
|
|
|
import java.net.SocketException;
|
|
|
|
|
import java.net.UnknownHostException;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.concurrent.ExecutorService;
|
|
|
|
|
import java.util.concurrent.Executors;
|
|
|
|
|
|
|
|
|
|
import pub.devrel.easypermissions.AfterPermissionGranted;
|
|
|
|
|
import pub.devrel.easypermissions.EasyPermissions;
|
|
|
|
|
|
|
|
|
|
public class MainActivity extends AppCompatActivity implements AMapLocationListener, LocationSource,
|
|
|
|
|
public class MainActivity extends AppCompatActivity implements AMapLocationListener, LocationSource,
|
|
|
|
|
AMap.OnMapLongClickListener, GeocodeSearch.OnGeocodeSearchListener, AMap.OnMarkerDragListener,
|
|
|
|
|
AMap.OnMarkerClickListener{
|
|
|
|
|
|
|
|
|
@ -79,10 +85,6 @@ public class MainActivity extends AppCompatActivity implements AMapLocationListe
|
|
|
|
|
private GeocodeSearch geocodeSearch;
|
|
|
|
|
//解析成功标识码
|
|
|
|
|
private static final int PARSE_SUCCESS_CODE = 1000;
|
|
|
|
|
//POI查询对象
|
|
|
|
|
private PoiSearch.Query query;
|
|
|
|
|
//POI搜索对象
|
|
|
|
|
private PoiSearch poiSearch;
|
|
|
|
|
//城市码
|
|
|
|
|
private String cityCode = null;
|
|
|
|
|
//浮动按钮
|
|
|
|
@ -97,11 +99,15 @@ public class MainActivity extends AppCompatActivity implements AMapLocationListe
|
|
|
|
|
private MarkerOptions markerOption = new MarkerOptions();
|
|
|
|
|
//
|
|
|
|
|
private DatagramSocket reveSocket;
|
|
|
|
|
private final static String Recieve_IP = "192.168.240.46";
|
|
|
|
|
private final static int RECEIVE_PORT = 9999;
|
|
|
|
|
private boolean listenStatus = true;
|
|
|
|
|
private ExecutorService mThreadPool = Executors.newCachedThreadPool();
|
|
|
|
|
////////////////////////////可修改部分/////////////////////////////
|
|
|
|
|
private final LatLng Monitorlatlng = new LatLng(28.258348849051746,113.04552182879037);
|
|
|
|
|
private final static String Recieve_IP = "192.168.39.46";
|
|
|
|
|
private final static int RECEIVE_PORT = 9987;
|
|
|
|
|
private final static String SEND_to_IP = "192.168.39.47";
|
|
|
|
|
private final static int SEND_PORT = 8888;
|
|
|
|
|
/////////////////////////////////////////////////////////////////
|
|
|
|
|
private InetAddress serverAddr;
|
|
|
|
|
//private MainActivity.ReceiveHandler receiveHandler = new ReceiveHandler();
|
|
|
|
|
private String Angle;
|
|
|
|
|
private String MonitorAngle;
|
|
|
|
|
private float distance;
|
|
|
|
@ -129,7 +135,7 @@ public class MainActivity extends AppCompatActivity implements AMapLocationListe
|
|
|
|
|
|
|
|
|
|
udpReceiveThread = new UdpReceiveThread();
|
|
|
|
|
udpReceiveThread.startListening();
|
|
|
|
|
|
|
|
|
|
addSensorMarker(Monitorlatlng);
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* 检查Android版本
|
|
|
|
@ -282,6 +288,7 @@ public class MainActivity extends AppCompatActivity implements AMapLocationListe
|
|
|
|
|
city = aMapLocation.getCity();
|
|
|
|
|
//当前位置经纬度坐标
|
|
|
|
|
Current_latlng = new LatLng(aMapLocation.getLatitude(), aMapLocation.getLongitude());
|
|
|
|
|
MonitorDistance = AMapUtils.calculateLineDistance(Current_latlng,Monitorlatlng);
|
|
|
|
|
//获取纬度
|
|
|
|
|
double latitude = aMapLocation.getLatitude();
|
|
|
|
|
//获取经度
|
|
|
|
@ -344,19 +351,16 @@ public class MainActivity extends AppCompatActivity implements AMapLocationListe
|
|
|
|
|
@Override
|
|
|
|
|
public void onMapLongClick(LatLng latLng) {
|
|
|
|
|
distance = AMapUtils.calculateLineDistance(Current_latlng,latLng);
|
|
|
|
|
LatLng Monitorlatlng = new LatLng(113.05370390725032,28.256794281117525);
|
|
|
|
|
MonitorDistance = AMapUtils.calculateLineDistance(Current_latlng,Monitorlatlng);
|
|
|
|
|
Angle = calcAngle(Current_latlng.latitude, Current_latlng.longitude, latLng.latitude,latLng.longitude);
|
|
|
|
|
MonitorAngle = calcAngle(Current_latlng.latitude, Current_latlng.longitude, 113.05370390725032,28.256794281117525);
|
|
|
|
|
//showMsg("长按了地图,经度:"+latLng.longitude+",纬度:"+latLng.latitude);
|
|
|
|
|
Log.i(TAG,String.valueOf(latLng.longitude));
|
|
|
|
|
Log.i(TAG,String.valueOf(latLng.latitude));
|
|
|
|
|
Log.i(TAG, String.valueOf(latLng.longitude));
|
|
|
|
|
Log.i(TAG, String.valueOf(latLng.latitude));
|
|
|
|
|
showMsg("直线距离:"+distance+"米"+"角度"+Angle);
|
|
|
|
|
//坐标转地址113.05370390725032 28.256794281117525
|
|
|
|
|
latlonToAddress(latLng);
|
|
|
|
|
//添加标点
|
|
|
|
|
addMarker(latLng);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -381,6 +385,126 @@ public class MainActivity extends AppCompatActivity implements AMapLocationListe
|
|
|
|
|
marker.showInfoWindow();
|
|
|
|
|
markerList.add(marker);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 添加传感器标点
|
|
|
|
|
private Marker sensorMarker = null;
|
|
|
|
|
|
|
|
|
|
private void addSensorMarker(LatLng latLng) {
|
|
|
|
|
// 显示浮动按钮
|
|
|
|
|
|
|
|
|
|
// 添加传感器标点
|
|
|
|
|
MarkerOptions sensorMarkerOption = new MarkerOptions();
|
|
|
|
|
sensorMarker = aMap.addMarker(sensorMarkerOption
|
|
|
|
|
.position(latLng)
|
|
|
|
|
.draggable(false)
|
|
|
|
|
// 标点图标
|
|
|
|
|
.icon(BitmapDescriptorFactory.
|
|
|
|
|
fromBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.sensor)))
|
|
|
|
|
// 备注
|
|
|
|
|
.title("传感器")
|
|
|
|
|
.snippet("距离:")
|
|
|
|
|
|
|
|
|
|
);
|
|
|
|
|
sensorMarker.showInfoWindow();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 添加无人机标点
|
|
|
|
|
private Marker droneMarker = null;
|
|
|
|
|
|
|
|
|
|
private void addDroneMarker() {
|
|
|
|
|
// 添加无人机标点
|
|
|
|
|
MarkerOptions droneMarkerOption = new MarkerOptions();
|
|
|
|
|
droneMarker = aMap.addMarker(droneMarkerOption
|
|
|
|
|
.position(Current_latlng)
|
|
|
|
|
.draggable(false)
|
|
|
|
|
// 标点图标
|
|
|
|
|
.icon(BitmapDescriptorFactory.
|
|
|
|
|
fromBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.drone)))
|
|
|
|
|
// 备注
|
|
|
|
|
.title("无人机")
|
|
|
|
|
.snippet("距离:")
|
|
|
|
|
);
|
|
|
|
|
droneMarker.showInfoWindow();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 移动无人机标点到传感器标点并显示平移动画
|
|
|
|
|
public static class SmoothMoveUtil {
|
|
|
|
|
|
|
|
|
|
private Marker droneMarker;
|
|
|
|
|
private LatLng startLatLng;
|
|
|
|
|
private LatLng endLatLng;
|
|
|
|
|
private long duration;
|
|
|
|
|
|
|
|
|
|
public SmoothMoveUtil(Marker marker) {
|
|
|
|
|
this.droneMarker = marker;
|
|
|
|
|
}
|
|
|
|
|
public void moveDroneMarker(LatLng sensorLatLng, long duration) {
|
|
|
|
|
if (droneMarker != null) {
|
|
|
|
|
startLatLng = droneMarker.getPosition();
|
|
|
|
|
endLatLng = sensorLatLng;
|
|
|
|
|
this.duration = duration;
|
|
|
|
|
// 开始动画
|
|
|
|
|
startSmoothMove();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void startSmoothMove() {
|
|
|
|
|
LatLngInterpolator latLngInterpolator = new LatLngInterpolator.LinearFixed();
|
|
|
|
|
ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, 1);
|
|
|
|
|
valueAnimator.setDuration(duration);
|
|
|
|
|
valueAnimator.setInterpolator(new LinearInterpolator());
|
|
|
|
|
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onAnimationUpdate(ValueAnimator animation) {
|
|
|
|
|
float fraction = animation.getAnimatedFraction();
|
|
|
|
|
LatLng newPosition = latLngInterpolator.interpolate(fraction, startLatLng, endLatLng);
|
|
|
|
|
droneMarker.setPosition(newPosition);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
valueAnimator.addListener(new Animator.AnimatorListener() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onAnimationStart(Animator animation) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onAnimationEnd(Animator animation) {
|
|
|
|
|
// 动画结束
|
|
|
|
|
droneMarker.setPosition(endLatLng);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onAnimationCancel(Animator animation) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onAnimationRepeat(Animator animation) {
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
valueAnimator.start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// LatLng 插值器接口——算经纬度过渡用的
|
|
|
|
|
public interface LatLngInterpolator {
|
|
|
|
|
LatLng interpolate(float fraction, LatLng a, LatLng b);
|
|
|
|
|
|
|
|
|
|
class LinearFixed implements LatLngInterpolator {
|
|
|
|
|
@Override
|
|
|
|
|
public LatLng interpolate(float fraction, LatLng a, LatLng b) {
|
|
|
|
|
double lat = (b.latitude - a.latitude) * fraction + a.latitude;
|
|
|
|
|
double lngDelta = b.longitude - a.longitude;
|
|
|
|
|
|
|
|
|
|
// 如果跨越了 180 度经度,则进行调整
|
|
|
|
|
if (Math.abs(lngDelta) > 180) {
|
|
|
|
|
lngDelta -= Math.signum(lngDelta) * 360;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double lng = lngDelta * fraction + a.longitude;
|
|
|
|
|
return new LatLng(lat, lng);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 清空地图Marker
|
|
|
|
|
* @param view
|
|
|
|
@ -394,12 +518,21 @@ public class MainActivity extends AppCompatActivity implements AMapLocationListe
|
|
|
|
|
ClearMarker_btn.hide();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void moveDrone(){
|
|
|
|
|
SmoothMoveUtil moveUtil = new SmoothMoveUtil(droneMarker);
|
|
|
|
|
long time = (long) MonitorDistance/2*1000;
|
|
|
|
|
moveUtil.moveDroneMarker(Monitorlatlng,time);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void changeAct(View view) {
|
|
|
|
|
udpReceiveThread.stopListening();
|
|
|
|
|
Intent intent = new Intent(MainActivity.this, VideoActivity.class);
|
|
|
|
|
Bundle b = new Bundle();
|
|
|
|
|
b.putString("Angle", MonitorAngle);
|
|
|
|
|
b.putFloat("Distance", MonitorDistance);
|
|
|
|
|
intent.putExtras(b);
|
|
|
|
|
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
|
|
|
|
|
startActivity(intent);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -474,7 +607,7 @@ public class MainActivity extends AppCompatActivity implements AMapLocationListe
|
|
|
|
|
RegeocodeAddress regeocodeAddress = regeocodeResult.getRegeocodeAddress();
|
|
|
|
|
//显示解析后的地址
|
|
|
|
|
showMsg("地址:"+regeocodeAddress.getFormatAddress());
|
|
|
|
|
}else {
|
|
|
|
|
} else {
|
|
|
|
|
showMsg("获取地址失败");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -486,33 +619,35 @@ public class MainActivity extends AppCompatActivity implements AMapLocationListe
|
|
|
|
|
|
|
|
|
|
public class UdpReceiveThread extends Thread {
|
|
|
|
|
private static final String TAG = "UdpReceiveThread";
|
|
|
|
|
private static final int RECEIVE_PORT = 9999;
|
|
|
|
|
private static final int mRECEIVE_PORT = RECEIVE_PORT;
|
|
|
|
|
private boolean mRunning;
|
|
|
|
|
private String mReceiveIp = Recieve_IP;
|
|
|
|
|
private String mExpectedData = "Motion detected!";
|
|
|
|
|
private float mMonitorDistance = MonitorDistance;
|
|
|
|
|
private String mMonitorAngle = MonitorAngle;
|
|
|
|
|
private float udpMonitorDistance = MonitorDistance;
|
|
|
|
|
private String udpMonitorAngle = MonitorAngle;
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
try {
|
|
|
|
|
DatagramSocket socket = new DatagramSocket(RECEIVE_PORT);
|
|
|
|
|
DatagramSocket socket = new DatagramSocket(mRECEIVE_PORT);
|
|
|
|
|
byte[] buffer = new byte[1024];
|
|
|
|
|
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
|
|
|
|
|
while (mRunning) {
|
|
|
|
|
Log.i(TAG, "run: 111111111111111111");
|
|
|
|
|
socket.receive(packet);
|
|
|
|
|
byte[] data = packet.getData();
|
|
|
|
|
String receivedString = new String(data, 0, packet.getLength());
|
|
|
|
|
Log.d(TAG, "Received data: " + receivedString);
|
|
|
|
|
if (receivedString.equals(mExpectedData)) {
|
|
|
|
|
Intent intent = new Intent(MainActivity.this, VideoActivity.class);
|
|
|
|
|
Bundle b = new Bundle();
|
|
|
|
|
b.putString("Angle", mMonitorAngle);
|
|
|
|
|
b.putFloat("Distance", mMonitorDistance);
|
|
|
|
|
intent.putExtras(b);
|
|
|
|
|
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
|
|
|
|
|
startActivity(intent);
|
|
|
|
|
Log.i(TAG, "run: 222222222222222222222222222");
|
|
|
|
|
sendCommand("WARNING");
|
|
|
|
|
Context context = MainActivity.this;
|
|
|
|
|
showAlert(context,"传感器报警");
|
|
|
|
|
addDroneMarker();
|
|
|
|
|
moveDrone();
|
|
|
|
|
mRunning = false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
socket.close();
|
|
|
|
@ -523,6 +658,16 @@ public class MainActivity extends AppCompatActivity implements AMapLocationListe
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public void showAlert(Context context, String message) {
|
|
|
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(context);
|
|
|
|
|
builder.setTitle("警告");
|
|
|
|
|
builder.setMessage(message);
|
|
|
|
|
builder.setIcon(R.drawable.warning);
|
|
|
|
|
builder.setPositiveButton("确定", null);
|
|
|
|
|
|
|
|
|
|
AlertDialog dialog = builder.create();
|
|
|
|
|
dialog.show();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void startListening() {
|
|
|
|
|
mRunning = true;
|
|
|
|
@ -585,11 +730,48 @@ public class MainActivity extends AppCompatActivity implements AMapLocationListe
|
|
|
|
|
angle = 90-angle;
|
|
|
|
|
} else if (subLat < 0) {
|
|
|
|
|
// 此时的点在中心点的左下角
|
|
|
|
|
angle += 180;
|
|
|
|
|
angle = 270-angle;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return String.valueOf(angle);
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
*UDP发送线程
|
|
|
|
|
*/
|
|
|
|
|
class SendRunnable implements Runnable {
|
|
|
|
|
byte[] mData;
|
|
|
|
|
InetAddress mAddress;
|
|
|
|
|
int mPort;
|
|
|
|
|
|
|
|
|
|
public SendRunnable(byte[] data, InetAddress address, int port) {
|
|
|
|
|
mData = data;
|
|
|
|
|
mAddress = address;
|
|
|
|
|
mPort = port;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
try {
|
|
|
|
|
// 创建发送数据报文
|
|
|
|
|
DatagramPacket packet = new DatagramPacket(mData, mData.length, mAddress, mPort);
|
|
|
|
|
// 创建 DatagramSocket 对象并发送数据报文
|
|
|
|
|
DatagramSocket socket = new DatagramSocket();
|
|
|
|
|
socket.send(packet);
|
|
|
|
|
|
|
|
|
|
// 关闭 DatagramSocket 对象
|
|
|
|
|
socket.close();
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//封装好的接口函数
|
|
|
|
|
private void sendCommand(String str) throws UnknownHostException {
|
|
|
|
|
byte[] sendData = str.getBytes();
|
|
|
|
|
InetAddress address = InetAddress.getByName(SEND_to_IP);
|
|
|
|
|
SendRunnable sendRunnable1 = new SendRunnable(sendData, address, SEND_PORT);
|
|
|
|
|
mThreadPool.execute(sendRunnable1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|