Android高德地圖多邊形的繪制與編輯

最近項目中用到了一個高德地圖畫地塊的功能,差了一下api,發(fā)現(xiàn)只有js有相關的方法,所以仿照js的交互方式做了一個android版的多邊形繪制與編輯。

先不說,上一張效果圖

完成效果圖

1.通過觀察,我們大致能知道主要的繪制與編輯方式,點完第二個點之后,和第一個點連成線段,這個線段的中間位置會有一個小點,下面是添加marker點的代碼

/**

* 添加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++) { // 在地圖上添一組圖片標記(marker)對象,并設置是否改變地圖狀態(tài)以至于所有的marker對象都在當前地圖可視區(qū)域范圍內顯示

? ? ? ? ? ? ? ? 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點之后還要畫點之間的線,還有多邊形的填充色這里貼一下相關的代碼

/**

* 畫線

*/

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);

? ? ? ? //計算周長

? ? ? ? setPerimeter();

}

}

/**

* 畫多邊形

*/

private void drawRect() {

? ? if (polygon == null) {

? ? ? ? polygonOptions.addAll(allLatLngsWithLine);

? ? ? ? //創(chuàng)建多邊形

? ? ? ? polygon = aMap.addPolygon(polygonOptions);

? ? } else {

? ? ? ? polygon.setPoints(allLatLngsWithLine);

}

? ? //計算面積

? ? setArea();

}

3.是時候該拖動marker點改變多邊形的形狀了.一開始我是準備用marker點的監(jiān)聽做它的移動監(jiān)聽的,發(fā)現(xiàn)marker的拖動必須要長按才能拖動,用戶體驗并不好,所以就改成了地圖的觸摸監(jiā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);

? ? ? ? ? ? ? ? //獲取觸摸的點下標

? ? ? ? ? ? ? ? checkPos = getNearestLatLng(downLatLng);

? ? ? ? ? ? ? ? //觸摸的點是矩形的點

? ? ? ? ? ? ? ? if (checkPos > -1) {

? ? ? ? ? ? ? ? ? ? //如果是小點

? ? ? ? ? ? ? ? ? ? if (allLatLngs.get(checkPos).getState() == MyLatLng.UNABLE) {

? ? ? ? ? ? ? ? ? ? ? ? //更改為大點

? ? ? ? ? ? ? ? ? ? ? ? chageMarker();

? ? ? ? ? ? ? ? ? ? ? ? //如果是封閉圖形并且點擊的是最后的一個點

? ? ? ? ? ? ? ? ? ? ? ? if (isEnd && checkPos == allLatLngs.size() - 1) {

? ? ? ? ? ? ? ? ? ? ? ? ? ? //添加兩個mark

? ? ? ? ? ? ? ? ? ? ? ? ? ? addTwoMarker(getCenterLatlng(allLatLngs.get(checkPos).getLatLng(), allLatLngs.get(checkPos - 1).getLatLng()), getCenterLatlng(allLatLngs.get(checkPos).getLatLng(), allLatLngs.get(0).getLatLng()));

? ? ? ? ? ? ? ? ? ? ? ? } else {

? ? ? ? ? ? ? ? ? ? ? ? ? ? //不是封閉圖形

//添加兩個marker

? ? ? ? ? ? ? ? ? ? ? ? ? ? addTwoMarker(getCenterLatlng(allLatLngs.get(checkPos).getLatLng(), allLatLngs.get(checkPos - 1).getLatLng()), getCenterLatlng(allLatLngs.get(checkPos).getLatLng(), allLatLngs.get(checkPos + 1).getLatLng()));

}

? ? ? ? ? ? ? ? ? ? ? ? //將選中點的下標更改

? ? ? ? ? ? ? ? ? ? ? ? checkPos += 1;

}

? ? ? ? ? ? ? ? ? ? //獲取選中的marker點

? ? ? ? ? ? ? ? ? ? touchMark = markers.get(checkPos);

? ? ? ? ? ? ? ? ? ? //禁止地圖放大旋轉等操作

? ? ? ? ? ? ? ? ? ? uiSettings.setScrollGesturesEnabled(false);

}

? ? ? ? ? ? ? ? break;

? ? ? ? ? ? ? ? //移動中

? ? ? ? ? ? case MotionEvent.ACTION_MOVE:

