高德地图上面实现小米无人机兴趣点功能

来源:互联网 发布:淘宝做任务送礼物微信 编辑:程序博客网 时间:2024/05/18 03:28

 这么快就2017年了,以前都没有写博客的习惯,现在学习一下。

 这次是防小米无人机兴趣点的实现,可以先看图,其实很简单,主要就是设置一些参数,右边是一些seebar,左边是在地图上面根据右边的参数画出对应大小的圆。 

 主要功能点:1:拖动半径 ,左边的圆形大小会改变。

                       2:拖动角度,左边的小圆形和三角箭头在圆上面移动,小圆和三角之间的夹角为90 度

   下面看代码

  
import android.content.Context;
import android.graphics.Point;
import android.graphics.drawable.BitmapDrawable;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.model.CameraPosition;
import com.amap.api.maps.model.Circle;
import com.amap.api.maps.model.CircleOptions;
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.Polyline;
import com.amap.api.maps.model.PolylineOptions;
import com.fimi.kernel.utils.ToastUtil;
import com.fimi.kernel.view.percent.PercentRelativeLayout;
import com.fimi.soul.R;
import com.fimi.soul.biz.FlyAction.DataHandler;
import com.fimi.soul.biz.FlyAction.MyBitmapDescriptor;
import com.fimi.soul.drone.Drone;
import com.fimi.soul.drone.DroneInterfaces;
import com.fimi.soul.entity.FlyActionBean;
import com.fimi.soul.utils.ConvertSize;
import com.fimi.soul.utils.GeoTools;
import com.fimi.soul.utils.GpsCorrect;
import com.fimi.soul.utils.Latitudelongitude;
import com.fimi.soul.utils.SystemUtil;
import com.fimi.soul.view.MyEditView;
import com.fimi.soul.view.myhorizontalseebar.SeekBar;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;


public class PoiOperaView implements View.OnClickListener, View.OnTouchListener {
private MyEditView poi_height, poispeek, poiridusvalue, poianglevalue;
private TextView
spend_time, poiangle, pointheight, pointsppek, pointridus;
private SeekBar height_seebar_value, speek_seebar_value, ridus_seebar_value, angle_seebar_value;

private RadioGroup drone_direction;
private Context context;
private PercentRelativeLayout poi_operaRL;
private RadioButton clockwise, anticlockwise;
private Button myspinner, delete_poi, setPoi;
private PopupWindow popupWindow;
private String[] head_angle;
private boolean isTouchHeight, isTouchSpeek, isTouchRidus, isTouchAngle;
private Drone drone;
private int limitSpeek;
private Circle chacheCircle;
private DataHandler dataHandler;

private Marker startMarker, arrawMarker;
private AMap aMap;
private Polyline mPolyline;
EditText[] viewArray;


InputMethodManager inputMethodManager;
private int keyHeight;

private int minHeight = 5;

private int maxHeight = 120;

private int maxSpeek = 10;

private int minSpeek = 1;

private int maxRadius = 450;

private int minRadius = 5;

private int maxAngle = 360;

private int minAngle = 0;

private String cacheString;

private SeekBar[] seebarArray;


//初始化view 和右边seebar


public PoiOperaView(View view, final Drone drone) {
inputMethodManager = (InputMethodManager) drone.context.getSystemService(Context.INPUT_METHOD_SERVICE);
keyHeight = drone.context.getResources().getDisplayMetrics().heightPixels / 3;
this.drone = drone;
aMap = drone.getaMap();
this.context = drone.context;
dataHandler = DataHandler.getDataHandler();
head_angle = context.getResources().getStringArray(R.array.drone_head);
pointheight = (TextView) view.findViewById(R.id.pointheight);
pointsppek = (TextView) view.findViewById(R.id.pointsppek);
pointridus = (TextView) view.findViewById(R.id.pointridus);


poianglevalue = (MyEditView) view.findViewById(R.id.poianglevalue);
poiangle = (TextView) view.findViewById(R.id.poiangle);
angle_seebar_value = (SeekBar) view.findViewById(R.id.angle_seebar_value);
angle_seebar_value.setMax(maxAngle);


//更改角度
angle_seebar_value.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (isTouchAngle) {
FlyActionBean mFlyActionBean = dataHandler.getmFlyActionBean();
if (null != mFlyActionBean) {
poianglevalue.setText(progress + "°");
mFlyActionBean.setStart_point_agle((short) progress);
createMarker(false);

}
}
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
isTouchAngle = true;
}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
isTouchAngle = false;
}
});

