最近項(xiàng)目中用到了一個(gè)高德地圖畫地塊的功能,差了一下api,發(fā)現(xiàn)只有js有相關(guān)的方法,所以仿照js的交互方式做了一個(gè)android版的多邊形繪制與編輯燥滑。
先不說,上一張效果圖
1.通過觀察,我們大致能知道主要的繪制與編輯方式,點(diǎn)完第二個(gè)點(diǎn)之后,和第一個(gè)點(diǎn)連成線段,這個(gè)線段的中間位置會(huì)有一個(gè)小點(diǎn),下面是添加marker點(diǎn)的代碼
/**
* 添加marker
*/
private void addMarker(LatLng latLng) {
? ? if (allLatLngs.size() == 0) {
? ? ? ? MarkerOptions options= new MarkerOptions();
? ? ? ? options.position(latLng).draggable(false).visible(true);
? ? ? ? Marker marker= aMap.addMarker(options);
? ? ? ? marker.setObject((allLatLngs.size() + 1));
? ? ? ? marker.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.icon_dian_s));
? ? ? ? marker.setAnchor(0.5f, 0.5f);
? ? ? ? allLatLngs.add(new MyLatLng(latLng, MyLatLng.ABLE));
? ? ? ? allLatLngsWithLine.add(latLng);
? ? ? ? markers.add(marker);
? ? ? ? //畫線
? ? ? ? drawLine(latLng, null);
? ? } else {
? ? ? ? if (isEnd) {
? ? ? ? ? ? MarkerOptions options= new MarkerOptions();
? ? ? ? ? ? latLng= new LatLng(((latLng.latitude + allLatLngs.get(allLatLngs.size() - 1).getLatLng().latitude) / 2), ((latLng.longitude + allLatLngs.get(allLatLngs.size() - 1).getLatLng().longitude) / 2));
? ? ? ? ? ? options.position(latLng).draggable(false).visible(true);
? ? ? ? ? ? Marker marker= aMap.addMarker(options);
? ? ? ? ? ? marker.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.icon_dian_y));
? ? ? ? ? ? marker.setAnchor(0.5f, 0.5f);
? ? ? ? ? ? allLatLngs.add(new MyLatLng(latLng, MyLatLng.UNABLE));
? ? ? ? ? ? allLatLngsWithLine.add(latLng);
? ? ? ? ? ? markers.add(marker);
? ? ? ? ? ? drawLine(allLatLngsWithLine.get(allLatLngsWithLine.size() - 1), allLatLngsWithLine.get(0));
? ? ? ? } else {
? ? ? ? ? ? for (int i= 0; i< 2; i++) { // 在地圖上添一組圖片標(biāo)記(marker)對(duì)象腿宰,并設(shè)置是否改變地圖狀態(tài)以至于所有的marker對(duì)象都在當(dāng)前地圖可視區(qū)域范圍內(nèi)顯示
? ? ? ? ? ? ? ? MarkerOptions options= new MarkerOptions();
? ? ? ? ? ? ? ? if (i== 0) {
? ? ? ? ? ? ? ? ? ? LatLng latLng2= new LatLng(((latLng.latitude + allLatLngs.get(allLatLngs.size() - 1).getLatLng().latitude) / 2), ((latLng.longitude + allLatLngs.get(allLatLngs.size() - 1).getLatLng().longitude) / 2));
? ? ? ? ? ? ? ? ? ? options.position(latLng2).draggable(false).visible(true);
? ? ? ? ? ? ? ? ? ? allLatLngs.add(new MyLatLng(latLng2, MyLatLng.UNABLE));
? ? ? ? ? ? ? ? ? ? allLatLngsWithLine.add(latLng2);
? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? options.position(latLng).draggable(false).visible(true);
? ? ? ? ? ? ? ? ? ? allLatLngs.add(new MyLatLng(latLng, MyLatLng.ABLE));
? ? ? ? ? ? ? ? ? ? allLatLngsWithLine.add(latLng);
}
? ? ? ? ? ? ? ? Marker marker= aMap.addMarker(options);
? ? ? ? ? ? ? ? marker.setAnchor(0.5f, 0.5f);
? ? ? ? ? ? ? ? if (i== 1) {
? ? ? ? ? ? ? ? ? ? marker.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.icon_dian_s));
? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? marker.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.icon_dian_y));
}
? ? ? ? ? ? ? ? markers.add(marker);
}
? ? ? ? ? ? drawLine(allLatLngsWithLine.get(allLatLngsWithLine.size() - 2), allLatLngsWithLine.get(allLatLngsWithLine.size() - 1));
}
}
}
2.畫上marker點(diǎn)之后還要畫點(diǎn)之間的線,還有多邊形的填充色這里貼一下相關(guān)的代碼
/**
* 畫線
*/
private void drawLine(LatLng latLng, LatLng latLng2) {
? ? if (polyline == null) {
? ? ? ? polyline = aMap.addPolyline(new PolylineOptions().
? ? ? ? ? ? ? ? add(latLng).width(10).color(Color.argb(255, 1, 1, 1)));
? ? } else {
? ? ? ? List<LatLng> points= polyline.getPoints();
? ? ? ? if (isEnd) {
? ? ? ? ? ? points.add(latLng);
? ? ? ? ? ? points.add(latLng2);
? ? ? ? } else {
? ? ? ? ? ? if (!points.contains(latLng)) {
? ? ? ? ? ? ? ? points.add(latLng);
}
? ? ? ? ? ? if (!points.contains(latLng2)) {
? ? ? ? ? ? ? ? points.add(latLng2);
}
}
? ? ? ? polyline.setPoints(points);
? ? ? ? //計(jì)算周長(zhǎng)
? ? ? ? setPerimeter();
}
}
/**
* 畫多邊形
*/
private void drawRect() {
? ? if (polygon == null) {
? ? ? ? polygonOptions.addAll(allLatLngsWithLine);
? ? ? ? //創(chuàng)建多邊形
? ? ? ? polygon = aMap.addPolygon(polygonOptions);
? ? } else {
? ? ? ? polygon.setPoints(allLatLngsWithLine);
}
? ? //計(jì)算面積
? ? setArea();
}
3.是時(shí)候該拖動(dòng)marker點(diǎn)改變多邊形的形狀了.一開始我是準(zhǔn)備用marker點(diǎn)的監(jiān)聽做它的移動(dòng)監(jiān)聽的,發(fā)現(xiàn)marker的拖動(dòng)必須要長(zhǎng)按才能拖動(dòng),用戶體驗(yàn)并不好,所以就改成了地圖的觸摸監(jiān)聽.大致說一下思路:如果拖拽的是大點(diǎn),則改變其左右兩個(gè)小點(diǎn)的坐標(biāo)和與其相鄰的兩個(gè)大點(diǎn)間的線段,而拖拽小點(diǎn)則先將其變成一個(gè)大點(diǎn),然后在與其相鄰的兩個(gè)大點(diǎn)之間添加兩個(gè)小點(diǎn)就行.下面上關(guān)鍵代碼
//添加拖拽事件
aMap.setOnMapTouchListener(new AMap.OnMapTouchListener() {
? ? @Override
? ? public void onTouch(MotionEvent motionEvent) {
? ? ? ? switch (motionEvent.getAction()) {
? ? ? ? ? ? //按下
? ? ? ? ? ? case MotionEvent.ACTION_DOWN:
? ? ? ? ? ? ? ? float down_x= motionEvent.getX();
? ? ? ? ? ? ? ? float down_y= motionEvent.getY();
? ? ? ? ? ? ? ? Point downPoint= new Point();
? ? ? ? ? ? ? ? downPoint.set((int) down_x, (int) down_y);
? ? ? ? ? ? ? ? //獲取觸摸到的位置
? ? ? ? ? ? ? ? LatLng downLatLng= aMap.getProjection().fromScreenLocation(downPoint);
? ? ? ? ? ? ? ? //獲取觸摸的點(diǎn)下標(biāo)
? ? ? ? ? ? ? ? checkPos = getNearestLatLng(downLatLng);
? ? ? ? ? ? ? ? //觸摸的點(diǎn)是矩形的點(diǎn)
? ? ? ? ? ? ? ? if (checkPos > -1) {
? ? ? ? ? ? ? ? ? ? //如果是小點(diǎn)
? ? ? ? ? ? ? ? ? ? if (allLatLngs.get(checkPos).getState() == MyLatLng.UNABLE) {
? ? ? ? ? ? ? ? ? ? ? ? //更改為大點(diǎn)
? ? ? ? ? ? ? ? ? ? ? ? chageMarker();
? ? ? ? ? ? ? ? ? ? ? ? //如果是封閉圖形并且點(diǎn)擊的是最后的一個(gè)點(diǎn)
? ? ? ? ? ? ? ? ? ? ? ? if (isEnd && checkPos == allLatLngs.size() - 1) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? //添加兩個(gè)mark
? ? ? ? ? ? ? ? ? ? ? ? ? ? addTwoMarker(getCenterLatlng(allLatLngs.get(checkPos).getLatLng(), allLatLngs.get(checkPos - 1).getLatLng()), getCenterLatlng(allLatLngs.get(checkPos).getLatLng(), allLatLngs.get(0).getLatLng()));
? ? ? ? ? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? ? ? ? ? //不是封閉圖形
//添加兩個(gè)marker
? ? ? ? ? ? ? ? ? ? ? ? ? ? addTwoMarker(getCenterLatlng(allLatLngs.get(checkPos).getLatLng(), allLatLngs.get(checkPos - 1).getLatLng()), getCenterLatlng(allLatLngs.get(checkPos).getLatLng(), allLatLngs.get(checkPos + 1).getLatLng()));
}
? ? ? ? ? ? ? ? ? ? ? ? //將選中點(diǎn)的下標(biāo)更改
? ? ? ? ? ? ? ? ? ? ? ? checkPos += 1;
}
? ? ? ? ? ? ? ? ? ? //獲取選中的marker點(diǎn)
? ? ? ? ? ? ? ? ? ? touchMark = markers.get(checkPos);
? ? ? ? ? ? ? ? ? ? //禁止地圖放大旋轉(zhuǎn)等操作
? ? ? ? ? ? ? ? ? ? uiSettings.setScrollGesturesEnabled(false);
}
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? //移動(dòng)中
? ? ? ? ? ? case MotionEvent.ACTION_MOVE:
? ? ? ? ? ? ? ? //有選中的marker點(diǎn)
? ? ? ? ? ? ? ? if (touchMark != null) {
? ? ? ? ? ? ? ? ? ? float move_x= motionEvent.getX();
? ? ? ? ? ? ? ? ? ? float move_y= motionEvent.getY();
? ? ? ? ? ? ? ? ? ? Point movePoint= new Point();
? ? ? ? ? ? ? ? ? ? movePoint.set((int) move_x, (int) move_y);
? ? ? ? ? ? ? ? ? ? //獲取到觸摸點(diǎn)經(jīng)緯度
? ? ? ? ? ? ? ? ? ? LatLng moveLatLng= aMap.getProjection().fromScreenLocation(movePoint);
? ? ? ? ? ? ? ? ? ? //更新的坐標(biāo)點(diǎn)位置
? ? ? ? ? ? ? ? ? ? touchMark.setPosition(moveLatLng);
? ? ? ? ? ? ? ? ? ? //如果已經(jīng)畫出線(兩個(gè)大點(diǎn))
? ? ? ? ? ? ? ? ? ? if (polyline != null) {
? ? ? ? ? ? ? ? ? ? ? ? //會(huì)比markers多一個(gè)點(diǎn)
? ? ? ? ? ? ? ? ? ? ? ? List<LatLng> points= polyline.getPoints();
? ? ? ? ? ? ? ? ? ? ? ? //修改線數(shù)據(jù)中當(dāng)前觸摸點(diǎn)的坐標(biāo)信息
? ? ? ? ? ? ? ? ? ? ? ? points.set(checkPos, moveLatLng);
? ? ? ? ? ? ? ? ? ? ? ? //修改當(dāng)前選中marker點(diǎn)坐標(biāo)集合的信息
? ? ? ? ? ? ? ? ? ? ? ? allLatLngs.get(checkPos).setLatLng(moveLatLng);
? ? ? ? ? ? ? ? ? ? ? ? //修改當(dāng)前選中線的點(diǎn)的坐標(biāo)集合的信息
? ? ? ? ? ? ? ? ? ? ? ? allLatLngsWithLine.set(checkPos, moveLatLng);
? ? ? ? ? ? ? ? ? ? ? ? //不需要添加兩個(gè)點(diǎn)
? ? ? ? ? ? ? ? ? ? ? ? if (checkPos == 0) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? //獲取選中大點(diǎn)旁邊的小點(diǎn)
? ? ? ? ? ? ? ? ? ? ? ? ? ? Marker marker= markers.get(checkPos + 1);
? ? ? ? ? ? ? ? ? ? ? ? ? ? //獲取第一個(gè)大點(diǎn)和第二個(gè)大點(diǎn)的中間坐標(biāo)
? ? ? ? ? ? ? ? ? ? ? ? ? ? LatLng center= getCenterLatlng(moveLatLng, allLatLngs.get(checkPos + 2).getLatLng());
? ? ? ? ? ? ? ? ? ? ? ? ? ? //修改marker的坐標(biāo)
? ? ? ? ? ? ? ? ? ? ? ? ? ? marker.setPosition(center);
? ? ? ? ? ? ? ? ? ? ? ? ? ? //修改線的坐標(biāo)
? ? ? ? ? ? ? ? ? ? ? ? ? ? points.set(checkPos + 1, center);
? ? ? ? ? ? ? ? ? ? ? ? ? ? //修改總marker坐標(biāo)集合信息
? ? ? ? ? ? ? ? ? ? ? ? ? ? allLatLngs.get(checkPos + 1).setLatLng(center);
? ? ? ? ? ? ? ? ? ? ? ? ? ? //修改總線集合信息
? ? ? ? ? ? ? ? ? ? ? ? ? ? allLatLngsWithLine.set(checkPos + 1, center);
? ? ? ? ? ? ? ? ? ? ? ? ? ? //如果是已經(jīng)封閉的則需要修改最后一個(gè)大點(diǎn)與第一個(gè)大點(diǎn)中間點(diǎn)的坐標(biāo)
? ? ? ? ? ? ? ? ? ? ? ? ? ? if (isEnd) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //操作同上
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Marker marker2= markers.get(markers.size() - 1);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? LatLng cen= getCenterLatlng(moveLatLng, allLatLngs.get(markers.size() - 2).getLatLng());
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? marker2.setPosition(cen);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? points.set(points.size() - 1, moveLatLng);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? points.set(points.size() - 2, cen);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? allLatLngs.get(markers.size() - 1).setLatLng(cen);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? allLatLngsWithLine.set(markers.size() - 1, cen);
}
? ? ? ? ? ? ? ? ? ? ? ? ? ? //當(dāng)觸摸的點(diǎn)是最后一個(gè)大點(diǎn)或者最后一個(gè)小點(diǎn)的時(shí)候
? ? ? ? ? ? ? ? ? ? ? ? } else if (checkPos == markers.size() - 2 || checkPos == markers.size() - 1) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? //原理同上
//最后一個(gè)點(diǎn)
? ? ? ? ? ? ? ? ? ? ? ? ? ? Marker marker2= markers.get(checkPos - 1);
? ? ? ? ? ? ? ? ? ? ? ? ? ? LatLng center= getCenterLatlng(moveLatLng, allLatLngs.get(checkPos - 2).getLatLng());
? ? ? ? ? ? ? ? ? ? ? ? ? ? marker2.setPosition(center);
? ? ? ? ? ? ? ? ? ? ? ? ? ? points.set(checkPos - 1, center);
? ? ? ? ? ? ? ? ? ? ? ? ? ? allLatLngs.get(checkPos - 1).setLatLng(center);
? ? ? ? ? ? ? ? ? ? ? ? ? ? allLatLngsWithLine.set(checkPos - 1, center);
? ? ? ? ? ? ? ? ? ? ? ? ? ? if (isEnd) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Marker marker= markers.get(checkPos + 1);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? LatLng cen= getCenterLatlng(moveLatLng, allLatLngs.get(0).getLatLng());
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? marker.setPosition(cen);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? points.set(checkPos + 1, cen);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? allLatLngs.get(checkPos + 1).setLatLng(cen);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? allLatLngsWithLine.set(checkPos + 1, cen);
}
? ? ? ? ? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? ? ? ? ? //原理同上
//中間的點(diǎn)
? ? ? ? ? ? ? ? ? ? ? ? ? ? Marker marker= markers.get(checkPos + 1);
? ? ? ? ? ? ? ? ? ? ? ? ? ? LatLng center= getCenterLatlng(moveLatLng, allLatLngs.get(checkPos + 2).getLatLng());
? ? ? ? ? ? ? ? ? ? ? ? ? ? marker.setPosition(center);
? ? ? ? ? ? ? ? ? ? ? ? ? ? allLatLngs.get(checkPos + 1).setLatLng(center);
? ? ? ? ? ? ? ? ? ? ? ? ? ? allLatLngsWithLine.set(checkPos + 1, center);
? ? ? ? ? ? ? ? ? ? ? ? ? ? Marker marker2= markers.get(checkPos - 1);
? ? ? ? ? ? ? ? ? ? ? ? ? ? LatLng center2= getCenterLatlng(moveLatLng, allLatLngs.get(checkPos - 2).getLatLng());
? ? ? ? ? ? ? ? ? ? ? ? ? ? marker2.setPosition(center2);
? ? ? ? ? ? ? ? ? ? ? ? ? ? allLatLngs.get(checkPos - 1).setLatLng(center2);
? ? ? ? ? ? ? ? ? ? ? ? ? ? allLatLngsWithLine.set(checkPos - 1, center2);
? ? ? ? ? ? ? ? ? ? ? ? ? ? //移動(dòng)線
? ? ? ? ? ? ? ? ? ? ? ? ? ? points.set(checkPos + 1, center);
? ? ? ? ? ? ? ? ? ? ? ? ? ? points.set(checkPos - 1, center2);
}
? ? ? ? ? ? ? ? ? ? ? ? //更改線數(shù)據(jù)
? ? ? ? ? ? ? ? ? ? ? ? polyline.setPoints(points);
? ? ? ? ? ? ? ? ? ? ? ? //計(jì)算周長(zhǎng)
? ? ? ? ? ? ? ? ? ? ? ? setPerimeter();
? ? ? ? ? ? ? ? ? ? ? ? //如果封閉的話
? ? ? ? ? ? ? ? ? ? ? ? if (isEnd) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? //計(jì)算面積
? ? ? ? ? ? ? ? ? ? ? ? ? ? drawRect();
}
}
}
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? //抬起
? ? ? ? ? ? case MotionEvent.ACTION_UP:
? ? ? ? ? ? ? ? if (touchMark != null) {
? ? ? ? ? ? ? ? ? ? //清除選中點(diǎn)信息
? ? ? ? ? ? ? ? ? ? touchMark = null;
? ? ? ? ? ? ? ? ? ? //恢復(fù)地圖操作
? ? ? ? ? ? ? ? ? ? uiSettings.setScrollGesturesEnabled(true);
}
? ? ? ? ? ? ? ? break;
}
}
});
4.下面貼出完整代碼
package com.amap.map3d.demo.basic;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.Point;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.MotionEvent;
import android.widget.TextView;
import com.amap.api.maps.AMap;
import com.amap.api.maps.AMapUtils;
import com.amap.api.maps.TextureMapView;
import com.amap.api.maps.UiSettings;
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.Polygon;
import com.amap.api.maps.model.PolygonOptions;
import com.amap.api.maps.model.Polyline;
import com.amap.api.maps.model.PolylineOptions;
import com.amap.map3d.demo.MyLatLng;
import com.amap.map3d.demo.R;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
/**
* AMapV2地圖中介紹如何TextureMapView顯示一個(gè)基本地圖
*/
public class TextureMapViewActivity extends Activity {
? ? private TextureMapView mapView;
? ? private AMap aMap;
? ? //存放所有marker點(diǎn)標(biāo)記
? ? private List<MyLatLng> allLatLngs = new ArrayList<>();
? ? //所有線段的點(diǎn)
? ? private List<LatLng> allLatLngsWithLine = new ArrayList<>();
? ? //所有marker點(diǎn)
? ? private List<Marker> markers = new ArrayList<>();
? ? //線的對(duì)象
? ? private Polyline polyline;
? ? //多邊形的對(duì)象
? ? private Polygon polygon;
? ? //是否為封閉圖形
? ? private boolean isEnd = false;
? ? //矩形對(duì)象的配置
? ? private PolygonOptions polygonOptions = new PolygonOptions();
? ? //當(dāng)前觸摸的marker
? ? private Marker touchMark;
? ? //當(dāng)前選中的點(diǎn)下標(biāo),相對(duì)于allLatLngs的
? ? private int checkPos;
? ? //地圖設(shè)置
? ? UiSettings uiSettings;
? ? ///周長(zhǎng)
? ? private TextView mPerimeterTv;
? ? //面積
? ? private TextView mAreaTv;
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.basic_texturemap_activity);
? ? ? ? mPerimeterTv = findViewById(R.id.perimeter);
? ? ? ? mAreaTv = findViewById(R.id.area);
? ? ? ? /*
* 設(shè)置離線地圖存儲(chǔ)目錄,在下載離線地圖或初始化地圖設(shè)置;
* 使用過程中可自行設(shè)置, 若自行設(shè)置了離線地圖存儲(chǔ)的路徑,
* 則需要在離線地圖下載和使用地圖頁面都進(jìn)行路徑設(shè)置
* */
? ? ? ? //Demo中為了其他界面可以使用下載的離線地圖,使用默認(rèn)位置存儲(chǔ)鸠匀,屏蔽了自定義設(shè)置
//? MapsInitializer.sdcardDir =OffLineMapUtils.getSdCacheDir(this);
? ? ? ? mapView = (TextureMapView) findViewById(R.id.map);
? ? ? ? mapView.onCreate(savedInstanceState);// 此方法必須重寫
? ? ? ? init();
}
? ? /**
* 初始化AMap對(duì)象
*/
? ? private void init() {
? ? ? ? if (aMap == null) {
? ? ? ? ? ? aMap = mapView.getMap();
}
? ? ? ? //獲取地圖ui設(shè)置對(duì)象
? ? ? ? uiSettings = aMap.getUiSettings();
? ? ? ? //地圖點(diǎn)擊監(jiān)聽
? ? ? ? aMap.setOnMapClickListener(new AMap.OnMapClickListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onMapClick(LatLng latLng) {
? ? ? ? ? ? ? ? if (!isEnd) {
? ? ? ? ? ? ? ? ? ? addMarker(latLng);
}
}
? ? ? ? });
? ? ? ? //marker點(diǎn)擊監(jiān)聽
? ? ? ? aMap.setOnMarkerClickListener(new AMap.OnMarkerClickListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public boolean onMarkerClick(Marker marker) {
? ? ? ? ? ? ? ? if (isEnd) {
? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? if (markers.get(0).equals(marker)) {
? ? ? ? ? ? ? ? ? ? ? ? //封閉圖形
? ? ? ? ? ? ? ? ? ? ? ? isEnd = true;
? ? ? ? ? ? ? ? ? ? ? ? //初始化多邊形參數(shù)
? ? ? ? ? ? ? ? ? ? ? ? createAreaStyle();
? ? ? ? ? ? ? ? ? ? ? ? //添加marker
? ? ? ? ? ? ? ? ? ? ? ? addMarker(marker.getPosition());
? ? ? ? ? ? ? ? ? ? ? ? //畫多邊形
? ? ? ? ? ? ? ? ? ? ? ? drawRect();
? ? ? ? ? ? ? ? ? ? ? ? return true;
? ? ? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? ? ? //未封閉,點(diǎn)擊其它marke
? ? ? ? ? ? ? ? ? ? }
}
? ? ? ? ? ? ? ? return false;
}
? ? ? ? });
? ? ? ? //添加拖拽事件
? ? ? ? aMap.setOnMapTouchListener(new AMap.OnMapTouchListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onTouch(MotionEvent motionEvent) {
? ? ? ? ? ? ? ? switch (motionEvent.getAction()) {
? ? ? ? ? ? ? ? ? ? //按下
? ? ? ? ? ? ? ? ? ? case MotionEvent.ACTION_DOWN:
? ? ? ? ? ? ? ? ? ? ? ? float down_x= motionEvent.getX();
? ? ? ? ? ? ? ? ? ? ? ? float down_y= motionEvent.getY();
? ? ? ? ? ? ? ? ? ? ? ? Point downPoint= new Point();
? ? ? ? ? ? ? ? ? ? ? ? downPoint.set((int) down_x, (int) down_y);
? ? ? ? ? ? ? ? ? ? ? ? //獲取觸摸到的位置
? ? ? ? ? ? ? ? ? ? ? ? LatLng downLatLng= aMap.getProjection().fromScreenLocation(downPoint);
? ? ? ? ? ? ? ? ? ? ? ? //獲取觸摸的點(diǎn)下標(biāo)
? ? ? ? ? ? ? ? ? ? ? ? checkPos = getNearestLatLng(downLatLng);
? ? ? ? ? ? ? ? ? ? ? ? //觸摸的點(diǎn)是矩形的點(diǎn)
? ? ? ? ? ? ? ? ? ? ? ? if (checkPos > -1) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? //如果是小點(diǎn)
? ? ? ? ? ? ? ? ? ? ? ? ? ? if (allLatLngs.get(checkPos).getState() == MyLatLng.UNABLE) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //更改為大點(diǎn)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? chageMarker();
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //如果是封閉圖形并且點(diǎn)擊的是最后的一個(gè)點(diǎn)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (isEnd && checkPos == allLatLngs.size() - 1) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //添加兩個(gè)mark
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? addTwoMarker(getCenterLatlng(allLatLngs.get(checkPos).getLatLng(), allLatLngs.get(checkPos - 1).getLatLng()), getCenterLatlng(allLatLngs.get(checkPos).getLatLng(), allLatLngs.get(0).getLatLng()));
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //不是封閉圖形
//添加兩個(gè)marker
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? addTwoMarker(getCenterLatlng(allLatLngs.get(checkPos).getLatLng(), allLatLngs.get(checkPos - 1).getLatLng()), getCenterLatlng(allLatLngs.get(checkPos).getLatLng(), allLatLngs.get(checkPos + 1).getLatLng()));
}
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //將選中點(diǎn)的下標(biāo)更改
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? checkPos += 1;
}
? ? ? ? ? ? ? ? ? ? ? ? ? ? //獲取選中的marker點(diǎn)
? ? ? ? ? ? ? ? ? ? ? ? ? ? touchMark = markers.get(checkPos);
? ? ? ? ? ? ? ? ? ? ? ? ? ? //禁止地圖放大旋轉(zhuǎn)等操作
? ? ? ? ? ? ? ? ? ? ? ? ? ? uiSettings.setScrollGesturesEnabled(false);
}
? ? ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? ? ? ? ? //移動(dòng)中
? ? ? ? ? ? ? ? ? ? case MotionEvent.ACTION_MOVE:
? ? ? ? ? ? ? ? ? ? ? ? //有選中的marker點(diǎn)
? ? ? ? ? ? ? ? ? ? ? ? if (touchMark != null) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? float move_x= motionEvent.getX();
? ? ? ? ? ? ? ? ? ? ? ? ? ? float move_y= motionEvent.getY();
? ? ? ? ? ? ? ? ? ? ? ? ? ? Point movePoint= new Point();
? ? ? ? ? ? ? ? ? ? ? ? ? ? movePoint.set((int) move_x, (int) move_y);
? ? ? ? ? ? ? ? ? ? ? ? ? ? //獲取到觸摸點(diǎn)經(jīng)緯度
? ? ? ? ? ? ? ? ? ? ? ? ? ? LatLng moveLatLng= aMap.getProjection().fromScreenLocation(movePoint);
? ? ? ? ? ? ? ? ? ? ? ? ? ? //更新的坐標(biāo)點(diǎn)位置
? ? ? ? ? ? ? ? ? ? ? ? ? ? touchMark.setPosition(moveLatLng);
? ? ? ? ? ? ? ? ? ? ? ? ? ? //如果已經(jīng)畫出線(兩個(gè)大點(diǎn))
? ? ? ? ? ? ? ? ? ? ? ? ? ? if (polyline != null) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //會(huì)比markers多一個(gè)點(diǎn)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? List<LatLng> points= polyline.getPoints();
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //修改線數(shù)據(jù)中當(dāng)前觸摸點(diǎn)的坐標(biāo)信息
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? points.set(checkPos, moveLatLng);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //修改當(dāng)前選中marker點(diǎn)坐標(biāo)集合的信息
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? allLatLngs.get(checkPos).setLatLng(moveLatLng);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //修改當(dāng)前選中線的點(diǎn)的坐標(biāo)集合的信息
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? allLatLngsWithLine.set(checkPos, moveLatLng);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //不需要添加兩個(gè)點(diǎn)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (checkPos == 0) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //獲取選中大點(diǎn)旁邊的小點(diǎn)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Marker marker= markers.get(checkPos + 1);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //獲取第一個(gè)大點(diǎn)和第二個(gè)大點(diǎn)的中間坐標(biāo)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? LatLng center= getCenterLatlng(moveLatLng, allLatLngs.get(checkPos + 2).getLatLng());
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //修改marker的坐標(biāo)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? marker.setPosition(center);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //修改線的坐標(biāo)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? points.set(checkPos + 1, center);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //修改總marker坐標(biāo)集合信息
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? allLatLngs.get(checkPos + 1).setLatLng(center);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //修改總線集合信息
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? allLatLngsWithLine.set(checkPos + 1, center);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //如果是已經(jīng)封閉的則需要修改最后一個(gè)大點(diǎn)與第一個(gè)大點(diǎn)中間點(diǎn)的坐標(biāo)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (isEnd) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //操作同上
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Marker marker2= markers.get(markers.size() - 1);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? LatLng cen= getCenterLatlng(moveLatLng, allLatLngs.get(markers.size() - 2).getLatLng());
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? marker2.setPosition(cen);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? points.set(points.size() - 1, moveLatLng);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? points.set(points.size() - 2, cen);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? allLatLngs.get(markers.size() - 1).setLatLng(cen);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? allLatLngsWithLine.set(markers.size() - 1, cen);
}
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //當(dāng)觸摸的點(diǎn)是最后一個(gè)大點(diǎn)或者最后一個(gè)小點(diǎn)的時(shí)候
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } else if (checkPos == markers.size() - 2 || checkPos == markers.size() - 1) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //原理同上
//最后一個(gè)點(diǎn)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Marker marker2= markers.get(checkPos - 1);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? LatLng center= getCenterLatlng(moveLatLng, allLatLngs.get(checkPos - 2).getLatLng());
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? marker2.setPosition(center);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? points.set(checkPos - 1, center);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? allLatLngs.get(checkPos - 1).setLatLng(center);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? allLatLngsWithLine.set(checkPos - 1, center);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (isEnd) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Marker marker= markers.get(checkPos + 1);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? LatLng cen= getCenterLatlng(moveLatLng, allLatLngs.get(0).getLatLng());
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? marker.setPosition(cen);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? points.set(checkPos + 1, cen);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? allLatLngs.get(checkPos + 1).setLatLng(cen);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? allLatLngsWithLine.set(checkPos + 1, cen);
}
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //原理同上
//中間的點(diǎn)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Marker marker= markers.get(checkPos + 1);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? LatLng center= getCenterLatlng(moveLatLng, allLatLngs.get(checkPos + 2).getLatLng());
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? marker.setPosition(center);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? allLatLngs.get(checkPos + 1).setLatLng(center);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? allLatLngsWithLine.set(checkPos + 1, center);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Marker marker2= markers.get(checkPos - 1);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? LatLng center2= getCenterLatlng(moveLatLng, allLatLngs.get(checkPos - 2).getLatLng());
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? marker2.setPosition(center2);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? allLatLngs.get(checkPos - 1).setLatLng(center2);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? allLatLngsWithLine.set(checkPos - 1, center2);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //移動(dòng)線
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? points.set(checkPos + 1, center);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? points.set(checkPos - 1, center2);
}
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //更改線數(shù)據(jù)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? polyline.setPoints(points);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //計(jì)算周長(zhǎng)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? setPerimeter();
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //如果封閉的話
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (isEnd) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //計(jì)算面積
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? drawRect();
}
}
}
? ? ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? ? ? ? ? //抬起
? ? ? ? ? ? ? ? ? ? case MotionEvent.ACTION_UP:
? ? ? ? ? ? ? ? ? ? ? ? if (touchMark != null) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? //清除選中點(diǎn)信息
? ? ? ? ? ? ? ? ? ? ? ? ? ? touchMark = null;
? ? ? ? ? ? ? ? ? ? ? ? ? ? //恢復(fù)地圖操作
? ? ? ? ? ? ? ? ? ? ? ? ? ? uiSettings.setScrollGesturesEnabled(true);
}
? ? ? ? ? ? ? ? ? ? ? ? break;
}
}
? ? ? ? });
}
? ? /**
* 獲取兩個(gè)點(diǎn)的中點(diǎn)坐標(biāo)
*
? ? * @parammyLatLng
? ? * @parammyLatLng2
? ? * @return
*/
? ? private LatLng getCenterLatlng(LatLng myLatLng, LatLng myLatLng2) {
? ? ? ? return new LatLng((myLatLng.latitude + myLatLng2.latitude) / 2, (myLatLng.longitude + myLatLng2.longitude) / 2);
}
? ? /**
* 修改makeer類型和icon
*/
? ? private void chageMarker() {
? ? ? ? Marker marker= markers.get(checkPos);
? ? ? ? //修改點(diǎn)類型為大點(diǎn)
? ? ? ? allLatLngs.get(checkPos).setState(MyLatLng.ABLE);
? ? ? ? marker.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.icon_dian_s));
}
? ? /**
* 在拖拽點(diǎn)兩側(cè)添加maker
*/
? ? private void addTwoMarker(LatLng latLng, LatLng latLng2) {
? ? ? ? List<LatLng> points= polyline.getPoints();
? ? ? ? //先添加2再添加1
? ? ? ? MarkerOptions options= new MarkerOptions();
? ? ? ? options.position(latLng2).draggable(false).visible(true);
? ? ? ? Marker marker= aMap.addMarker(options);
? ? ? ? marker.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.icon_dian_y));
? ? ? ? //設(shè)置偏移
? ? ? ? marker.setAnchor(0.5f, 0.5f);
? ? ? ? //如果是最后一個(gè)點(diǎn)
? ? ? ? if (isEnd && checkPos == allLatLngs.size() - 1) {
? ? ? ? ? ? //直接把點(diǎn)信息添加進(jìn)去
? ? ? ? ? ? allLatLngs.add(new MyLatLng(latLng2, MyLatLng.UNABLE));
? ? ? ? ? ? allLatLngsWithLine.add(latLng2);
? ? ? ? ? ? markers.add(marker);
? ? ? ? } else {
? ? ? ? ? ? //按位置插入點(diǎn)信息
? ? ? ? ? ? allLatLngs.add(checkPos + 1, new MyLatLng(latLng2, MyLatLng.UNABLE));
? ? ? ? ? ? allLatLngsWithLine.add(checkPos + 1, latLng2);
? ? ? ? ? ? markers.add(checkPos + 1, marker);
}
? ? ? ? points.add(checkPos + 1, latLng2);
? ? ? ? MarkerOptions options2= new MarkerOptions();
? ? ? ? options2.position(latLng).draggable(false).visible(true);
? ? ? ? Marker marker2= aMap.addMarker(options2);
? ? ? ? marker2.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.icon_dian_y));
? ? ? ? marker2.setAnchor(0.5f, 0.5f);
? ? ? ? allLatLngs.add(checkPos, new MyLatLng(latLng, MyLatLng.UNABLE));
? ? ? ? allLatLngsWithLine.add(checkPos, latLng);
? ? ? ? markers.add(checkPos, marker2);
? ? ? ? points.add(checkPos, latLng);
? ? ? ? polyline.setPoints(points);
}
? ? /**
* 方法必須重寫
*/
? ? @Override
? ? protected void onResume() {
? ? ? ? super.onResume();
? ? ? ? mapView.onResume();
}
? ? /**
* 方法必須重寫
*/
? ? @Override
? ? protected void onPause() {
? ? ? ? super.onPause();
? ? ? ? mapView.onPause();
}
? ? /**
* 方法必須重寫
*/
? ? @Override
? ? protected void onSaveInstanceState(Bundle outState) {
? ? ? ? super.onSaveInstanceState(outState);
? ? ? ? mapView.onSaveInstanceState(outState);
}
? ? /**
* 方法必須重寫
*/
? ? @Override
? ? protected void onDestroy() {
? ? ? ? super.onDestroy();
? ? ? ? mapView.onDestroy();
}
? ? /**
* 添加marker
*/
? ? private void addMarker(LatLng latLng) {
? ? ? ? if (allLatLngs.size() == 0) {
? ? ? ? ? ? MarkerOptions options= new MarkerOptions();
? ? ? ? ? ? options.position(latLng).draggable(false).visible(true);
? ? ? ? ? ? Marker marker= aMap.addMarker(options);
? ? ? ? ? ? marker.setObject((allLatLngs.size() + 1));
? ? ? ? ? ? marker.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.icon_dian_s));
? ? ? ? ? ? marker.setAnchor(0.5f, 0.5f);
? ? ? ? ? ? allLatLngs.add(new MyLatLng(latLng, MyLatLng.ABLE));
? ? ? ? ? ? allLatLngsWithLine.add(latLng);
? ? ? ? ? ? markers.add(marker);
? ? ? ? ? ? //畫線
? ? ? ? ? ? drawLine(latLng, null);
? ? ? ? } else {
? ? ? ? ? ? if (isEnd) {
? ? ? ? ? ? ? ? MarkerOptions options= new MarkerOptions();
? ? ? ? ? ? ? ? latLng= new LatLng(((latLng.latitude + allLatLngs.get(allLatLngs.size() - 1).getLatLng().latitude) / 2), ((latLng.longitude + allLatLngs.get(allLatLngs.size() - 1).getLatLng().longitude) / 2));
? ? ? ? ? ? ? ? options.position(latLng).draggable(false).visible(true);
? ? ? ? ? ? ? ? Marker marker= aMap.addMarker(options);
? ? ? ? ? ? ? ? marker.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.icon_dian_y));
? ? ? ? ? ? ? ? marker.setAnchor(0.5f, 0.5f);
? ? ? ? ? ? ? ? allLatLngs.add(new MyLatLng(latLng, MyLatLng.UNABLE));
? ? ? ? ? ? ? ? allLatLngsWithLine.add(latLng);
? ? ? ? ? ? ? ? markers.add(marker);
? ? ? ? ? ? ? ? drawLine(allLatLngsWithLine.get(allLatLngsWithLine.size() - 1), allLatLngsWithLine.get(0));
? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? for (int i= 0; i< 2; i++) { // 在地圖上添一組圖片標(biāo)記(marker)對(duì)象,并設(shè)置是否改變地圖狀態(tài)以至于所有的marker對(duì)象都在當(dāng)前地圖可視區(qū)域范圍內(nèi)顯示
? ? ? ? ? ? ? ? ? ? MarkerOptions options= new MarkerOptions();
? ? ? ? ? ? ? ? ? ? if (i== 0) {
? ? ? ? ? ? ? ? ? ? ? ? LatLng latLng2= new LatLng(((latLng.latitude + allLatLngs.get(allLatLngs.size() - 1).getLatLng().latitude) / 2), ((latLng.longitude + allLatLngs.get(allLatLngs.size() - 1).getLatLng().longitude) / 2));
? ? ? ? ? ? ? ? ? ? ? ? options.position(latLng2).draggable(false).visible(true);
? ? ? ? ? ? ? ? ? ? ? ? allLatLngs.add(new MyLatLng(latLng2, MyLatLng.UNABLE));
? ? ? ? ? ? ? ? ? ? ? ? allLatLngsWithLine.add(latLng2);
? ? ? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? ? ? options.position(latLng).draggable(false).visible(true);
? ? ? ? ? ? ? ? ? ? ? ? allLatLngs.add(new MyLatLng(latLng, MyLatLng.ABLE));
? ? ? ? ? ? ? ? ? ? ? ? allLatLngsWithLine.add(latLng);
}
? ? ? ? ? ? ? ? ? ? Marker marker= aMap.addMarker(options);
? ? ? ? ? ? ? ? ? ? marker.setAnchor(0.5f, 0.5f);
? ? ? ? ? ? ? ? ? ? if (i== 1) {
? ? ? ? ? ? ? ? ? ? ? ? marker.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.icon_dian_s));
? ? ? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? ? ? marker.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.icon_dian_y));
}
? ? ? ? ? ? ? ? ? ? markers.add(marker);
}
? ? ? ? ? ? ? ? drawLine(allLatLngsWithLine.get(allLatLngsWithLine.size() - 2), allLatLngsWithLine.get(allLatLngsWithLine.size() - 1));
}
}
}
? ? /**
* 計(jì)算周長(zhǎng)
*/
? ? private void? setPerimeter(){
? ? ? ? List<LatLng> points= polyline.getPoints();
? ? ? ? float f= 0;
? ? ? ? for (int i= 0; i< points.size() - 1; i++) {
? ? ? ? ? ? f= f+ AMapUtils.calculateLineDistance(points.get(i), points.get(i+ 1));
}
? ? ? ? mPerimeterTv.setText("周長(zhǎng):" + f+"米");
}
? ? /**
* 計(jì)算面積
*/
? ? private void setArea(){
? ? ? ? List<LatLng> points2= new ArrayList<>();
? ? ? ? List<LatLng> points= polygon.getPoints();
? ? ? ? points2.addAll(points);
? ? ? ? BigDecimal bigDecimal= polygon_area(points2);
? ? ? ? mAreaTv.setText("面積:"+bigDecimal+"km2");
}
? ? /**
* 畫線
*/
? ? private void drawLine(LatLng latLng, LatLng latLng2) {
? ? ? ? if (polyline == null) {
? ? ? ? ? ? polyline = aMap.addPolyline(new PolylineOptions().
? ? ? ? ? ? ? ? ? ? add(latLng).width(10).color(Color.argb(255, 1, 1, 1)));
? ? ? ? } else {
? ? ? ? ? ? List<LatLng> points= polyline.getPoints();
? ? ? ? ? ? if (isEnd) {
? ? ? ? ? ? ? ? points.add(latLng);
? ? ? ? ? ? ? ? points.add(latLng2);
? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? if (!points.contains(latLng)) {
? ? ? ? ? ? ? ? ? ? points.add(latLng);
}
? ? ? ? ? ? ? ? if (!points.contains(latLng2)) {
? ? ? ? ? ? ? ? ? ? points.add(latLng2);
}
}
? ? ? ? ? ? polyline.setPoints(points);
? ? ? ? ? ? //計(jì)算周長(zhǎng)
? ? ? ? ? ? setPerimeter();
}
}
? ? /**
* 畫多邊形
*/
? ? private void drawRect() {
? ? ? ? if (polygon == null) {
? ? ? ? ? ? polygonOptions.addAll(allLatLngsWithLine);
? ? ? ? ? ? //創(chuàng)建多邊形
? ? ? ? ? ? polygon = aMap.addPolygon(polygonOptions);
? ? ? ? } else {
? ? ? ? ? ? polygon.setPoints(allLatLngsWithLine);
}
? ? ? ? //計(jì)算面積
? ? ? ? setArea();
}
? ? /**
* 獲取面積
? ? * @paramring
? ? * @return
*/
? private? BigDecimal? polygon_area(List<LatLng>? ring){
? ? ? ? double sJ= 6378137;
? ? ? ? double Hq= 0.017453292519943295;
? ? ? ? double c= sJ*Hq;
? ? ? ? double d= 0;
? ? ? ? if (3 > ring.size()) {
? ? ? ? ? ? return new BigDecimal( 0);
}
? ? ? ? for (int i= 0; i< ring.size() - 1; i++){
? ? ? ? ? ? LatLng h= ring.get(i);
? ? ? ? ? ? LatLng k= ring.get(i+ 1);
? ? ? ? ? ? double u= h.longitude * c* Math.cos(h.latitude * Hq);
? ? ? ? ? ? double hhh= h.latitude * c;
? ? ? ? ? ? double v= k.longitude * c* Math.cos(k.latitude *Hq);
? ? ? ? ? ? d= d+ (u* k.latitude * c- v* hhh);
}
? ? ? ? LatLng g1= ring.get(ring.size()-1);
? ? ? ? LatLng point= ring.get(0);
? ? ? ? double eee= g1.longitude * c* Math.cos(g1.latitude * Hq);
? ? ? ? double g2= g1.latitude * c;
? ? ? ? double k= point.longitude * c* Math.cos(point.latitude * Hq);
? ? ? ? d+= eee* point.latitude * c- k* g2;
? ? ? ? return new BigDecimal( 0.5*Math.abs(d)).divide(new BigDecimal(1000000));
}
? ? /**
* 繪制圖形的顏色樣式
*/
? ? private void createAreaStyle() {
? ? ? ? int fillColor= Color.parseColor("#11000000");
? ? ? ? // 設(shè)置多邊形的邊框顏色逾柿,32位 ARGB格式缀棍,默認(rèn)為黑色
? ? ? ? polygonOptions.strokeWidth(10);
? ? ? ? polygonOptions.strokeWidth(10); // 設(shè)置多邊形的填充顏色宅此,32位ARGB格式
? ? ? ? polygonOptions.fillColor(fillColor); // 注意要加前兩位的透明度 // 在地圖上添加一個(gè)多邊形(polygon)對(duì)象
? ? }
? ? /**
* 獲取所有點(diǎn)里離該點(diǎn)最近的點(diǎn)的索引值,閾值為1000爬范,如果所有值都比2大父腕,則表示沒有最近的點(diǎn)(返回-1)
*這個(gè)閾值可以繼續(xù)做優(yōu)化,根據(jù)地圖縮放等級(jí)動(dòng)態(tài)更改能獲得更好體驗(yàn)
? ? * @paramlatLng
*/
? ? @NonNull
? ? private int getNearestLatLng(LatLng latLng) {
? ? ? ? for (int i= 0; i< allLatLngs.size(); i++) {
? ? ? ? ? ? //判斷兩點(diǎn)間的直線距離
? ? ? ? ? ? float distance= AMapUtils.calculateLineDistance(latLng, allLatLngs.get(i).getLatLng());
? ? ? ? ? ? if (((int) distance) < 1000) {
? ? ? ? ? ? ? ? return i;
}
}
? ? ? ? return -1;
}
}
5.還有一個(gè)涉及到的bean類也一并貼出
public class MyLatLng {
? ? public static final int ABLE = 1;
? ? public static final int UNABLE = 0;
? ? private LatLng latLng;
? ? private int state;
? ? public MyLatLng(LatLng latLng, int state) {
? ? ? ? this.latLng = latLng;
? ? ? ? this.state = state;
}
? ? public LatLng getLatLng() {
? ? ? ? return latLng;
}
? ? public void setLatLng(LatLng latLng) {
? ? ? ? this.latLng = latLng;
}
? ? public int getState() {
? ? ? ? return state;
}
? ? public void setState(int state) {
? ? ? ? this.state = state;
}
}