? ? ? ? ? ? ? ? //有選中的marker點

? ? ? ? ? ? ? ? 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);

? ? ? ? ? ? ? ? ? ? //獲取到觸摸點經(jīng)緯度

? ? ? ? ? ? ? ? ? ? LatLng moveLatLng= aMap.getProjection().fromScreenLocation(movePoint);

? ? ? ? ? ? ? ? ? ? //更新的坐標點位置

? ? ? ? ? ? ? ? ? ? touchMark.setPosition(moveLatLng);

? ? ? ? ? ? ? ? ? ? //如果已經(jīng)畫出線(兩個大點)

? ? ? ? ? ? ? ? ? ? if (polyline != null) {

? ? ? ? ? ? ? ? ? ? ? ? //會比markers多一個點

? ? ? ? ? ? ? ? ? ? ? ? List<LatLng> points= polyline.getPoints();

? ? ? ? ? ? ? ? ? ? ? ? //修改線數(shù)據(jù)中當前觸摸點的坐標信息

? ? ? ? ? ? ? ? ? ? ? ? points.set(checkPos, moveLatLng);

? ? ? ? ? ? ? ? ? ? ? ? //修改當前選中marker點坐標集合的信息

? ? ? ? ? ? ? ? ? ? ? ? allLatLngs.get(checkPos).setLatLng(moveLatLng);

? ? ? ? ? ? ? ? ? ? ? ? //修改當前選中線的點的坐標集合的信息

? ? ? ? ? ? ? ? ? ? ? ? allLatLngsWithLine.set(checkPos, moveLatLng);

? ? ? ? ? ? ? ? ? ? ? ? //不需要添加兩個點

? ? ? ? ? ? ? ? ? ? ? ? if (checkPos == 0) {

? ? ? ? ? ? ? ? ? ? ? ? ? ? //獲取選中大點旁邊的小點

? ? ? ? ? ? ? ? ? ? ? ? ? ? Marker marker= markers.get(checkPos + 1);

? ? ? ? ? ? ? ? ? ? ? ? ? ? //獲取第一個大點和第二個大點的中間坐標

? ? ? ? ? ? ? ? ? ? ? ? ? ? LatLng center= getCenterLatlng(moveLatLng, allLatLngs.get(checkPos + 2).getLatLng());

? ? ? ? ? ? ? ? ? ? ? ? ? ? //修改marker的坐標

? ? ? ? ? ? ? ? ? ? ? ? ? ? marker.setPosition(center);

? ? ? ? ? ? ? ? ? ? ? ? ? ? //修改線的坐標

? ? ? ? ? ? ? ? ? ? ? ? ? ? points.set(checkPos + 1, center);

? ? ? ? ? ? ? ? ? ? ? ? ? ? //修改總marker坐標集合信息

? ? ? ? ? ? ? ? ? ? ? ? ? ? allLatLngs.get(checkPos + 1).setLatLng(center);

? ? ? ? ? ? ? ? ? ? ? ? ? ? //修改總線集合信息

? ? ? ? ? ? ? ? ? ? ? ? ? ? allLatLngsWithLine.set(checkPos + 1, center);

? ? ? ? ? ? ? ? ? ? ? ? ? ? //如果是已經(jīng)封閉的則需要修改最后一個大點與第一個大點中間點的坐標

? ? ? ? ? ? ? ? ? ? ? ? ? ? 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);

}

? ? ? ? ? ? ? ? ? ? ? ? ? ? //當觸摸的點是最后一個大點或者最后一個小點的時候

? ? ? ? ? ? ? ? ? ? ? ? } else if (checkPos == markers.size() - 2 || checkPos == markers.size() - 1) {

? ? ? ? ? ? ? ? ? ? ? ? ? ? //原理同上

//最后一個點

? ? ? ? ? ? ? ? ? ? ? ? ? ? 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 {

? ? ? ? ? ? ? ? ? ? ? ? ? ? //原理同上

//中間的點

? ? ? ? ? ? ? ? ? ? ? ? ? ? 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);

? ? ? ? ? ? ? ? ? ? ? ? ? ? //移動線

? ? ? ? ? ? ? ? ? ? ? ? ? ? points.set(checkPos + 1, center);

? ? ? ? ? ? ? ? ? ? ? ? ? ? points.set(checkPos - 1, center2);

}