setPoi = (Button) view.findViewById(R.id.setPoi);
setPoi.setOnClickListener(this);
spend_time = (TextView) view.findViewById(R.id.spend_time);
delete_poi = (Button) view.findViewById(R.id.delete_poi);
delete_poi.setOnClickListener(this);
myspinner = (Button) view.findViewById(R.id.myspinner);
myspinner.setText(head_angle[0]);
myspinner.setOnClickListener(this);
drone_direction = (RadioGroup) view.findViewById(R.id.drone_direction);
clockwise = (RadioButton) view.findViewById(R.id.clockwise);
anticlockwise = (RadioButton) view.findViewById(R.id.anticlockwise);
drone_direction.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
FlyActionBean mFlyActionBean = dataHandler.getmFlyActionBean();
if (null != mFlyActionBean) {
switch (checkedId) {
case R.id.clockwise:
mFlyActionBean.setPara1(3);//2
break;
case R.id.anticlockwise:
mFlyActionBean.setPara1(1);//0
break;
}
changeViewColor();
createMarker(false);
}
}
});
drone_direction.check(R.id.clockwise);

poi_operaRL = (PercentRelativeLayout) view.findViewById(R.id.poi_operaRL);
poi_height = (MyEditView) view.findViewById(R.id.poi_height_value);
poispeek = (MyEditView) view.findViewById(R.id.poispeek);
poiridusvalue = (MyEditView) view.findViewById(R.id.poiridusvalue);


height_seebar_value = (SeekBar) view.findViewById(R.id.height_seebar_value);

speek_seebar_value = (SeekBar) view.findViewById(R.id.speek_seebar_value);

ridus_seebar_value = (SeekBar) view.findViewById(R.id.ridus_seebar_value);
ridus_seebar_value.setMax(maxRadius);
speek_seebar_value.setMax(maxSpeek);
height_seebar_value.setMax(maxHeight);

height_seebar_value.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (isTouchHeight) {
FlyActionBean mFlyActionBean = dataHandler.getmFlyActionBean();
if (null != mFlyActionBean) {
if (progress < minHeight) {
progress = minHeight;
}
poi_height.setText(progress + "m");
mFlyActionBean.setHeight(progress);
for (Marker marker : DataHandler.getDataHandler().getMarkerList()) {
if (marker.getObject().equals(mFlyActionBean)) {
marker.setIcon(MyBitmapDescriptor.obtainFromView(context, mFlyActionBean.getDrawableRes(), mFlyActionBean.getHeight(), true));
break;
}
}
}
}
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
isTouchHeight = true;
}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
isTouchHeight = false;
}
});

//速度进度条 就是飞机绕圆圈飞行的速度
speek_seebar_value.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (isTouchSpeek) {
FlyActionBean mFlyActionBean = dataHandler.getmFlyActionBean();
if (null != mFlyActionBean) {
if (progress < minSpeek) {
progress = minSpeek;
}
poispeek.setText(progress + "m/s");
mFlyActionBean.setSpeek(progress);
caluRunTime();
}
}
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
isTouchSpeek = true;

}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
isTouchSpeek = false;
if (ridus_seebar_value.getProgress() * 1.5 < seekBar.getProgress() * seekBar.getProgress()) {
int minRidus = (int) (seekBar.getProgress() * seekBar.getProgress() / 1.5d) + 1;
if(minRidus< minRadius){
minRidus=minRadius;
}
ridus_seebar_value.setProgress(minRidus);
poiridusvalue.setText(minRidus + "m");
FlyActionBean mFlyActionBean = dataHandler.getmFlyActionBean();
if (null != mFlyActionBean) {
mFlyActionBean.setRidus(minRidus);
drawCircle(mFlyActionBean);
createMarker(false);
}
}
}
});

