Commit e6cb3e4f8b88ff49a9c506668ce7044994313929
1 parent
0bad6b91
1.赛事详情
Showing
8 changed files
with
575 additions
and
27 deletions
moudle_pedometer/src/main/java/com/cnlive/moudle/pedometer/frame/presenter/RunningMapFragmentPresenter.java
| ... | ... | @@ -3,6 +3,7 @@ package com.cnlive.moudle.pedometer.frame.presenter; |
| 3 | 3 | import android.content.Context; |
| 4 | 4 | import android.graphics.Bitmap; |
| 5 | 5 | import android.graphics.BitmapFactory; |
| 6 | +import android.os.Handler; | |
| 6 | 7 | import android.util.Log; |
| 7 | 8 | import android.widget.Toast; |
| 8 | 9 | |
| ... | ... | @@ -26,6 +27,24 @@ import com.baidu.trace.api.entity.PolygonSearchResponse; |
| 26 | 27 | import com.baidu.trace.api.entity.SearchResponse; |
| 27 | 28 | import com.baidu.trace.api.entity.SortBy; |
| 28 | 29 | import com.baidu.trace.api.entity.UpdateEntityResponse; |
| 30 | +import com.baidu.trace.api.fence.AddMonitoredPersonResponse; | |
| 31 | +import com.baidu.trace.api.fence.CreateFenceRequest; | |
| 32 | +import com.baidu.trace.api.fence.CreateFenceResponse; | |
| 33 | +import com.baidu.trace.api.fence.DeleteFenceRequest; | |
| 34 | +import com.baidu.trace.api.fence.DeleteFenceResponse; | |
| 35 | +import com.baidu.trace.api.fence.DeleteMonitoredPersonResponse; | |
| 36 | +import com.baidu.trace.api.fence.FenceInfo; | |
| 37 | +import com.baidu.trace.api.fence.FenceListRequest; | |
| 38 | +import com.baidu.trace.api.fence.FenceListResponse; | |
| 39 | +import com.baidu.trace.api.fence.HistoryAlarmResponse; | |
| 40 | +import com.baidu.trace.api.fence.ListMonitoredPersonResponse; | |
| 41 | +import com.baidu.trace.api.fence.MonitoredStatus; | |
| 42 | +import com.baidu.trace.api.fence.MonitoredStatusByLocationResponse; | |
| 43 | +import com.baidu.trace.api.fence.MonitoredStatusInfo; | |
| 44 | +import com.baidu.trace.api.fence.MonitoredStatusRequest; | |
| 45 | +import com.baidu.trace.api.fence.MonitoredStatusResponse; | |
| 46 | +import com.baidu.trace.api.fence.OnFenceListener; | |
| 47 | +import com.baidu.trace.api.fence.UpdateFenceResponse; | |
| 29 | 48 | import com.baidu.trace.api.track.HistoryTrackRequest; |
| 30 | 49 | import com.baidu.trace.api.track.HistoryTrackResponse; |
| 31 | 50 | import com.baidu.trace.api.track.LatestPointResponse; |
| ... | ... | @@ -43,6 +62,7 @@ import com.cnlive.moudle.pedometer.model.Constant; |
| 43 | 62 | import com.cnlive.moudle.pedometer.service.StepService; |
| 44 | 63 | import com.cnlive.moudle.pedometer.utils.BitmapUtil; |
| 45 | 64 | import com.cnlive.moudle.pedometer.utils.CommonUtil; |
| 65 | +import com.cnlive.moudle.pedometer.utils.GsonUtil; | |
| 46 | 66 | import com.cnlive.moudle.pedometer.utils.MapUtil; |
| 47 | 67 | import com.cnlive.moudle.pedometer.utils.MyMapUtil; |
| 48 | 68 | import com.example.moudle_pedometer.R; |
| ... | ... | @@ -76,6 +96,7 @@ public class RunningMapFragmentPresenter extends MvpBasePresenter<RunningMapFrag |
| 76 | 96 | private List<LatLng> trackPoints; |
| 77 | 97 | private List<LatLng> realTimePoints; |
| 78 | 98 | |
| 99 | + | |
| 79 | 100 | /** |
| 80 | 101 | * 查询历史轨迹 |
| 81 | 102 | * |
| ... | ... | @@ -106,7 +127,7 @@ public class RunningMapFragmentPresenter extends MvpBasePresenter<RunningMapFrag |
| 106 | 127 | //去噪:可设置是否需要去除角度、速度和方向异常的轨迹点 |
| 107 | 128 | processOption.setNeedDenoise(true); |
| 108 | 129 | //绑路:设置是否将轨迹点绑定至路网道路 |
| 109 | - processOption.setNeedMapMatch(false); | |
| 130 | + processOption.setNeedMapMatch(true); | |
| 110 | 131 | //抽稀:设置是否进行抽稀处理,对冗余的轨迹点进行抽稀去除处理,如一条直线上除起终点外的多个轨迹点 |
| 111 | 132 | // processOption.setNeedVacuate(true); |
| 112 | 133 | //自动判断交通方式 |
| ... | ... | @@ -152,6 +173,13 @@ public class RunningMapFragmentPresenter extends MvpBasePresenter<RunningMapFrag |
| 152 | 173 | queryHistoryTrace(context, entityName, startTime, endTime); |
| 153 | 174 | } else { |
| 154 | 175 | if (getView() != null) { |
| 176 | + List<com.baidu.trace.model.LatLng> fenceLatLngs = new ArrayList<>(); | |
| 177 | +// for (LatLng latLng:trackPoints){ | |
| 178 | +// com.baidu.trace.model.LatLng tempLatLng=new com.baidu.trace.model.LatLng(latLng.latitude,latLng.longitude); | |
| 179 | +// fenceLatLngs.add(tempLatLng); | |
| 180 | +// } | |
| 181 | +// Log.e(TAG, "onHistoryTrackCallback: "+trackPoints.size()+","+ GsonUtil.GsonString(trackPoints)); | |
| 182 | +// createFence(context, fenceLatLngs); | |
| 155 | 183 | MyMapUtil.drawHistoryTrack(context, getView().baiduMap, trackPoints, SortType.asc, R.color.red, R.drawable.icon_start, R.drawable.icon_end); |
| 156 | 184 | } |
| 157 | 185 | } |
| ... | ... | @@ -161,6 +189,7 @@ public class RunningMapFragmentPresenter extends MvpBasePresenter<RunningMapFrag |
| 161 | 189 | public void onLatestPointCallback(LatestPointResponse latestPointResponse) { |
| 162 | 190 | super.onLatestPointCallback(latestPointResponse); |
| 163 | 191 | Log.e(TAG, "onLatestPointCallback: "); |
| 192 | + Toast.makeText(context, "onLatestPointCallback", Toast.LENGTH_SHORT).show(); | |
| 164 | 193 | |
| 165 | 194 | // if (StatusCodes.SUCCESS != latestPointResponse.getStatus()) { |
| 166 | 195 | // } else { |
| ... | ... | @@ -256,6 +285,7 @@ public class RunningMapFragmentPresenter extends MvpBasePresenter<RunningMapFrag |
| 256 | 285 | if (traceLocation != null) { |
| 257 | 286 | realTimePoints.add(new LatLng(traceLocation.getLatitude(), traceLocation.getLongitude())); |
| 258 | 287 | } |
| 288 | + Log.e(TAG, "onReceiveLocation: size:" + realTimePoints.size()); | |
| 259 | 289 | if (realTimePoints.size() > 2) { |
| 260 | 290 | if (getView() != null) { |
| 261 | 291 | if (realTimeOverlay == null) { |
| ... | ... | @@ -275,4 +305,152 @@ public class RunningMapFragmentPresenter extends MvpBasePresenter<RunningMapFrag |
| 275 | 305 | } |
| 276 | 306 | |
| 277 | 307 | } |
| 308 | + | |
| 309 | + //创建地理围栏 | |
| 310 | + public void createFence(Context context, List<com.baidu.trace.model.LatLng> latLngs) { | |
| 311 | + CreateFenceRequest createFenceRequest = null; | |
| 312 | + int offset = 1;// 偏离距离 | |
| 313 | + int denoise = 5; //围栏去噪精度 | |
| 314 | + // 初始化围栏监听器 | |
| 315 | + OnFenceListener onFenceListener = new OnFenceListener() { | |
| 316 | + // 创建围栏回调 | |
| 317 | + @Override | |
| 318 | + public void onCreateFenceCallback(CreateFenceResponse createFenceResponse) { | |
| 319 | + if ("成功".equals(createFenceResponse.getMessage())) { | |
| 320 | + Toast.makeText(context, "创建围栏回调成功", Toast.LENGTH_SHORT).show(); | |
| 321 | + Log.e(TAG, "onCreateFenceCallback: " + createFenceResponse.getFenceId()); | |
| 322 | + // 查询围栏监控者状态 | |
| 323 | + List<Long> list = new ArrayList<>(); | |
| 324 | + list.add(createFenceResponse.getFenceId()); | |
| 325 | + // 创建查询服务端围栏监控状态请求 | |
| 326 | + | |
| 327 | + } | |
| 328 | + } | |
| 329 | + | |
| 330 | + // 更新围栏回调 | |
| 331 | + @Override | |
| 332 | + public void onUpdateFenceCallback(UpdateFenceResponse updateFenceResponse) { | |
| 333 | + | |
| 334 | + } | |
| 335 | + | |
| 336 | + // 删除围栏回调 | |
| 337 | + @Override | |
| 338 | + public void onDeleteFenceCallback(DeleteFenceResponse deleteFenceResponse) { | |
| 339 | + | |
| 340 | + } | |
| 341 | + | |
| 342 | + // 围栏列表回调 | |
| 343 | + @Override | |
| 344 | + public void onFenceListCallback(FenceListResponse fenceListResponse) { | |
| 345 | + if ("成功".equals(fenceListResponse.getMessage())) { | |
| 346 | + Toast.makeText(context, "围栏列表回调成功:" + fenceListResponse.getSize(), Toast.LENGTH_SHORT).show(); | |
| 347 | + } | |
| 348 | + | |
| 349 | + | |
| 350 | +// //获取围栏列表响应结果 | |
| 351 | +// DeleteFenceRequest deleteFenceRequest = DeleteFenceRequest.buildServerRequest(1, StepService.serviceId, "yangshuai", null); | |
| 352 | +// StepService.mClient.deleteFence(deleteFenceRequest,this); | |
| 353 | + } | |
| 354 | + | |
| 355 | + // 监控状态回调 | |
| 356 | + @Override | |
| 357 | + public void onMonitoredStatusCallback(MonitoredStatusResponse monitoredStatusResponse) { | |
| 358 | + Toast.makeText(context, "围栏列表回调成功:onMonitoredStatusCallback", Toast.LENGTH_SHORT).show(); | |
| 359 | + Log.e(TAG, "onMonitoredStatusCallback: " + monitoredStatusResponse.getMonitoredStatusInfos().get(0).getMonitoredStatus()); | |
| 360 | + //查询监控对象状态响应结果 | |
| 361 | + List<MonitoredStatusInfo> monitoredStatusInfos = monitoredStatusResponse.getMonitoredStatusInfos(); | |
| 362 | + for (MonitoredStatusInfo monitoredStatusInfo : monitoredStatusInfos) { | |
| 363 | + monitoredStatusInfo.getFenceId(); | |
| 364 | + | |
| 365 | + MonitoredStatus status = monitoredStatusInfo.getMonitoredStatus();//获取状态 | |
| 366 | + switch (status) { | |
| 367 | + case in: | |
| 368 | + //监控的设备在围栏内 | |
| 369 | + Toast.makeText(context, "监控的设备在围栏内", Toast.LENGTH_SHORT).show(); | |
| 370 | + Log.e(TAG, "监控的设备在围栏内: "); | |
| 371 | + break; | |
| 372 | + case out: | |
| 373 | + //监控的设备在围栏外 | |
| 374 | + Toast.makeText(context, "监控的设备在围栏外", Toast.LENGTH_SHORT).show(); | |
| 375 | + Log.e(TAG, "监控的设备在围栏外: "); | |
| 376 | + | |
| 377 | + break; | |
| 378 | + case unknown: | |
| 379 | + //监控的设备状态未知 | |
| 380 | + Toast.makeText(context, "监控的设备状态未知", Toast.LENGTH_SHORT).show(); | |
| 381 | + Log.e(TAG, "监控的设备状态未知: "); | |
| 382 | + | |
| 383 | + break; | |
| 384 | + } | |
| 385 | + } | |
| 386 | + } | |
| 387 | + | |
| 388 | + // 指定位置监控状态回调 | |
| 389 | + @Override | |
| 390 | + public void onMonitoredStatusByLocationCallback(MonitoredStatusByLocationResponse monitoredStatusByLocationResponse) { | |
| 391 | + Log.e(TAG, "onMonitoredStatusByLocationCallback: "); | |
| 392 | + } | |
| 393 | + | |
| 394 | + // 历史报警回调 | |
| 395 | + @Override | |
| 396 | + public void onHistoryAlarmCallback(HistoryAlarmResponse historyAlarmResponse) { | |
| 397 | + Log.e(TAG, "onHistoryAlarmCallback: "); | |
| 398 | + } | |
| 399 | + | |
| 400 | + @Override | |
| 401 | + public void onAddMonitoredPersonCallback(AddMonitoredPersonResponse addMonitoredPersonResponse) { | |
| 402 | + Log.e(TAG, "onAddMonitoredPersonCallback: "); | |
| 403 | + } | |
| 404 | + | |
| 405 | + @Override | |
| 406 | + public void onDeleteMonitoredPersonCallback(DeleteMonitoredPersonResponse deleteMonitoredPersonResponse) { | |
| 407 | + Log.e(TAG, "onDeleteMonitoredPersonCallback: "); | |
| 408 | + } | |
| 409 | + | |
| 410 | + @Override | |
| 411 | + public void onListMonitoredPersonCallback(ListMonitoredPersonResponse listMonitoredPersonResponse) { | |
| 412 | + Log.e(TAG, "onListMonitoredPersonCallback: "); | |
| 413 | + } | |
| 414 | + }; | |
| 415 | + // 创建本地圆形围栏 | |
| 416 | +// if (StepService.mClient != null) { | |
| 417 | +// if (createFenceRequest == null) { | |
| 418 | +// createFenceRequest = CreateFenceRequest.buildServerPolylineRequest(1, StepService.serviceId, "test", "yangshuai", latLngs, offset, denoise, CoordType.gcj02); | |
| 419 | +// StepService.mClient.createFence(createFenceRequest, onFenceListener); | |
| 420 | +// } | |
| 421 | +// } | |
| 422 | + | |
| 423 | + FenceListRequest request = FenceListRequest.buildServerRequest(1, StepService.serviceId, "yangshuai", null, CoordType.gcj02); | |
| 424 | + | |
| 425 | + //发起查询围栏请求 | |
| 426 | + if (StepService.mClient != null) { | |
| 427 | + StepService.mClient.queryFenceList(request, onFenceListener); | |
| 428 | + } | |
| 429 | + | |
| 430 | + List<Long> fenid = new ArrayList<>(); | |
| 431 | + fenid.add(107L); | |
| 432 | + MonitoredStatusRequest request1 = MonitoredStatusRequest.buildLocalRequest(1, | |
| 433 | + StepService.serviceId, "yangshuai", fenid); | |
| 434 | + Handler handler = new Handler(); | |
| 435 | + Runnable runnable = new Runnable() { | |
| 436 | + @Override | |
| 437 | + public void run() { | |
| 438 | + StepService.mClient.queryMonitoredStatus(request1, onFenceListener); | |
| 439 | +// handler.postDelayed(this, 5000); | |
| 440 | + } | |
| 441 | + }; | |
| 442 | + handler.postDelayed(runnable, 5000); | |
| 443 | + } | |
| 444 | + | |
| 445 | + public void query() { | |
| 446 | + /** | |
| 447 | + * 以查询服务端围栏为例 | |
| 448 | + * fenceIds:服务端围栏编号列表,List<Long>,如传入null,表示查询所有围栏 | |
| 449 | + */ | |
| 450 | +// FenceListRequest request = FenceListRequest.buildServerRequest(1, StepService.serviceId, "yangshuai", null, CoordType.gcj02); | |
| 451 | +// //发起查询围栏请求 | |
| 452 | +// if (StepService.mClient != null) { | |
| 453 | +// StepService.mClient.queryFenceList(request, fenceListener); | |
| 454 | +// } | |
| 455 | + } | |
| 278 | 456 | } | ... | ... |
moudle_pedometer/src/main/java/com/cnlive/moudle/pedometer/frame/view/RunMainFragmentView.java
| ... | ... | @@ -60,7 +60,7 @@ public class RunMainFragmentView implements MvpView { |
| 60 | 60 | * 初始化相关视图 |
| 61 | 61 | */ |
| 62 | 62 | public void initView() { |
| 63 | - uid = "yangshuai"; | |
| 63 | + uid = "yangshuai11"; | |
| 64 | 64 | fragment.binding.llRunMainRoot.setVisibility(View.GONE); |
| 65 | 65 | ButtonAnimUtil.startAnimation(fragment.binding.llStartTimeTip, false); |
| 66 | 66 | firstTimeFlag = true; | ... | ... |
moudle_pedometer/src/main/java/com/cnlive/moudle/pedometer/frame/view/RunRaceDetailFragmentView.java
| ... | ... | @@ -48,6 +48,8 @@ import com.cnlive.moudle.pedometer.ui.activity.RunningMapActivity; |
| 48 | 48 | import com.cnlive.moudle.pedometer.ui.fragment.RunMainFragment; |
| 49 | 49 | import com.cnlive.moudle.pedometer.ui.fragment.RunRaceDetailFragment; |
| 50 | 50 | import com.cnlive.moudle.pedometer.utils.BitmapUtil; |
| 51 | +import com.cnlive.moudle.pedometer.utils.GsonUtil; | |
| 52 | +import com.cnlive.moudle.pedometer.utils.MyMapUtil; | |
| 51 | 53 | import com.cnlive.moudle.pedometer.utils.ScreenUtil; |
| 52 | 54 | import com.example.moudle_pedometer.R; |
| 53 | 55 | import com.hannesdorfmann.mosby3.mvp.MvpView; |
| ... | ... | @@ -92,7 +94,7 @@ public class RunRaceDetailFragmentView implements MvpView, SensorEventListener { |
| 92 | 94 | |
| 93 | 95 | private String startPoint = "首都师范大学南门"; |
| 94 | 96 | private String endPoint = "首都师范大学篮球场"; |
| 95 | - | |
| 97 | + private String test = "[{\"latitude\":39.933889208848,\"latitudeE6\":3.9933889208848E7,\"longitude\":116.30900265517,\"longitudeE6\":1.1630900265517001E8},{\"latitude\":39.933868507347,\"latitudeE6\":3.9933868507347E7,\"longitude\":116.30865416508,\"longitudeE6\":1.1630865416508E8},{\"latitude\":39.934756505077,\"latitudeE6\":3.9934756505077E7,\"longitude\":116.30860516469,\"longitudeE6\":1.1630860516468999E8},{\"latitude\":39.934781620502,\"latitudeE6\":3.9934781620502E7,\"longitude\":116.30961013007,\"longitudeE6\":1.1630961013007E8},{\"latitude\":39.934881620374,\"latitudeE6\":3.9934881620374E7,\"longitude\":116.30960513008,\"longitudeE6\":1.1630960513008E8},{\"latitude\":39.934077623132,\"latitudeE6\":3.9934077623132E7,\"longitude\":116.30965812942,\"longitudeE6\":1.1630965812942E8},{\"latitude\":39.933972618463,\"latitudeE6\":3.9933972618463E7,\"longitude\":116.30962113087,\"longitudeE6\":1.1630962113087E8},{\"latitude\":39.933932608856,\"latitudeE6\":3.9933932608856E7,\"longitude\":116.30953913383,\"longitudeE6\":1.1630953913383E8},{\"latitude\":39.933903844942,\"latitudeE6\":3.9933903844942E7,\"longitude\":116.30918357649,\"longitudeE6\":1.1630918357649E8}]"; | |
| 96 | 98 | private LinkedList<LatLng> importLatLngs;//关键点 |
| 97 | 99 | private LatLng startLatLng = new LatLng(39.934733, 116.308646);//起始点 |
| 98 | 100 | |
| ... | ... | @@ -109,6 +111,8 @@ public class RunRaceDetailFragmentView implements MvpView, SensorEventListener { |
| 109 | 111 | return; |
| 110 | 112 | } |
| 111 | 113 | initMap(); |
| 114 | + List<LatLng> trackPoints = GsonUtil.jsonToList(test, LatLng.class); | |
| 115 | + Log.e(TAG, "initView: " + trackPoints.size()); | |
| 112 | 116 | //初始化运营人员所选的关键点 |
| 113 | 117 | importLatLngs = new LinkedList<>(); |
| 114 | 118 | // importLatLngs.add(new LatLng(39.935861,116.308621)); |
| ... | ... | @@ -124,6 +128,7 @@ public class RunRaceDetailFragmentView implements MvpView, SensorEventListener { |
| 124 | 128 | importLatLngs.add(new LatLng(39.934733, 116.309515)); |
| 125 | 129 | importLatLngs.add(new LatLng(39.934733, 116.308742)); |
| 126 | 130 | |
| 131 | + MyMapUtil.drawMapLine(getContext(), baiduMap, trackPoints, R.color.red); | |
| 127 | 132 | |
| 128 | 133 | // searchRouteResult(new LatLng(39.934266, 116.309228), new LatLng(39.939756, 116.319766), 0); |
| 129 | 134 | |
| ... | ... | @@ -351,11 +356,11 @@ public class RunRaceDetailFragmentView implements MvpView, SensorEventListener { |
| 351 | 356 | //显示当前位置图标 |
| 352 | 357 | Bitmap pointBitmap = BitmapUtil.getBitmapFromDp(getContext(), BitmapFactory.decodeResource(getContext().getResources(), R.drawable.icon_end), 30, 30); |
| 353 | 358 | endMark = new MarkerOptions() |
| 354 | - .position(points.get(points.size()-1)) | |
| 359 | + .position(points.get(points.size() - 1)) | |
| 355 | 360 | .icon(BitmapDescriptorFactory.fromBitmap(pointBitmap)) |
| 356 | 361 | .perspective(true) |
| 357 | 362 | // endMark.anchor(0.5f, 0.5f) |
| 358 | - .perspective(true); | |
| 363 | + .perspective(true); | |
| 359 | 364 | baiduMap.addOverlay(endMark); |
| 360 | 365 | } |
| 361 | 366 | //mPloyline 折线对象 |
| ... | ... | @@ -576,21 +581,21 @@ public class RunRaceDetailFragmentView implements MvpView, SensorEventListener { |
| 576 | 581 | // baiduMap.addOverlay(currentMark); |
| 577 | 582 | |
| 578 | 583 | //根据几个关键点进行步行路径规划 |
| 579 | - if (importLatLngs != null) { | |
| 580 | - if (importLatLngs.size() >= 1) { | |
| 581 | - for (int index = 0; index < importLatLngs.size(); index++) { | |
| 582 | - //开始分段规划路径 | |
| 583 | - //第一个点 | |
| 584 | - if (index == 0) { | |
| 585 | - searchRouteResult(startLatLng, importLatLngs.get(index), 0, importLatLngs.size()); | |
| 586 | - } else { | |
| 587 | - searchRouteResult(importLatLngs.get(index - 1), importLatLngs.get(index), index, importLatLngs.size()); | |
| 588 | - } | |
| 589 | - } | |
| 590 | - } else { | |
| 591 | - searchRouteResult(startLatLng, endLatLng, 0, importLatLngs.size()); | |
| 592 | - } | |
| 593 | - } | |
| 584 | +// if (importLatLngs != null) { | |
| 585 | +// if (importLatLngs.size() >= 1) { | |
| 586 | +// for (int index = 0; index < importLatLngs.size(); index++) { | |
| 587 | +// //开始分段规划路径 | |
| 588 | +// //第一个点 | |
| 589 | +// if (index == 0) { | |
| 590 | +// searchRouteResult(startLatLng, importLatLngs.get(index), 0, importLatLngs.size()); | |
| 591 | +// } else { | |
| 592 | +// searchRouteResult(importLatLngs.get(index - 1), importLatLngs.get(index), index, importLatLngs.size()); | |
| 593 | +// } | |
| 594 | +// } | |
| 595 | +// } else { | |
| 596 | +// searchRouteResult(startLatLng, endLatLng, 0, importLatLngs.size()); | |
| 597 | +// } | |
| 598 | +// } | |
| 594 | 599 | |
| 595 | 600 | } |
| 596 | 601 | } | ... | ... |
moudle_pedometer/src/main/java/com/cnlive/moudle/pedometer/frame/view/RunningMapFragmentView.java
| ... | ... | @@ -7,6 +7,7 @@ import android.hardware.Sensor; |
| 7 | 7 | import android.hardware.SensorEvent; |
| 8 | 8 | import android.hardware.SensorEventListener; |
| 9 | 9 | import android.hardware.SensorManager; |
| 10 | +import android.os.Handler; | |
| 10 | 11 | import android.os.SystemClock; |
| 11 | 12 | import android.util.Log; |
| 12 | 13 | import android.view.MotionEvent; |
| ... | ... | @@ -21,7 +22,6 @@ import com.baidu.mapapi.map.BaiduMap; |
| 21 | 22 | import com.baidu.mapapi.map.BitmapDescriptor; |
| 22 | 23 | import com.baidu.mapapi.map.BitmapDescriptorFactory; |
| 23 | 24 | import com.baidu.mapapi.map.MapStatus; |
| 24 | -import com.baidu.mapapi.map.MapStatusUpdateFactory; | |
| 25 | 25 | import com.baidu.mapapi.map.MapView; |
| 26 | 26 | import com.baidu.mapapi.map.MarkerOptions; |
| 27 | 27 | import com.baidu.mapapi.map.MyLocationConfiguration; |
| ... | ... | @@ -29,15 +29,15 @@ import com.baidu.mapapi.map.MyLocationData; |
| 29 | 29 | import com.baidu.mapapi.map.UiSettings; |
| 30 | 30 | import com.baidu.mapapi.model.LatLng; |
| 31 | 31 | import com.cnlive.moudle.pedometer.model.RunningMapBean; |
| 32 | -import com.cnlive.moudle.pedometer.ui.fragment.RunMainFragment; | |
| 33 | 32 | import com.cnlive.moudle.pedometer.ui.fragment.RunningMapFragment; |
| 34 | 33 | import com.cnlive.moudle.pedometer.utils.BitmapUtil; |
| 35 | 34 | import com.cnlive.moudle.pedometer.utils.ButtonAnimUtil; |
| 35 | +import com.cnlive.moudle.pedometer.utils.MyMapUtil; | |
| 36 | 36 | import com.cnlive.moudle.pedometer.utils.StepUtil; |
| 37 | 37 | import com.example.moudle_pedometer.R; |
| 38 | 38 | import com.hannesdorfmann.mosby3.mvp.MvpView; |
| 39 | 39 | |
| 40 | -import org.greenrobot.eventbus.EventBus; | |
| 40 | +import java.util.List; | |
| 41 | 41 | |
| 42 | 42 | public class RunningMapFragmentView implements MvpView, SensorEventListener { |
| 43 | 43 | private static final String TAG = "RunningMapFragmentView"; |
| ... | ... | @@ -62,6 +62,8 @@ public class RunningMapFragmentView implements MvpView, SensorEventListener { |
| 62 | 62 | private LocationClient mLocClient; |
| 63 | 63 | public MyLocationListenner myListener = new MyLocationListenner(); |
| 64 | 64 | private final int DEFULT_ZOOM = 17; |
| 65 | + private boolean isFirstLoadMap = true; | |
| 66 | + private List<LatLng> trackPoints; | |
| 65 | 67 | |
| 66 | 68 | public RunningMapFragmentView(RunningMapFragment fragment) { |
| 67 | 69 | this.fragment = fragment; |
| ... | ... | @@ -71,7 +73,8 @@ public class RunningMapFragmentView implements MvpView, SensorEventListener { |
| 71 | 73 | return fragment == null ? null : fragment.getActivity(); |
| 72 | 74 | } |
| 73 | 75 | |
| 74 | - public void initView() { | |
| 76 | + public void initView(List<LatLng> trackPoints) { | |
| 77 | + this.trackPoints = trackPoints; | |
| 75 | 78 | this.mMapView = fragment.binding.mapView; |
| 76 | 79 | if (baiduMap == null) { |
| 77 | 80 | baiduMap = mMapView.getMap(); |
| ... | ... | @@ -110,7 +113,21 @@ public class RunningMapFragmentView implements MvpView, SensorEventListener { |
| 110 | 113 | option.setScanSpan(3000); |
| 111 | 114 | mLocClient.setLocOption(option); |
| 112 | 115 | mLocClient.start(); |
| 113 | - | |
| 116 | + //地图加载完毕监听 | |
| 117 | + baiduMap.setOnMapLoadedCallback(new BaiduMap.OnMapLoadedCallback() { | |
| 118 | + @Override | |
| 119 | + public void onMapLoaded() { | |
| 120 | + if (isFirstLoadMap) { | |
| 121 | + isFirstLoadMap = false; | |
| 122 | + new Handler().postDelayed(new Runnable() { | |
| 123 | + @Override | |
| 124 | + public void run() { | |
| 125 | + MyMapUtil.drawMapLine(getContext(), baiduMap, trackPoints, R.color.green); | |
| 126 | + } | |
| 127 | + }, 3000); | |
| 128 | + } | |
| 129 | + } | |
| 130 | + }); | |
| 114 | 131 | //返回键 |
| 115 | 132 | fragment.binding.flBack.setOnTouchListener(new View.OnTouchListener() { |
| 116 | 133 | @Override |
| ... | ... | @@ -186,6 +203,9 @@ public class RunningMapFragmentView implements MvpView, SensorEventListener { |
| 186 | 203 | firstLatLng = new LatLng(location.getLatitude(), |
| 187 | 204 | location.getLongitude()); |
| 188 | 205 | baiduMap.setMyLocationData(mylocation); |
| 206 | + if (trackPoints!=null){ | |
| 207 | + Log.e(TAG, "trackPoints: "+MyMapUtil.isInPolygon(firstLatLng,trackPoints) ); | |
| 208 | + } | |
| 189 | 209 | // if (isFirstLoc) { |
| 190 | 210 | // builder.target(firstLatLng).zoom(DEFULT_ZOOM); |
| 191 | 211 | // isFirstLoc = false; | ... | ... |
moudle_pedometer/src/main/java/com/cnlive/moudle/pedometer/service/StepService.java
| ... | ... | @@ -315,6 +315,7 @@ public class StepService extends Service implements SensorEventListener { |
| 315 | 315 | */ |
| 316 | 316 | @Override |
| 317 | 317 | public void onPushCallback(byte messageType, PushMessage pushMessage) { |
| 318 | + Log.e(TAG, "onPushCallback: " ); | |
| 318 | 319 | if (messageType < 0x03 || messageType > 0x04) { |
| 319 | 320 | return; |
| 320 | 321 | } |
| ... | ... | @@ -322,6 +323,8 @@ public class StepService extends Service implements SensorEventListener { |
| 322 | 323 | if (null == alarmPushInfo) { |
| 323 | 324 | return; |
| 324 | 325 | } |
| 326 | + Toast.makeText(getApplicationContext(),"onPushCallback",Toast.LENGTH_SHORT).show(); | |
| 327 | + | |
| 325 | 328 | } |
| 326 | 329 | |
| 327 | 330 | @Override | ... | ... |
moudle_pedometer/src/main/java/com/cnlive/moudle/pedometer/ui/fragment/RunningMapFragment.java
| ... | ... | @@ -4,12 +4,14 @@ import android.content.Context; |
| 4 | 4 | import android.hardware.Sensor; |
| 5 | 5 | import android.hardware.SensorManager; |
| 6 | 6 | import android.os.Bundle; |
| 7 | +import android.os.Handler; | |
| 7 | 8 | import android.os.SystemClock; |
| 8 | 9 | import android.support.annotation.Nullable; |
| 9 | 10 | import android.text.TextUtils; |
| 10 | 11 | import android.util.Log; |
| 11 | 12 | import android.view.View; |
| 12 | 13 | |
| 14 | +import com.baidu.mapapi.model.LatLng; | |
| 13 | 15 | import com.cnlive.libs.base.ui.QMUIFragment; |
| 14 | 16 | import com.cnlive.libs.base.util.StatusBarUtil2; |
| 15 | 17 | import com.cnlive.moudle.pedometer.frame.presenter.RunningMapFragmentPresenter; |
| ... | ... | @@ -18,6 +20,8 @@ import com.cnlive.moudle.pedometer.model.RunningMapBean; |
| 18 | 20 | import com.cnlive.moudle.pedometer.model.Constant; |
| 19 | 21 | import com.cnlive.moudle.pedometer.model.MessageEvent; |
| 20 | 22 | import com.cnlive.moudle.pedometer.service.StepService; |
| 23 | +import com.cnlive.moudle.pedometer.utils.GsonUtil; | |
| 24 | +import com.cnlive.moudle.pedometer.utils.MyMapUtil; | |
| 21 | 25 | import com.cnlive.moudle.pedometer.utils.MySpHelper; |
| 22 | 26 | import com.cnlive.moudle.pedometer.utils.TimeUtil; |
| 23 | 27 | import com.example.moudle_pedometer.R; |
| ... | ... | @@ -27,6 +31,9 @@ import org.greenrobot.eventbus.EventBus; |
| 27 | 31 | import org.greenrobot.eventbus.Subscribe; |
| 28 | 32 | import org.greenrobot.eventbus.ThreadMode; |
| 29 | 33 | |
| 34 | +import java.util.ArrayList; | |
| 35 | +import java.util.List; | |
| 36 | + | |
| 30 | 37 | import static android.content.Context.SENSOR_SERVICE; |
| 31 | 38 | |
| 32 | 39 | /** |
| ... | ... | @@ -38,6 +45,7 @@ public class RunningMapFragment extends QMUIFragment<RunningMapFragmentView, Run |
| 38 | 45 | private static final String TAG = "RunningMapFragment"; |
| 39 | 46 | private RunningMapBean runningMapBean; |
| 40 | 47 | private SensorManager mSensorManager; |
| 48 | + private String test = "[{\"latitude\":39.933889208848,\"latitudeE6\":3.9933889208848E7,\"longitude\":116.30900265517,\"longitudeE6\":1.1630900265517001E8},{\"latitude\":39.933868507347,\"latitudeE6\":3.9933868507347E7,\"longitude\":116.30865416508,\"longitudeE6\":1.1630865416508E8},{\"latitude\":39.934756505077,\"latitudeE6\":3.9934756505077E7,\"longitude\":116.30860516469,\"longitudeE6\":1.1630860516468999E8},{\"latitude\":39.934781620502,\"latitudeE6\":3.9934781620502E7,\"longitude\":116.30961013007,\"longitudeE6\":1.1630961013007E8},{\"latitude\":39.934881620374,\"latitudeE6\":3.9934881620374E7,\"longitude\":116.30960513008,\"longitudeE6\":1.1630960513008E8},{\"latitude\":39.934077623132,\"latitudeE6\":3.9934077623132E7,\"longitude\":116.30965812942,\"longitudeE6\":1.1630965812942E8},{\"latitude\":39.933972618463,\"latitudeE6\":3.9933972618463E7,\"longitude\":116.30962113087,\"longitudeE6\":1.1630962113087E8},{\"latitude\":39.933932608856,\"latitudeE6\":3.9933932608856E7,\"longitude\":116.30953913383,\"longitudeE6\":1.1630953913383E8},{\"latitude\":39.933903844942,\"latitudeE6\":3.9933903844942E7,\"longitude\":116.30918357649,\"longitudeE6\":1.1630918357649E8}]"; | |
| 41 | 49 | |
| 42 | 50 | @Override |
| 43 | 51 | protected int getToolbarId() { |
| ... | ... | @@ -75,11 +83,12 @@ public class RunningMapFragment extends QMUIFragment<RunningMapFragmentView, Run |
| 75 | 83 | super.onViewCreated(view, savedInstanceState); |
| 76 | 84 | EventBus.getDefault().register(this); |
| 77 | 85 | mSensorManager = (SensorManager) getActivity().getSystemService(SENSOR_SERVICE);//获取传感器管理服务 |
| 86 | + List<LatLng> trackPoints = GsonUtil.jsonToList(test, LatLng.class); | |
| 78 | 87 | if (getArguments() != null) { |
| 79 | 88 | runningMapBean = (RunningMapBean) getArguments().getSerializable("runningMapBean"); |
| 80 | 89 | } |
| 81 | 90 | if (getMvpView() != null) { |
| 82 | - getMvpView().initView(); | |
| 91 | + getMvpView().initView(trackPoints); | |
| 83 | 92 | if (runningMapBean != null) { |
| 84 | 93 | getMvpView().updateChronometer(runningMapBean); |
| 85 | 94 | } |
| ... | ... | @@ -103,8 +112,16 @@ public class RunningMapFragment extends QMUIFragment<RunningMapFragmentView, Run |
| 103 | 112 | // Log.e(TAG, "onViewCreated: " + "hour:" + hour + ",minute" + minute + ",second:" + second); |
| 104 | 113 | getPresenter().queryHistoryTrace(getActivity(), StepService.entityName, startTime, endTime); |
| 105 | 114 | //查询实时轨迹 |
| 106 | - getPresenter().queryRealTimeTrace(getActivity(),StepService.entityName, startTime); | |
| 115 | + getPresenter().queryRealTimeTrace(getActivity(), StepService.entityName, startTime); | |
| 116 | + } | |
| 117 | + | |
| 118 | + | |
| 119 | + List<com.baidu.trace.model.LatLng> fenceLatLngs = new ArrayList<>(); | |
| 120 | + for (LatLng latLng : trackPoints) { | |
| 121 | + com.baidu.trace.model.LatLng tempLatLng = new com.baidu.trace.model.LatLng(latLng.latitude, latLng.longitude); | |
| 122 | + fenceLatLngs.add(tempLatLng); | |
| 107 | 123 | } |
| 124 | + getPresenter().createFence(getActivity(), fenceLatLngs); | |
| 108 | 125 | } |
| 109 | 126 | |
| 110 | 127 | @Override | ... | ... |
moudle_pedometer/src/main/java/com/cnlive/moudle/pedometer/utils/GsonUtil.java
0 → 100644
| 1 | +package com.cnlive.moudle.pedometer.utils; | |
| 2 | + | |
| 3 | +import com.google.gson.Gson; | |
| 4 | +import com.google.gson.JsonArray; | |
| 5 | +import com.google.gson.JsonElement; | |
| 6 | +import com.google.gson.JsonObject; | |
| 7 | +import com.google.gson.JsonParser; | |
| 8 | +import com.google.gson.reflect.TypeToken; | |
| 9 | + | |
| 10 | +import java.util.ArrayList; | |
| 11 | +import java.util.List; | |
| 12 | +import java.util.Map; | |
| 13 | + | |
| 14 | +/** | |
| 15 | + * Created by sijia | |
| 16 | + * on 2016/12/19. | |
| 17 | + */ | |
| 18 | +public class GsonUtil { | |
| 19 | + private static Gson gson = null; | |
| 20 | + | |
| 21 | + static { | |
| 22 | + if (gson == null) { | |
| 23 | + gson = new Gson(); | |
| 24 | + } | |
| 25 | + } | |
| 26 | + | |
| 27 | + private GsonUtil() { | |
| 28 | + } | |
| 29 | + | |
| 30 | + /** | |
| 31 | + * 转成json | |
| 32 | + * | |
| 33 | + * @param object | |
| 34 | + * @return | |
| 35 | + */ | |
| 36 | + public static String GsonString(Object object) { | |
| 37 | + String gsonString = null; | |
| 38 | + if (gson != null) { | |
| 39 | + gsonString = gson.toJson(object); | |
| 40 | + } | |
| 41 | + return gsonString; | |
| 42 | + } | |
| 43 | + | |
| 44 | + /** | |
| 45 | + * 转成bean | |
| 46 | + * | |
| 47 | + * @param gsonString | |
| 48 | + * @param cls | |
| 49 | + * @return | |
| 50 | + */ | |
| 51 | + public static <T> T GsonToBean(String gsonString, Class<T> cls) { | |
| 52 | + T t = null; | |
| 53 | + if (gson != null) { | |
| 54 | + t = gson.fromJson(gsonString, cls); | |
| 55 | + } | |
| 56 | + return t; | |
| 57 | + } | |
| 58 | + | |
| 59 | + /** | |
| 60 | + * 转成list | |
| 61 | + * 解决泛型问题 | |
| 62 | + * | |
| 63 | + * @param json | |
| 64 | + * @param cls | |
| 65 | + * @param <T> | |
| 66 | + * @return | |
| 67 | + */ | |
| 68 | + public static <T> List<T> jsonToList(String json, Class<T> cls) { | |
| 69 | + Gson gson = new Gson(); | |
| 70 | + List<T> list = new ArrayList<T>(); | |
| 71 | + JsonArray array = new JsonParser().parse(json).getAsJsonArray(); | |
| 72 | + for (final JsonElement elem : array) { | |
| 73 | + list.add(gson.fromJson(elem, cls)); | |
| 74 | + } | |
| 75 | + return list; | |
| 76 | + } | |
| 77 | + | |
| 78 | + | |
| 79 | + /** | |
| 80 | + * 转成list中有map的 | |
| 81 | + * | |
| 82 | + * @param gsonString | |
| 83 | + * @return | |
| 84 | + */ | |
| 85 | + public static List<Map<String, String>> GsonToListMaps(String gsonString) { | |
| 86 | + List<Map<String, String>> list = null; | |
| 87 | + if (gson != null) { | |
| 88 | + list = gson.fromJson(gsonString, | |
| 89 | + new TypeToken<List<Map<String, String>>>() { | |
| 90 | + }.getType()); | |
| 91 | + } | |
| 92 | + for(int i=0;i<list.size();i++){ | |
| 93 | + for(Map.Entry<String, String> entry:list.get(i).entrySet()){ | |
| 94 | + if(entry.getValue()==null) { | |
| 95 | + entry.setValue(""); | |
| 96 | + } | |
| 97 | + } | |
| 98 | + } | |
| 99 | + return list; | |
| 100 | + } | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + /** | |
| 105 | + * 转成map的 | |
| 106 | + * | |
| 107 | + * @param gsonString | |
| 108 | + * @return | |
| 109 | + */ | |
| 110 | + public static <T> Map<String, T> GsonToMaps(String gsonString) { | |
| 111 | + Map<String, T> map = null; | |
| 112 | + if (gson != null) { | |
| 113 | + map = gson.fromJson(gsonString, new TypeToken<Map<String, T>>() { | |
| 114 | + }.getType()); | |
| 115 | + } | |
| 116 | + return map; | |
| 117 | + } | |
| 118 | + | |
| 119 | + /** | |
| 120 | + * 过滤null | |
| 121 | + * @param jsonElement | |
| 122 | + * @return | |
| 123 | + */ | |
| 124 | + public static String StrConver(JsonElement jsonElement){ | |
| 125 | + if(jsonElement==null||jsonElement.isJsonNull()||jsonElement.equals("null")){ | |
| 126 | + return ""; | |
| 127 | + } | |
| 128 | + else { | |
| 129 | + return jsonElement.getAsString(); | |
| 130 | + } | |
| 131 | + } | |
| 132 | + public static int IntConver(JsonElement jsonElement){ | |
| 133 | + if(jsonElement.isJsonNull()){ | |
| 134 | + return 0; | |
| 135 | + } | |
| 136 | + else { | |
| 137 | + return jsonElement.getAsInt(); | |
| 138 | + } | |
| 139 | + } | |
| 140 | + | |
| 141 | + public static JsonObject toJsonObject(String str){ | |
| 142 | + return new JsonParser().parse(str).getAsJsonObject(); | |
| 143 | + } | |
| 144 | + | |
| 145 | + public static JsonArray toJsonArray(String str){ | |
| 146 | + return new JsonParser().parse(str).getAsJsonArray(); | |
| 147 | + } | |
| 148 | + | |
| 149 | +} | |
| 150 | + | ... | ... |
moudle_pedometer/src/main/java/com/cnlive/moudle/pedometer/utils/MyMapUtil.java
| ... | ... | @@ -210,4 +210,179 @@ public class MyMapUtil { |
| 210 | 210 | public static boolean isEqualToZero(double value) { |
| 211 | 211 | return Math.abs(value - 0.0) < 0.01 ? true : false; |
| 212 | 212 | } |
| 213 | + | |
| 214 | + | |
| 215 | + /** | |
| 216 | + * 地球半径 | |
| 217 | + */ | |
| 218 | + private static double EARTH_RADIUS = 6378138.0; | |
| 219 | + | |
| 220 | + private static double rad(double d) { | |
| 221 | + return d * Math.PI / 180.0; | |
| 222 | + } | |
| 223 | + | |
| 224 | + /** | |
| 225 | + * 计算是否在圆上(单位/千米) | |
| 226 | + * | |
| 227 | + * @param radius 半径 | |
| 228 | + * @param lat1 纬度 | |
| 229 | + * @param lng1 经度 | |
| 230 | + * @return double | |
| 231 | + * @throws | |
| 232 | + * @Title: GetDistance | |
| 233 | + * @Description: TODO() | |
| 234 | + */ | |
| 235 | + public static boolean isInCircle(double radius, double lat1, double lng1, double lat2, double lng2) { | |
| 236 | + double radLat1 = rad(lat1); | |
| 237 | + double radLat2 = rad(lat2); | |
| 238 | + double a = radLat1 - radLat2; | |
| 239 | + double b = rad(lng1) - rad(lng2); | |
| 240 | + double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + | |
| 241 | + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2))); | |
| 242 | + s = s * EARTH_RADIUS; | |
| 243 | + s = Math.round(s * 10000) / 10000; | |
| 244 | + if (s > radius) {//不在圆上 | |
| 245 | + return false; | |
| 246 | + } else { | |
| 247 | + return true; | |
| 248 | + } | |
| 249 | + } | |
| 250 | + | |
| 251 | + /** | |
| 252 | + * 是否在矩形区域内 | |
| 253 | + * | |
| 254 | + * @param lat 测试点经度 | |
| 255 | + * @param lng 测试点纬度 | |
| 256 | + * @param minLat 纬度范围限制1 | |
| 257 | + * @param maxLat 纬度范围限制2 | |
| 258 | + * @param minLng 经度限制范围1 | |
| 259 | + * @param maxLng 经度范围限制2 | |
| 260 | + * @return boolean | |
| 261 | + * @throws | |
| 262 | + * @Title: isInArea | |
| 263 | + * @Description: TODO() | |
| 264 | + */ | |
| 265 | + public static boolean isInRectangleArea(double lat, double lng, double minLat, | |
| 266 | + double maxLat, double minLng, double maxLng) { | |
| 267 | + if (isInRange(lat, minLat, maxLat)) {//如果在纬度的范围内 | |
| 268 | + if (minLng * maxLng > 0) { | |
| 269 | + if (isInRange(lng, minLng, maxLng)) { | |
| 270 | + return true; | |
| 271 | + } else { | |
| 272 | + return false; | |
| 273 | + } | |
| 274 | + } else { | |
| 275 | + if (Math.abs(minLng) + Math.abs(maxLng) < 180) { | |
| 276 | + if (isInRange(lng, minLng, maxLng)) { | |
| 277 | + return true; | |
| 278 | + } else { | |
| 279 | + return false; | |
| 280 | + } | |
| 281 | + } else { | |
| 282 | + double left = Math.max(minLng, maxLng); | |
| 283 | + double right = Math.min(minLng, maxLng); | |
| 284 | + if (isInRange(lng, left, 180) || isInRange(lng, right, -180)) { | |
| 285 | + return true; | |
| 286 | + } else { | |
| 287 | + return false; | |
| 288 | + } | |
| 289 | + } | |
| 290 | + } | |
| 291 | + } else { | |
| 292 | + return false; | |
| 293 | + } | |
| 294 | + } | |
| 295 | + | |
| 296 | + /** | |
| 297 | + * 判断是否在经纬度范围内 | |
| 298 | + * | |
| 299 | + * @param point | |
| 300 | + * @param left | |
| 301 | + * @param right | |
| 302 | + * @return boolean | |
| 303 | + * @throws | |
| 304 | + * @Title: isInRange | |
| 305 | + * @Description: TODO() | |
| 306 | + */ | |
| 307 | + public static boolean isInRange(double point, double left, double right) { | |
| 308 | + if (point >= Math.min(left, right) && point <= Math.max(left, right)) { | |
| 309 | + return true; | |
| 310 | + } else { | |
| 311 | + return false; | |
| 312 | + } | |
| 313 | + } | |
| 314 | + | |
| 315 | + /** | |
| 316 | + * 判断点是否在多边形内 | |
| 317 | + * | |
| 318 | + * @param point 测试点 | |
| 319 | + * @param pts 多边形的点 | |
| 320 | + * @return boolean | |
| 321 | + * @throws | |
| 322 | + * @Title: IsPointInPoly | |
| 323 | + * @Description: TODO() | |
| 324 | + */ | |
| 325 | + public static boolean isInPolygon(LatLng point, List<LatLng> pts) { | |
| 326 | + | |
| 327 | + int N = pts.size(); | |
| 328 | + boolean boundOrVertex = true; | |
| 329 | + int intersectCount = 0;//交叉点数量 | |
| 330 | + double precision = 2e-10; //浮点类型计算时候与0比较时候的容差 | |
| 331 | + LatLng p1, p2;//临近顶点 | |
| 332 | + LatLng p = point; //当前点 | |
| 333 | + | |
| 334 | + p1 = pts.get(0); | |
| 335 | + for (int i = 1; i <= N; ++i) { | |
| 336 | + if (p.equals(p1)) { | |
| 337 | + return boundOrVertex; | |
| 338 | + } | |
| 339 | + | |
| 340 | + p2 = pts.get(i % N); | |
| 341 | + if (p.latitude < Math.min(p1.latitude, p2.latitude) || p.latitude > Math.max(p1.latitude, p2.latitude)) { | |
| 342 | + p1 = p2; | |
| 343 | + continue; | |
| 344 | + } | |
| 345 | + | |
| 346 | + //射线穿过算法 | |
| 347 | + if (p.latitude > Math.min(p1.latitude, p2.latitude) && p.latitude < Math.max(p1.latitude, p2.latitude)) { | |
| 348 | + if (p.longitude <= Math.max(p1.longitude, p2.longitude)) { | |
| 349 | + if (p1.latitude == p2.latitude && p.longitude >= Math.min(p1.longitude, p2.longitude)) { | |
| 350 | + return boundOrVertex; | |
| 351 | + } | |
| 352 | + | |
| 353 | + if (p1.longitude == p2.longitude) { | |
| 354 | + if (p1.longitude == p.longitude) { | |
| 355 | + return boundOrVertex; | |
| 356 | + } else { | |
| 357 | + ++intersectCount; | |
| 358 | + } | |
| 359 | + } else { | |
| 360 | + double xinters = (p.latitude - p1.latitude) * (p2.longitude - p1.longitude) / (p2.latitude - p1.latitude) + p1.longitude; | |
| 361 | + if (Math.abs(p.longitude - xinters) < precision) { | |
| 362 | + return boundOrVertex; | |
| 363 | + } | |
| 364 | + | |
| 365 | + if (p.longitude < xinters) { | |
| 366 | + ++intersectCount; | |
| 367 | + } | |
| 368 | + } | |
| 369 | + } | |
| 370 | + } else { | |
| 371 | + if (p.latitude == p2.latitude && p.longitude <= p2.longitude) { | |
| 372 | + LatLng p3 = pts.get((i + 1) % N); | |
| 373 | + if (p.latitude >= Math.min(p1.latitude, p3.latitude) && p.latitude <= Math.max(p1.latitude, p3.latitude)) { | |
| 374 | + ++intersectCount; | |
| 375 | + } else { | |
| 376 | + intersectCount += 2; | |
| 377 | + } | |
| 378 | + } | |
| 379 | + } | |
| 380 | + p1 = p2; | |
| 381 | + } | |
| 382 | + if (intersectCount % 2 == 0) {//偶数在多边形外 | |
| 383 | + return false; | |
| 384 | + } else { //奇数在多边形内 | |
| 385 | + return true; | |
| 386 | + } | |
| 387 | + } | |
| 213 | 388 | } | ... | ... |