? ? ? ? ? ? ? ? ? ? ? ? //更改線數(shù)據(jù)

? ? ? ? ? ? ? ? ? ? ? ? polyline.setPoints(points);

? ? ? ? ? ? ? ? ? ? ? ? //計算周長

? ? ? ? ? ? ? ? ? ? ? ? setPerimeter();

? ? ? ? ? ? ? ? ? ? ? ? //如果封閉的話

? ? ? ? ? ? ? ? ? ? ? ? if (isEnd) {

? ? ? ? ? ? ? ? ? ? ? ? ? ? //計算面積

? ? ? ? ? ? ? ? ? ? ? ? ? ? drawRect();

}

}

}

? ? ? ? ? ? ? ? break;

? ? ? ? ? ? ? ? //抬起

? ? ? ? ? ? case MotionEvent.ACTION_UP:

? ? ? ? ? ? ? ? if (touchMark != null) {

? ? ? ? ? ? ? ? ? ? //清除選中點信息

? ? ? ? ? ? ? ? ? ? touchMark = null;

? ? ? ? ? ? ? ? ? ? //恢復地圖操作

? ? ? ? ? ? ? ? ? ? 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顯示一個基本地圖

*/

public class TextureMapViewActivity extends Activity {

? ? private TextureMapView mapView;

? ? private AMap aMap;

? ? //存放所有marker點標記

? ? private List<MyLatLng> allLatLngs = new ArrayList<>();

? ? //所有線段的點

? ? private List<LatLng> allLatLngsWithLine = new ArrayList<>();

? ? //所有marker點

? ? private List<Marker> markers = new ArrayList<>();

? ? //線的對象

? ? private Polyline polyline;

? ? //多邊形的對象

? ? private Polygon polygon;

? ? //是否為封閉圖形

? ? private boolean isEnd = false;

? ? //矩形對象的配置

? ? private PolygonOptions polygonOptions = new PolygonOptions();

? ? //當前觸摸的marker

? ? private Marker touchMark;

? ? //當前選中的點下標,相對于allLatLngs的

? ? private int checkPos;

? ? //地圖設置

? ? UiSettings uiSettings;

? ? ///周長

? ? 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);

? ? ? ? /*

* 設置離線地圖存儲目錄,在下載離線地圖或初始化地圖設置;

* 使用過程中可自行設置, 若自行設置了離線地圖存儲的路徑,

* 則需要在離線地圖下載和使用地圖頁面都進行路徑設置

* */

? ? ? ? //Demo中為了其他界面可以使用下載的離線地圖,使用默認位置存儲,屏蔽了自定義設置

//? MapsInitializer.sdcardDir =OffLineMapUtils.getSdCacheDir(this);

? ? ? ? mapView = (TextureMapView) findViewById(R.id.map);

? ? ? ? mapView.onCreate(savedInstanceState);// 此方法必須重寫

? ? ? ? init();

}

? ? /**

* 初始化AMap對象

*/