//半径seebar 
ridus_seebar_value.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (isTouchRidus) {
FlyActionBean mFlyActionBean = dataHandler.getmFlyActionBean();
if (null != mFlyActionBean) {
if (progress <= limitSpeek) {
progress = limitSpeek;
}
if (progress < minRadius) {
progress = minRadius;
}
poiridusvalue.setText(progress + "m");

//设置当前的半径给FLyBean对象
mFlyActionBean.setRidus(progress);

//重新更改圆的半径
drawCircle(mFlyActionBean);

//更爱大圆上面的小圆marker 和小三角
createMarker(false);

//计算飞完一圈所需时间
caluRunTime();
}
}
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
isTouchRidus = true;
limitSpeek = (int) (speek_seebar_value.getProgress() * speek_seebar_value.getProgress() / 1.5d) + 1;
if (limitSpeek < minSpeek) {
limitSpeek = minSpeek;
}
}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
isTouchRidus = false;
if (ridus_seebar_value.getProgress() * 1.5 < seekBar.getProgress() * seekBar.getProgress()) {
int maxspeek = (int) Math.sqrt(ridus_seebar_value.getProgress() * 1.5d);
if (maxspeek >= maxSpeek) {
maxspeek = maxSpeek;
}
speek_seebar_value.setMax(maxspeek);
if (speek_seebar_value.getProgress() >= maxspeek) {
speek_seebar_value.setProgress(maxspeek);
poispeek.setText(maxspeek + "m/s");
}
}
}
});

SystemUtil.changeFontDINTT(context.getAssets(), poianglevalue, poi_height, poispeek, poiridusvalue);
SystemUtil.changeFontLanTing(context.getAssets(), pointsppek, pointridus, pointheight, clockwise, anticlockwise, spend_time, poiangle);

viewArray = new EditText[]{poi_height, poispeek, poiridusvalue, poianglevalue};
for (EditText view1 : viewArray) {
view1.setInputType(InputType.TYPE_CLASS_NUMBER);
view1.clearFocus();
}
poi_height.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus == false) {
changeTextMessage(poi_height, maxHeight, minHeight, height_seebar_value, "m");
}else{
cacheString=poi_height.getText().toString();
}
}
});

poispeek.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus == false) {
try{
String ridus= filterUnNumber( poiridusvalue.getText().toString());
int maxspeek = (int) Math.sqrt(Integer.parseInt(ridus) * 1.5d);
if (maxspeek >= maxSpeek) {
maxspeek = maxSpeek;
}
if (maxspeek == 0) {
changeTextMessage(poispeek, speek_seebar_value.getMax(), minSpeek, speek_seebar_value, "m/s");
} else {
changeTextMessage(poispeek, maxspeek, minSpeek, speek_seebar_value, "m/s");
}
}catch (Exception e){

}
}else{
cacheString=poispeek.getText().toString();
}
}
});

poiridusvalue.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus == false) {
changeTextMessage(poiridusvalue, ridus_seebar_value.getMax(), minRadius, ridus_seebar_value, "m");
}else{
cacheString=poiridusvalue.getText().toString();
}
}
});

poianglevalue.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus == false) {
changeTextMessage(poianglevalue, maxAngle, minAngle, angle_seebar_value, "°");
}else{
cacheString=poianglevalue.getText().toString();
}
}
});

poi_height.setOnFinishComposingListener(new MyEditView.OnFinishComposingListener() {
@Override
public void finishComposing() {
poi_height.clearFocus();
}
}

);
poispeek.setOnFinishComposingListener(new MyEditView.OnFinishComposingListener() {
@Override
public void finishComposing() {
poispeek.clearFocus();
}
});

