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