? ? private void init() {

? ? ? ? if (aMap == null) {

? ? ? ? ? ? aMap = mapView.getMap();

}

? ? ? ? //獲取地圖ui設置對象

? ? ? ? uiSettings = aMap.getUiSettings();

? ? ? ? //地圖點擊監(jiān)聽

? ? ? ? aMap.setOnMapClickListener(new AMap.OnMapClickListener() {

? ? ? ? ? ? @Override

? ? ? ? ? ? public void onMapClick(LatLng latLng) {

? ? ? ? ? ? ? ? if (!isEnd) {

? ? ? ? ? ? ? ? ? ? addMarker(latLng);

}

}

? ? ? ? });

? ? ? ? //marker點擊監(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 {

? ? ? ? ? ? ? ? ? ? ? ? //未封閉,點擊其它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);

? ? ? ? ? ? ? ? ? ? ? ? //獲取觸摸的點下標

? ? ? ? ? ? ? ? ? ? ? ? checkPos = getNearestLatLng(downLatLng);

? ? ? ? ? ? ? ? ? ? ? ? //觸摸的點是矩形的點

? ? ? ? ? ? ? ? ? ? ? ? if (checkPos > -1) {

? ? ? ? ? ? ? ? ? ? ? ? ? ? //如果是小點

? ? ? ? ? ? ? ? ? ? ? ? ? ? if (allLatLngs.get(checkPos).getState() == MyLatLng.UNABLE) {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //更改為大點

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? chageMarker();

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //如果是封閉圖形并且點擊的是最后的一個點

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (isEnd && checkPos == allLatLngs.size() - 1) {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //添加兩個mark

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? addTwoMarker(getCenterLatlng(allLatLngs.get(checkPos).getLatLng(), allLatLngs.get(checkPos - 1).getLatLng()), getCenterLatlng(allLatLngs.get(checkPos).getLatLng(), allLatLngs.get(0).getLatLng()));

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } else {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //不是封閉圖形

//添加兩個marker

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? addTwoMarker(getCenterLatlng(allLatLngs.get(checkPos).getLatLng(), allLatLngs.get(checkPos - 1).getLatLng()), getCenterLatlng(allLatLngs.get(checkPos).getLatLng(), allLatLngs.get(checkPos + 1).getLatLng()));

}

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //將選中點的下標更改

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? checkPos += 1;

}

? ? ? ? ? ? ? ? ? ? ? ? ? ? //獲取選中的marker點

? ? ? ? ? ? ? ? ? ? ? ? ? ? touchMark = markers.get(checkPos);

? ? ? ? ? ? ? ? ? ? ? ? ? ? //禁止地圖放大旋轉等操作

? ? ? ? ? ? ? ? ? ? ? ? ? ? uiSettings.setScrollGesturesEnabled(false);

}

? ? ? ? ? ? ? ? ? ? ? ? break;

? ? ? ? ? ? ? ? ? ? ? ? //移動中

? ? ? ? ? ? ? ? ? ? case MotionEvent.ACTION_MOVE:

? ? ? ? ? ? ? ? ? ? ? ? //有選中的marker點

? ? ? ? ? ? ? ? ? ? ? ? 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);

? ? ? ? ? ? ? ? ? ? ? ? ? ? //獲取到觸摸點經(jīng)緯度

? ? ? ? ? ? ? ? ? ? ? ? ? ? LatLng moveLatLng= aMap.getProjection().fromScreenLocation(movePoint);

? ? ? ? ? ? ? ? ? ? ? ? ? ? //更新的坐標點位置

? ? ? ? ? ? ? ? ? ? ? ? ? ? touchMark.setPosition(moveLatLng);

? ? ? ? ? ? ? ? ? ? ? ? ? ? //如果已經(jīng)畫出線(兩個大點)

? ? ? ? ? ? ? ? ? ? ? ? ? ? if (polyline != null) {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //會比markers多一個點

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? List<LatLng> points= polyline.getPoints();

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //修改線數(shù)據(jù)中當前觸摸點的坐標信息

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? points.set(checkPos, moveLatLng);

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //修改當前選中marker點坐標集合的信息

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? allLatLngs.get(checkPos).setLatLng(moveLatLng);

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //修改當前選中線的點的坐標集合的信息

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? allLatLngsWithLine.set(checkPos, moveLatLng);

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //不需要添加兩個點

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (checkPos == 0) {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //獲取選中大點旁邊的小點

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Marker marker= markers.get(checkPos + 1);

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //獲取第一個大點和第二個大點的中間坐標

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? LatLng center= getCenterLatlng(moveLatLng, allLatLngs.get(checkPos + 2).getLatLng());

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //修改marker的坐標

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? marker.setPosition(center);

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //修改線的坐標

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? points.set(checkPos + 1, center);

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //修改總marker坐標集合信息

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? allLatLngs.get(checkPos + 1).setLatLng(center);

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //修改總線集合信息

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? allLatLngsWithLine.set(checkPos + 1, center);

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //如果是已經(jīng)封閉的則需要修改最后一個大點與第一個大點中間點的坐標

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 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);

}

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //當觸摸的點是最后一個大點或者最后一個小點的時候

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } else if (checkPos == markers.size() - 2 || checkPos == markers.size() - 1) {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //原理同上

//最后一個點

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 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 {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //原理同上

//中間的點

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 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);

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //移動線

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? points.set(checkPos + 1, center);

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? points.set(checkPos - 1, center2);

}

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //更改線數(shù)據(jù)

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? polyline.setPoints(points);

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //計算周長

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? setPerimeter();

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //如果封閉的話

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (isEnd) {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //計算面積

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? drawRect();

}

}

}