poiridusvalue.setOnFinishComposingListener(new MyEditView.OnFinishComposingListener() {

@Override
public void finishComposing() {
poiridusvalue.clearFocus();
}
});

poianglevalue.setOnFinishComposingListener(new MyEditView.OnFinishComposingListener() {
@Override
public void finishComposing() {
poianglevalue.clearFocus();
}
});

seebarArray=new SeekBar[]{height_seebar_value, speek_seebar_value, ridus_seebar_value, angle_seebar_value};
for(SeekBar seekBar:seebarArray){
seekBar.setOnTouchListener(this);
}

}

public String getPrettyNumber(String number) {
return BigDecimal.valueOf(Integer.parseInt(number))
.stripTrailingZeros().toPlainString();
}
private void changeTextMessage(MyEditView mMyEditView, int maxHeight, int minHeight, SeekBar mSeekBar, String sigle) {
try {
String textStr = mMyEditView.getText().toString();
textStr = filterUnNumber(textStr);
textStr=getPrettyNumber(textStr);
int number = Integer.parseInt(textStr);
if (number > maxHeight) {
number = maxHeight;
textStr = maxHeight + sigle;
mMyEditView.setText(textStr);
} else if (number < minHeight) {
number = minHeight;
textStr = minHeight + sigle;
mMyEditView.setText(textStr);
}
mSeekBar.setProgress(Integer.valueOf(number));
if (!textStr.contains(sigle)) {
mMyEditView.setText(textStr + sigle);
}

FlyActionBean mFlyActionBean = dataHandler.getmFlyActionBean();
if (mSeekBar == angle_seebar_value) {
mFlyActionBean.setStart_point_agle((short) number);
createMarker(false);
} else if (mSeekBar == speek_seebar_value) {
mFlyActionBean.setSpeek(number);
caluRunTime();
} else if (mSeekBar == height_seebar_value) {
mFlyActionBean.setHeight(number);
for (Marker marker : DataHandler.getDataHandler().getMarkerList()) {
if (marker.getObject().equals(mFlyActionBean)) {
marker.setIcon(MyBitmapDescriptor.obtainFromView(context, mFlyActionBean.getDrawableRes(), mFlyActionBean.getHeight(), true));
break;
}
}
} else if (mSeekBar == ridus_seebar_value) {
mFlyActionBean.setRidus(number);
drawCircle(mFlyActionBean);
createMarker(false);
caluRunTime();
}

} catch (Exception e) {
if(null!=cacheString){
mMyEditView.setText(cacheString);
cacheString=" ";
}
}
}

public static String filterUnNumber(String str) {
// 只允数字
String regEx = "[^0-9]";
Pattern p = Pattern.compile(regEx);
java.util.regex.Matcher m = p.matcher(str);
return m.replaceAll("").trim();

}


public void showOperaView() {
if (!setPoi.isShown()) {
setPoi.setVisibility(View.VISIBLE);
ToastUtil.showToast(context, R.string.set_poi_tip, 1500);
}
}

public void setView(FlyActionBean mFlyActionBean) {

if (myspinner.getText().toString().equals(head_angle[0])) {
mFlyActionBean.setYaw_mode(1);
} else if (myspinner.getText().toString().equals(head_angle[1])) {
mFlyActionBean.setYaw_mode(0);
} else if (myspinner.getText().toString().equals(head_angle[2])) {
mFlyActionBean.setYaw_mode(2);
} else if (myspinner.getText().toString().equals(head_angle[3])) {
mFlyActionBean.setYaw_mode(3);
} else if (myspinner.getText().toString().equals(head_angle[4])) {
mFlyActionBean.setYaw_mode(4);
}

switch (drone_direction.getCheckedRadioButtonId()) {
case R.id.clockwise:
mFlyActionBean.setPara1(3);//2
break;
case R.id.anticlockwise:
mFlyActionBean.setPara1(1);//0
break;
}
changeViewColor();
caluRunTime();
}


