From a407383baf198fa28bc51f07ece2ee52e361fcca Mon Sep 17 00:00:00 2001 From: jiaguofeng Date: Mon, 22 Jun 2020 19:40:57 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=87=AA=E5=8A=A8=E7=BC=A9?= =?UTF-8?q?=E6=94=BE=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mogo/map/impl/custom/AMapViewWrapper.java | 21 ++- .../map/impl/custom/utils/MogoMapUtils.java | 155 +++++++++--------- 2 files changed, 93 insertions(+), 83 deletions(-) diff --git a/libraries/map-custom/src/main/java/com/mogo/map/impl/custom/AMapViewWrapper.java b/libraries/map-custom/src/main/java/com/mogo/map/impl/custom/AMapViewWrapper.java index 58a6e726ad..004359c61a 100644 --- a/libraries/map-custom/src/main/java/com/mogo/map/impl/custom/AMapViewWrapper.java +++ b/libraries/map-custom/src/main/java/com/mogo/map/impl/custom/AMapViewWrapper.java @@ -34,6 +34,10 @@ import com.zhidaoauto.map.sdk.open.abs.OnMapClickListener; import com.zhidaoauto.map.sdk.open.abs.OnMapLoadedListener; import com.zhidaoauto.map.sdk.open.abs.OnMapTouchListener; import com.zhidaoauto.map.sdk.open.camera.CameraPosition; +import com.zhidaoauto.map.sdk.open.camera.CameraUpdate; +import com.zhidaoauto.map.sdk.open.camera.CameraUpdateFactory; +import com.zhidaoauto.map.sdk.open.camera.LatLngBounds; +import com.zhidaoauto.map.sdk.open.camera.MapCameraMessage; import com.zhidaoauto.map.sdk.open.location.LocationListener; import com.zhidaoauto.map.sdk.open.location.MyLocationStyle; import com.zhidaoauto.map.sdk.open.marker.BitmapDescriptorFactory; @@ -548,14 +552,15 @@ public class AMapViewWrapper implements IMogoMapView, IMogoMapUIController, Loca return; } try { -// if ( DebugConfig.isDebug() ) { -// Logger.d( TAG, Log.getStackTraceString( new Throwable() ) ); -// } -// LatLngBounds latLngBounds = MogoMapUtils.getLatLngBounds( carPosition, lonLats, lockCarPosition ); -// if ( !lockCarPosition ) { -// loseLockMode(); -// } -// mMapView.getMapAutoViewHelper().moveCamera( new CameraUpdate(new MapCameraMessage( latLngBounds, bound.left, bound.right, bound.top, bound.bottom ) )); + if ( DebugConfig.isDebug() ) { + Logger.d( TAG, Log.getStackTraceString( new Throwable() ) ); + } + LatLngBounds latLngBounds = MogoMapUtils.getLatLngBounds( carPosition, lonLats, lockCarPosition ); + if ( !lockCarPosition ) { + loseLockMode(); + } + mMapView.getMapAutoViewHelper().setLocation(ObjectUtils.fromMogo(carPosition)); + mMapView.getMapAutoViewHelper().moveCamera( CameraUpdateFactory.INSTANCE.newLatLngBounds( latLngBounds, bound.left, bound.right, bound.top, bound.bottom ) ); } catch (Exception e) { Logger.e(TAG, e, "%s error.", tag); } diff --git a/libraries/map-custom/src/main/java/com/mogo/map/impl/custom/utils/MogoMapUtils.java b/libraries/map-custom/src/main/java/com/mogo/map/impl/custom/utils/MogoMapUtils.java index a539cc63bc..bd5865b560 100644 --- a/libraries/map-custom/src/main/java/com/mogo/map/impl/custom/utils/MogoMapUtils.java +++ b/libraries/map-custom/src/main/java/com/mogo/map/impl/custom/utils/MogoMapUtils.java @@ -1,7 +1,14 @@ package com.mogo.map.impl.custom.utils; +import android.util.Log; + +import com.mogo.map.MogoLatLng; +import com.mogo.map.exception.MogoMapException; +import com.zhidaoauto.map.sdk.open.camera.LatLngBounds; import com.zhidaoauto.map.sdk.open.query.LonLatPoint; +import java.util.List; + /** * @author congtaowang * @since 2020-03-04 @@ -12,81 +19,79 @@ public class MogoMapUtils { private static final String TAG = "MogoMapUtils"; -// public static LatLngBounds getLatLngBounds( MogoLatLng carPosition, List< MogoLatLng > lonLats, boolean lockCarPosition ) throws Exception { -// -// if ( lonLats == null || lonLats.isEmpty() ) { -// throw new MogoMapException( "经纬度不能为null或空集合" ); -// } -// LatLngBounds.Builder builder = new LatLngBounds.Builder(); -// for ( MogoLatLng lonLat : lonLats ) { -// builder.include( ObjectUtils.fromMogo2( lonLat ) ); -// } -// if ( carPosition != null && !lockCarPosition ) { -// builder.include( ObjectUtils.fromMogo2( carPosition ) ); -// } -// LatLngBounds latLngBounds = builder.build(); -// if ( !lockCarPosition ) { -// return latLngBounds; -// } -// -// if ( carPosition == null ) { -// throw new MogoMapException( "自车位置经纬度信息不能为空" ); -// } -// -// if ( latLngBounds.northeast == null && latLngBounds.southwest == null ) { -// return null; -// } -// -// double south = 0.0; -// double west = 0.0; -// -// double east = 0.0; -// double north = 0.0; -// -// double dLat = 0.0; -// double dLon = 0.0; -// -// if ( latLngBounds.northeast == null ) { -// dLat = Math.abs( carPosition.lat - latLngBounds.southwest.latitude ); -// dLon = Math.abs( carPosition.lng - latLngBounds.southwest.longitude ); -// } else if ( latLngBounds.southwest == null ) { -// dLat = Math.abs( carPosition.lat - latLngBounds.northeast.latitude ); -// dLon = Math.abs( carPosition.lng - latLngBounds.northeast.longitude ); -// } else { -// final double dLat1 = Math.abs( carPosition.lat - latLngBounds.southwest.latitude ); -// final double dLon1 = Math.abs( carPosition.lng - latLngBounds.southwest.longitude ); -// final double dLat2 = Math.abs( carPosition.lat - latLngBounds.northeast.latitude ); -// final double dLon2 = Math.abs( carPosition.lng - latLngBounds.northeast.longitude ); -// dLat = dLat1 > dLat2 ? dLat1 : dLat2; -// dLon = dLon1 > dLon2 ? dLon1 : dLon2; -// } -// -// west = carPosition.lat - dLat; -// south = carPosition.lng + dLon; -// -// east = carPosition.lat + dLat; -// north = carPosition.lng - dLon; -// -// if ( south == 0.0 || west == 0.0 || east == 0.0 || north == 0.0 ) { -// return null; -// } -// -// if ( east < west ) { -// double tmp = east; -// east = west; -// west = tmp; -// } -// -// if( north < south ){ -// double tmp = north; -// north = south; -// south = tmp; -// } -// -// Logger.d( TAG, "west = %s, east = %s, north = %s, south = %s", west, east, north, south ); -// -// return new LatLngBounds.Builder().include( new LatLng( east, north ) ).include( new LatLng( west, south ) ).build(); -// } + public static LatLngBounds getLatLngBounds(MogoLatLng carPosition, List< MogoLatLng > lonLats, boolean lockCarPosition ) throws Exception { + + if ( lonLats == null || lonLats.isEmpty() ) { + throw new MogoMapException( "经纬度不能为null或空集合" ); + } + LatLngBounds.Builder builder = new LatLngBounds.Builder(); + for ( MogoLatLng lonLat : lonLats ) { + builder.include( ObjectUtils.fromMogo( lonLat ) ); + } + if ( carPosition != null && !lockCarPosition ) { + builder.include( ObjectUtils.fromMogo( carPosition ) ); + } + LatLngBounds latLngBounds = builder.build(); + if ( !lockCarPosition ) { + return latLngBounds; + } + + if ( carPosition == null ) { + throw new MogoMapException( "自车位置经纬度信息不能为空" ); + } + + if ( latLngBounds.getNortheast() == null && latLngBounds.getSouthwest() == null ) { + return null; + } + + double south = 0.0; + double west = 0.0; + + double east = 0.0; + double north = 0.0; + + double dLat = 0.0; + double dLon = 0.0; + + if ( latLngBounds.getNortheast() == null ) { + dLat = Math.abs( carPosition.lat - latLngBounds.getSouthwest().getLatitude() ); + dLon = Math.abs( carPosition.lng - latLngBounds.getSouthwest().getLongitude() ); + } else if ( latLngBounds.getSouthwest() == null ) { + dLat = Math.abs( carPosition.lat - latLngBounds.getNortheast().getLatitude() ); + dLon = Math.abs( carPosition.lng - latLngBounds.getNortheast().getLongitude() ); + } else { + final double dLat1 = Math.abs( carPosition.lat - latLngBounds.getSouthwest().getLatitude() ); + final double dLon1 = Math.abs( carPosition.lng - latLngBounds.getSouthwest().getLongitude() ); + final double dLat2 = Math.abs( carPosition.lat - latLngBounds.getNortheast().getLatitude() ); + final double dLon2 = Math.abs( carPosition.lng - latLngBounds.getNortheast().getLongitude() ); + dLat = dLat1 > dLat2 ? dLat1 : dLat2; + dLon = dLon1 > dLon2 ? dLon1 : dLon2; + } + + west = carPosition.lat - dLat; + south = carPosition.lng + dLon; + + east = carPosition.lat + dLat; + north = carPosition.lng - dLon; + + if ( south == 0.0 || west == 0.0 || east == 0.0 || north == 0.0 ) { + return null; + } + + if ( east < west ) { + double tmp = east; + east = west; + west = tmp; + } + + if( north < south ){ + double tmp = north; + north = south; + south = tmp; + } + + return new LatLngBounds.Builder().include( new LonLatPoint( east, north ) ).include( new LonLatPoint( west, south ) ).build(); + } public static float calculateLineDistance(LonLatPoint var0, LonLatPoint var1) { if (var0 != null && var1 != null) {