? ? ? ? ? ? ? ? ? ? ? ? break;

? ? ? ? ? ? ? ? ? ? ? ? //抬起

? ? ? ? ? ? ? ? ? ? case MotionEvent.ACTION_UP:

? ? ? ? ? ? ? ? ? ? ? ? if (touchMark != null) {

? ? ? ? ? ? ? ? ? ? ? ? ? ? //清除選中點信息

? ? ? ? ? ? ? ? ? ? ? ? ? ? touchMark = null;

? ? ? ? ? ? ? ? ? ? ? ? ? ? //恢復地圖操作

? ? ? ? ? ? ? ? ? ? ? ? ? ? uiSettings.setScrollGesturesEnabled(true);

}

? ? ? ? ? ? ? ? ? ? ? ? break;

}

}

? ? ? ? });

}

? ? /**

* 獲取兩個點的中點坐標

*

? ? * @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);

? ? ? ? //修改點類型為大點

? ? ? ? allLatLngs.get(checkPos).setState(MyLatLng.ABLE);

? ? ? ? marker.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.icon_dian_s));

}

? ? /**

* 在拖拽點兩側添加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));

? ? ? ? //設置偏移

? ? ? ? marker.setAnchor(0.5f, 0.5f);

? ? ? ? //如果是最后一個點

? ? ? ? if (isEnd && checkPos == allLatLngs.size() - 1) {

? ? ? ? ? ? //直接把點信息添加進去

? ? ? ? ? ? allLatLngs.add(new MyLatLng(latLng2, MyLatLng.UNABLE));

? ? ? ? ? ? allLatLngsWithLine.add(latLng2);

? ? ? ? ? ? markers.add(marker);

? ? ? ? } else {

? ? ? ? ? ? //按位置插入點信息

? ? ? ? ? ? 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++) { // 在地圖上添一組圖片標記(marker)對象,并設置是否改變地圖狀態(tài)以至于所有的marker對象都在當前地圖可視區(qū)域范圍內顯示

? ? ? ? ? ? ? ? ? ? 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));

}

}

}

? ? /**

* 計算周長

*/

? ? 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("周長:" + f+"米");

}

? ? /**

* 計算面積

*/

? ? 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);

? ? ? ? ? ? //計算周長

? ? ? ? ? ? setPerimeter();

}

}

? ? /**

* 畫多邊形

*/

? ? private void drawRect() {

? ? ? ? if (polygon == null) {

? ? ? ? ? ? polygonOptions.addAll(allLatLngsWithLine);

? ? ? ? ? ? //創(chuàng)建多邊形

? ? ? ? ? ? polygon = aMap.addPolygon(polygonOptions);

? ? ? ? } else {

? ? ? ? ? ? polygon.setPoints(allLatLngsWithLine);

}

? ? ? ? //計算面積

? ? ? ? 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");

? ? ? ? // 設置多邊形的邊框顏色,32位 ARGB格式,默認為黑色

? ? ? ? polygonOptions.strokeWidth(10);

? ? ? ? polygonOptions.strokeWidth(10); // 設置多邊形的填充顏色,32位ARGB格式

? ? ? ? polygonOptions.fillColor(fillColor); // 注意要加前兩位的透明度 // 在地圖上添加一個多邊形(polygon)對象

? ? }

? ? /**

* 獲取所有點里離該點最近的點的索引值,閾值為1000,如果所有值都比2大,則表示沒有最近的點(返回-1)

*這個閾值可以繼續(xù)做優(yōu)化,根據(jù)地圖縮放等級動態(tài)更改能獲得更好體驗

? ? * @paramlatLng

*/

? ? @NonNull

? ? private int getNearestLatLng(LatLng latLng) {

? ? ? ? for (int i= 0; i< allLatLngs.size(); i++) {

? ? ? ? ? ? //判斷兩點間的直線距離

? ? ? ? ? ? float distance= AMapUtils.calculateLineDistance(latLng, allLatLngs.get(i).getLatLng());

? ? ? ? ? ? if (((int) distance) < 1000) {

? ? ? ? ? ? ? ? return i;

}

}

? ? ? ? return -1;

}

}

5.還有一個涉及到的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;

}

}

最后,這篇文章也基本結束,時間緊急,沒有完善其中的一些判定方法,如果各位有更好的方案,歡迎留言!!!

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容