public void hideOperaLayout() {
if (poi_operaRL.isShown()) {
poi_operaRL.setVisibility(View.GONE);
}
}

public void showOperalayout() {
if (!poi_operaRL.isShown()) {
poi_operaRL.setVisibility(View.VISIBLE);
}
}

public void hideOperaView() {
hideOperaLayout();

if (null != popupWindow && popupWindow.isShowing()) {
popupWindow.dismiss();
myspinner.setBackgroundResource(R.drawable.bg_fly_combobox_more_down);
}
if (null != chacheCircle) {
chacheCircle.remove();
chacheCircle = null;
}

if (null != startMarker) {
startMarker.destroy();
startMarker = null;
}

if (null != arrawMarker) {
arrawMarker.destroy();
arrawMarker = null;
}

if (null != mPolyline) {
mPolyline.remove();
mPolyline = null;
}
}

public void hideSetPoiButton() {
if (setPoi.isShown()) {
setPoi.setVisibility(View.GONE);
}
}

public void changeViewColor() {
if (clockwise.isChecked()) {
clockwise.setTextColor(context.getResources().getColor(R.color.color_height));
anticlockwise.setTextColor(context.getResources().getColor(R.color.black));
} else {
anticlockwise.setTextColor(context.getResources().getColor(R.color.color_height));
clockwise.setTextColor(context.getResources().getColor(R.color.black));
}
}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.myspinner:
showPopuDowSpinner();
break;
case R.id.delete_poi:
hideOperaView();
drone.notifyDroneEvent(DroneInterfaces.DroneEventsType.CLEARDATA);
showOperaView();
break;
case R.id.setPoi:
drone.notifyDroneEvent(DroneInterfaces.DroneEventsType.CREATEPOI);
setPoi.setVisibility(View.GONE);
break;
}
}

public void showpoiOperaFrame() {
showOperalayout();
moveMapIntoLeftCenter();
setStartValue();
}

public void setStartValue() {
FlyActionBean mFlyActionBean = DataHandler.getDataHandler().getmFlyActionBean();
if (null != mFlyActionBean) {
double distance = distanceDroneLatlngandHomeLatlng();
if (maxRadius - distance < minRadius) {
ToastUtil.showToast(context, R.string.cant_setPoi, 3000);
} else {
height_seebar_value.setProgress(mFlyActionBean.getHeight());
speek_seebar_value.setProgress(mFlyActionBean.getSpeek());
ridus_seebar_value.setProgress(mFlyActionBean.getRidus());
angle_seebar_value.setProgress(mFlyActionBean.getStart_point_agle());
ridus_seebar_value.setMax((int) (maxRadius - distance));
poi_height.setText(mFlyActionBean.getHeight() + "m");
poispeek.setText(mFlyActionBean.getSpeek() + "m/s");
poiridusvalue.setText(mFlyActionBean.getRidus() + "m");
poianglevalue.setText(mFlyActionBean.getStart_point_agle() + "°");
if (mFlyActionBean.getPara1() == 0 || mFlyActionBean.getPara1() == 1) {
drone_direction.check(anticlockwise.getId());
} else if (mFlyActionBean.getPara1() == 2 || mFlyActionBean.getPara1() == 3) {
drone_direction.check(clockwise.getId());
}

setView(mFlyActionBean);
createMarker(false);
addPolineStartMarkerandPoiMarker();
}
}
}

private void addPolineStartMarkerandPoiMarker() {
FlyActionBean mFlyActionBean = DataHandler.getDataHandler().getmFlyActionBean();
if (null != startMarker && null != mFlyActionBean) {
if (null == mPolyline) {
mPolyline = aMap.addPolyline(new PolylineOptions().add(startMarker.getPosition(),
mFlyActionBean.getLatLng()).color(drone.context.getResources().
getColor(R.color.ridus_color)).width(4));
} else {
List<LatLng> latLngList = new ArrayList<LatLng>();
latLngList.add(mFlyActionBean.getLatLng());
latLngList.add(startMarker.getPosition());
mPolyline.setPoints(latLngList);
}

}
}


