riku
2021-02-25 e102578ebfc95c27aeb13dce13fb82af53a2bead
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package cn.flightfeather.thirdapp.task;
 
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
 
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.PolylineOptions;
import com.amap.api.services.district.DistrictItem;
 
/**
 * Created by linli on 2018/3/1.
 * 获取区域边界点位信息的runnable
 */
 
public class ObtainBoundaryRunnable implements Runnable {
    private DistrictItem districtItem;
    private Handler handler;
    private boolean isCancel = false;
 
    /**
     * districtBoundary()
     * 以字符串数组形式返回行政区划边界值。
     * 字符串拆分规则: 经纬度,经度和纬度之间用","分隔,坐标点之间用";"分隔。
     * 例如:116.076498,40.115153;116.076603,40.115071;116.076333,40.115257;116.076498,40.115153。
     * 字符串数组由来: 如果行政区包括的是群岛,则坐标点是各个岛屿的边界,各个岛屿之间的经纬度使用"|"分隔。
     * 一个字符串数组可包含多个封闭区域,一个字符串表示一个封闭区域
     */
    public ObtainBoundaryRunnable(DistrictItem districtItem, Handler handler) {
 
        this.districtItem = districtItem;
        this.handler = handler;
    }
 
//        public void cancel() {
//            isCancel = true;
//        }
 
    /**
     * Starts executing the active part of the class' code. This method is
     * called when a thread is started that has been created with a class which
     * implements {@code Runnable}.
     */
    @Override
    public void run() {
        if (!isCancel) {
            try {
                String[] boundary = districtItem.districtBoundary();
                if (boundary != null && boundary.length > 0) {
                    for (String b : boundary) {
                        if (!b.contains("|")) {
                            String[] split = b.split(";");
                            PolylineOptions polylineOptions = new PolylineOptions();
                            boolean isFirst = true;
                            LatLng firstLatLng = null;
                            for (String s : split) {
                                String[] ll = s.split(",");
                                if (isFirst) {
                                    isFirst = false;
                                    firstLatLng = new LatLng(Double.parseDouble(ll[1]), Double.parseDouble(ll[0]));
                                }
                                polylineOptions.add(new LatLng(Double.parseDouble(ll[1]), Double.parseDouble(ll[0])));
                            }
                            if (firstLatLng != null) {
                                polylineOptions.add(firstLatLng);
                            }
                            polylineOptions.width(10).color(Color.BLUE).setDottedLine(true);
                            Message message = handler.obtainMessage();
                            message.what = 18;
                            message.obj = polylineOptions;
                            handler.sendMessage(message);
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}