private double distanceDroneLatlngandHomeLatlng() {
Latitudelongitude homeLatitudelongitude = GpsCorrect.Earth_To_Mars(drone.getHomeatt().getHomeLatitude(), drone.getHomeatt().getHomeLongitude());
LatLng homeLatLng = new LatLng(homeLatitudelongitude.getLatitude(), homeLatitudelongitude.getLongitude());
Latitudelongitude droneLatitudelongitude = GpsCorrect.Earth_To_Mars(drone.getPert().getCurrentLatitude(), drone.getPert().getCurrentLongitude());
LatLng droneLatLng = new LatLng(droneLatitudelongitude.getLatitude(), droneLatitudelongitude.getLongitude());
double distance = GeoTools.getDistance(homeLatLng, droneLatLng).valueInMeters();
return distance;
}

;

private void showPopuDowSpinner() {
if (null == popupWindow) {
myspinner.setBackgroundResource(R.drawable.bg_fly_combobox_more_up);
View view = LayoutInflater.from(context).inflate(R.layout.myspinner_item, null);
ListView listView = (ListView) view.findViewById(R.id.list_head_angle);
MyAdapter mMyAdapter = new MyAdapter(context, head_angle);
listView.setAdapter(mMyAdapter);
listView.setSelection(1);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
FlyActionBean mFlyActionBean = DataHandler.getDataHandler().getmFlyActionBean();
String head_angle = (String) parent.getItemAtPosition(position);
myspinner.setText(head_angle);
if (position == 0) {
position = position + 1;
} else if (position == 1) {
position = position - 1;
}
if (null != mFlyActionBean) {
mFlyActionBean.setYaw_mode(position);
}
popupWindow.dismiss();
myspinner.setBackgroundResource(R.drawable.bg_fly_combobox_more_down);
}
});
popupWindow = new PopupWindow(listView, myspinner.getWidth(), myspinner.getHeight() * 4);
popupWindow.setContentView(view);
popupWindow.showAsDropDown(myspinner, 0, ConvertSize.pxToDip(context, -8));
popupWindow.setOutsideTouchable(true);
popupWindow.setTouchable(true);
popupWindow.setFocusable(true);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
myspinner.setBackgroundResource(R.drawable.bg_fly_combobox_more_down);
}
});
} else {
if (popupWindow.isShowing()) {
popupWindow.dismiss();
myspinner.setBackgroundResource(R.drawable.bg_fly_combobox_more_down);
} else {
myspinner.setBackgroundResource(R.drawable.bg_fly_combobox_more_up);
popupWindow.showAsDropDown(myspinner, 0, ConvertSize.pxToDip(context, -8));
}
}
}

//拖动小圆改变半斤,更新这边的半径
public synchronized void changeCircleRidus() {
FlyActionBean mFlyActionBean = DataHandler.getDataHandler().getmFlyActionBean();
if (null != chacheCircle && null != startMarker && null != mFlyActionBean) {
int distance = (int) GeoTools.getDistance(mFlyActionBean.getLatLng(), startMarker.getPosition()).valueInMeters();
mFlyActionBean.setRidus(distance);

//画圆
drawCircle(mFlyActionBean);
LatLng northLatlng = getLatLng(mFlyActionBean, 0);
double angle = 0;

//顺时针还是逆时针 重新算小圆和三角的角度
switch (drone_direction.getCheckedRadioButtonId()) {
case R.id.clockwise:
angle = GeoTools.getHeadingFromCoordinates(northLatlng, startMarker.getPosition());
break;
case R.id.anticlockwise:
angle = maxAngle - GeoTools.getHeadingFromCoordinates(northLatlng, startMarker.getPosition());
break;
}
mFlyActionBean.setStart_point_agle((short) angle);
poianglevalue.setText((int) angle + "deg");

//算好角度,更改小圆和三角所在的位置
createMarker(false);

}
}


public void hidePopupWindow() {
if (null != popupWindow && popupWindow.isShowing()) {
popupWindow.dismiss();
}
}


private float cachex,cachey;

@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
cachex=event.getX();
cachey=event.getY();
break;
case MotionEvent.ACTION_MOVE:
if(Math.abs( v.getX()-cachex)>=Math.abs(v.getY()-cachey)){
v.getParent().requestDisallowInterceptTouchEvent(true);
}
break;
case MotionEvent.ACTION_UP:
v.getParent().requestDisallowInterceptTouchEvent(false);
break;
}
return false;
}


class MyAdapter extends BaseAdapter {
private Context context;
private String[] headAngle;

public MyAdapter(Context context, String[] headAngle) {
this.context = context;
this.headAngle = headAngle;
}

@Override
public int getCount() {
return null == headAngle ? 0 : headAngle.length;
}

@Override
public Object getItem(int position) {
return null == headAngle ? null : headAngle[position];
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = LayoutInflater.from(context).inflate(R.layout.mysimple_spinner_dropdown_item, null);
TextView textView = (TextView) view.findViewById(R.id.itemangle);
LinearLayout.LayoutParams ll = (LinearLayout.LayoutParams) textView.getLayoutParams();
if (null != popupWindow) {
ll.width = popupWindow.getWidth();
}
textView.setLayoutParams(ll);
textView.setText(headAngle[position]);
SystemUtil.changeFontLanTing(context.getAssets(), textView);
return view;
}
}

private void moveMapIntoLeftCenter() {
int operaWidth = poi_operaRL.getWidth();
FlyActionBean mFlyActionBean = dataHandler.getmFlyActionBean();
int screenWidth = context.getResources().getDisplayMetrics().widthPixels;
int screenHeight = context.getResources().getDisplayMetrics().heightPixels;
AMap aMap = drone.getaMap();
if (null != aMap) {
Point currentPoint = aMap.getProjection().toScreenLocation(mFlyActionBean.getLatLng());
if (currentPoint.x > screenWidth / 2) {
Point point = new Point((currentPoint.x - screenWidth / 3 + screenWidth / 2), screenHeight / 2);
LatLng latLng = aMap.getProjection().fromScreenLocation(point);
aMap.moveCamera(CameraUpdateFactory.changeLatLng(latLng));
}
}
drawCircle(mFlyActionBean);
}

public void drawCircle(FlyActionBean mFlyActionBean) {
aMap = drone.getaMap();
if (null != aMap && null != mFlyActionBean) {
if (chacheCircle == null) {
LatLng latLng = dataHandler.getmFlyActionBean().getLatLng();
int ridus = mFlyActionBean.getRidus();
chacheCircle = aMap.addCircle(new CircleOptions().radius(ridus).
strokeWidth(4).strokeColor(context.getResources().
getColor(R.color.ridus_color)).center(latLng));
dataHandler.setPoiCircle(chacheCircle);
} else {
chacheCircle.setRadius(mFlyActionBean.getRidus());
}
}
}

/**
* 获取开始点
*
* @return
*/
private Point getStartCirclePoint(double angle) {
FlyActionBean flyActionBean = dataHandler.getmFlyActionBean();
aMap = drone.getaMap();
if (null != flyActionBean && null != aMap) {
float screenDensity = context.getResources().getDisplayMetrics().density;
//距离
float distance = (float) ((flyActionBean.getRidus() * screenDensity) / aMap.getScalePerPixel());
Point centerPoint = aMap.getProjection().toScreenLocation(flyActionBean.getLatLng());
Point startPoint = new Point((int) (distance * Math.sin(angle)) + centerPoint.x, -(int) ((distance) * Math.cos(angle)) + centerPoint.y);
return startPoint;
}
return null;
};



//两个点之间的坐标
public LatLng getLatLng(FlyActionBean flyActionBean, double angle) {
LatLng latLng = null;
if (null != flyActionBean && null != chacheCircle) {
latLng = GeoTools.newCoordFromBearingAndDistance(flyActionBean.getLatLng(), angle, chacheCircle.getRadius());
}
return latLng;
}


//改变小圆和三角marker
public void createMarker(boolean isDragMarker) {
FlyActionBean flyActionBean = dataHandler.getmFlyActionBean();
aMap = drone.getaMap();
if (null != flyActionBean && null != aMap) {
switch (drone_direction.getCheckedRadioButtonId()) {
case R.id.clockwise:
if (!isDragMarker) {
createStartMarker(flyActionBean, aMap, true);
}
createArrowMarker(flyActionBean, aMap, true);
break;
case R.id.anticlockwise:
if (!isDragMarker) {
createStartMarker(flyActionBean, aMap, false);
}
createArrowMarker(flyActionBean, aMap, false);
break;
}
}
}


/**
* 画起始点 更改小圆marker 
*
* @param flyActionBean
* @param aMap
* @param judge
*/
private void createStartMarker(FlyActionBean flyActionBean, AMap aMap, boolean judge) {
LatLng startLatLng = null;
if (judge) {
startLatLng = getLatLng(flyActionBean, flyActionBean.getStart_point_agle());
} else {
startLatLng = getLatLng(flyActionBean, -flyActionBean.getStart_point_agle());
}
if (startMarker == null) {
startMarker = aMap.addMarker(new MarkerOptions().position(startLatLng).icon(MyBitmapDescriptor.getBitmapDescriptor(R.drawable.icn_rotate_point)));
startMarker.setZIndex(10000);
startMarker.setAnchor(0.5f, 0.5f);
startMarker.setDraggable(false);
startMarker.setRotateAngle(bearAngle());
} else {
startMarker.setPosition(startLatLng);
startMarker.setRotateAngle(bearAngle());
}
addPolineStartMarkerandPoiMarker();
}


/**
* 画箭头更改三角形
*
* @param flyActionBean
* @param aMap
* @param judge
*/
private void createArrowMarker(FlyActionBean flyActionBean, AMap aMap, boolean judge) {
LatLng arrowLatLng = null;
if (judge) {
arrowLatLng = getLatLng(flyActionBean, flyActionBean.getStart_point_agle() + 90);
} else {
arrowLatLng = getLatLng(flyActionBean, 270 - flyActionBean.getStart_point_agle());
}
if (arrawMarker == null) {
arrawMarker = aMap.addMarker(new MarkerOptions().position(arrowLatLng).icon(MyBitmapDescriptor.getBitmapDescriptor(R.drawable.icon_fly_direction)));
arrawMarker.setZIndex(10000);
if (judge) {
arrawMarker.setRotateAngle(-flyActionBean.getStart_point_agle() - 180 + aMap.getCameraPosition().bearing);
} else {
arrawMarker.setRotateAngle(flyActionBean.getStart_point_agle() - 180 + aMap.getCameraPosition().bearing);
}
arrawMarker.setAnchor(0.5f, 0.5f);
} else {
arrawMarker.setPosition(arrowLatLng);
if (judge) {
arrawMarker.setRotateAngle(-flyActionBean.getStart_point_agle() - 180 + bearAngle());
} else {
arrawMarker.setRotateAngle(flyActionBean.getStart_point_agle() - 180 + bearAngle());
}
}
}


private void caluRunTime() {
FlyActionBean flyActionBean = DataHandler.getDataHandler().getmFlyActionBean();
if (null != chacheCircle && null != flyActionBean) {
int time = (int) (chacheCircle.getRadius() * 2 * Math.PI / flyActionBean.getSpeek());
spend_time.setText(String.format(context.getString(R.string.spend_timereminder), time));
}
}

//获取地图角度有可能会报错
private synchronized float bearAngle() {
try {
CameraPosition cameraPosition = aMap.getCameraPosition();
if (null != cameraPosition) {
return cameraPosition.bearing;
}
return 0;
} catch (Exception e) {

}
return 0;
}

}



0 0
原创粉丝点击