diff --git a/libraries/map-amap/src/main/java/com/mogo/map/impl/amap/AMapNaviViewWrapper.java b/libraries/map-amap/src/main/java/com/mogo/map/impl/amap/AMapNaviViewWrapper.java index edb635f11a..41847b9f10 100644 --- a/libraries/map-amap/src/main/java/com/mogo/map/impl/amap/AMapNaviViewWrapper.java +++ b/libraries/map-amap/src/main/java/com/mogo/map/impl/amap/AMapNaviViewWrapper.java @@ -37,6 +37,7 @@ import com.mogo.map.impl.amap.marker.AMapMarkerWrapper; import com.mogo.map.impl.amap.message.AMapMessageListener; import com.mogo.map.impl.amap.message.AMapMessageManager; import com.mogo.map.impl.amap.navi.NaviClient; +import com.mogo.map.impl.amap.utils.MogoMapUtils; import com.mogo.map.impl.amap.utils.ObjectUtils; import com.mogo.map.listener.MogoMapListenerHandler; import com.mogo.map.marker.IMogoMarker; @@ -431,7 +432,7 @@ public class AMapNaviViewWrapper implements IMogoMapView, Logger.e( TAG, "latlng = null or is illegal" ); return; } - mockTouchEvent(); + loseLockMode(); mMapView.getMap() .moveCamera( CameraUpdateFactory.newLatLng( new LatLng( latLng.lat, latLng.lng ) ) ); } @@ -471,6 +472,24 @@ public class AMapNaviViewWrapper implements IMogoMapView, } } + @Override + public void loseLockMode() { + mockTouchEvent(); + } + + /** + * 模拟点击事件,达到锁车->普通事件 + */ + private void mockTouchEvent() { + long downTime = SystemClock.uptimeMillis(); + long eventTime = SystemClock.uptimeMillis() + 100; + int metaState = 0; + MotionEvent motionEvent = MotionEvent.obtain( downTime, eventTime, MotionEvent.ACTION_DOWN, 0, 0, metaState ); + mMapView.dispatchTouchEvent( motionEvent ); + MotionEvent upEvent = MotionEvent.obtain( downTime + 100, eventTime + 100, MotionEvent.ACTION_UP, 0, 0, metaState ); + mMapView.dispatchTouchEvent( upEvent ); + } + @Override public void setLockZoom( int var1 ) { if ( checkAMapView() ) { @@ -628,19 +647,6 @@ public class AMapNaviViewWrapper implements IMogoMapView, } } - /** - * 模拟点击事件,达到锁车->普通事件 - */ - private void mockTouchEvent() { - long downTime = SystemClock.uptimeMillis(); - long eventTime = SystemClock.uptimeMillis() + 100; - int metaState = 0; - MotionEvent motionEvent = MotionEvent.obtain( downTime, eventTime, MotionEvent.ACTION_DOWN, 0, 0, metaState ); - mMapView.dispatchTouchEvent( motionEvent ); - MotionEvent upEvent = MotionEvent.obtain( downTime + 100, eventTime + 100, MotionEvent.ACTION_UP, 0, 0, metaState ); - mMapView.dispatchTouchEvent( upEvent ); - } - @Override public void setRenderFps( int fps ) { if ( checkAMapView() ) { @@ -652,24 +658,18 @@ public class AMapNaviViewWrapper implements IMogoMapView, } @Override - public void showBounds( List< MogoLatLng > lonLats, Rect bound ) { - if ( checkAMapView() ) { - if ( lonLats == null ) { - return; - } - try { - LatLngBounds.Builder builder = new LatLngBounds.Builder(); - for ( MogoLatLng lonLat : lonLats ) { - builder.include( ObjectUtils.fromMogo2( lonLat ) ); - } - if ( bound == null ) { - bound = new Rect(); - } - LatLngBounds latLngBounds = builder.build(); - mMapView.getMap().moveCamera( CameraUpdateFactory.newLatLngBoundsRect( latLngBounds, bound.left, bound.right, bound.top, bound.bottom ) ); - } catch ( Exception e ) { - Logger.e( TAG, e, "error." ); + public void showBounds( String tag, MogoLatLng carPosition, List< MogoLatLng > lonLats, Rect bound, boolean lockCarPosition ) { + if ( !checkAMapView() ) { + return; + } + try { + LatLngBounds latLngBounds = MogoMapUtils.getLatLngBounds( carPosition, lonLats, lockCarPosition ); + if ( !lockCarPosition ) { + loseLockMode(); } + mMapView.getMap().moveCamera( CameraUpdateFactory.newLatLngBoundsRect( latLngBounds, bound.left, bound.right, bound.top, bound.bottom ) ); + } catch ( Exception e ) { + Logger.e( TAG, e, "%s error.", tag ); } } diff --git a/libraries/map-amap/src/main/java/com/mogo/map/impl/amap/uicontroller/AMapUIController.java b/libraries/map-amap/src/main/java/com/mogo/map/impl/amap/uicontroller/AMapUIController.java index 5d653eefbc..cd0790f4af 100644 --- a/libraries/map-amap/src/main/java/com/mogo/map/impl/amap/uicontroller/AMapUIController.java +++ b/libraries/map-amap/src/main/java/com/mogo/map/impl/amap/uicontroller/AMapUIController.java @@ -98,6 +98,13 @@ public class AMapUIController implements IMogoMapUIController { } } + @Override + public void loseLockMode() { + if ( mClient != null ) { + mClient.loseLockMode(); + } + } + @Override public void setLockZoom(int var1) { if ( mClient != null ) { @@ -189,9 +196,9 @@ public class AMapUIController implements IMogoMapUIController { } @Override - public void showBounds( List< MogoLatLng > lonLats, Rect bound ) { + public void showBounds( String tag, MogoLatLng carPosition, List< MogoLatLng > lonLats, Rect bound, boolean lockCarPosition ) { if ( mClient != null ) { - mClient.showBounds( lonLats, bound ); + mClient.showBounds( tag, carPosition, lonLats, bound, lockCarPosition ); } } diff --git a/libraries/map-amap/src/main/java/com/mogo/map/impl/amap/utils/MogoMapUtils.java b/libraries/map-amap/src/main/java/com/mogo/map/impl/amap/utils/MogoMapUtils.java new file mode 100644 index 0000000000..d4da664812 --- /dev/null +++ b/libraries/map-amap/src/main/java/com/mogo/map/impl/amap/utils/MogoMapUtils.java @@ -0,0 +1,79 @@ +package com.mogo.map.impl.amap.utils; + +import com.amap.api.maps.model.LatLng; +import com.amap.api.maps.model.LatLngBounds; +import com.mogo.map.MogoLatLng; +import com.mogo.map.exception.MogoMapException; + +import java.util.List; + +/** + * @author congtaowang + * @since 2020-03-04 + *

+ * 地图工具类 + */ +public class 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; + } + + return new LatLngBounds.Builder().include( new LatLng( east, north ) ).include( new LatLng( west, south ) ).build(); + } +} diff --git a/libraries/mogo-map-api/src/main/java/com/mogo/map/uicontroller/IMogoMapUIController.java b/libraries/mogo-map-api/src/main/java/com/mogo/map/uicontroller/IMogoMapUIController.java index b424b85395..a3546cc25f 100644 --- a/libraries/mogo-map-api/src/main/java/com/mogo/map/uicontroller/IMogoMapUIController.java +++ b/libraries/mogo-map-api/src/main/java/com/mogo/map/uicontroller/IMogoMapUIController.java @@ -58,10 +58,15 @@ public interface IMogoMapUIController { void showMyLocation( boolean visible ); /** - * 解锁锁车 + * 锁车 */ void recoverLockMode(); + /** + * 解锁锁车 + */ + void loseLockMode(); + /** * 设置锁车缩放级别 * @@ -151,10 +156,13 @@ public interface IMogoMapUIController { void setRenderFps( int fps ); /** - * @param lonLats 经纬度围成的范围 - * @param bound 地图上可显示的范围 + * @param tag 调用业务 + * @param carPosition 当前位置 + * @param lonLats 经纬度围成的范围 + * @param bound 地图上可显示的范围 + * @param lockCarPosition 是否锁定车辆位置 */ - void showBounds( List< MogoLatLng > lonLats, Rect bound ); + void showBounds( String tag, MogoLatLng carPosition, List< MogoLatLng > lonLats, Rect bound, boolean lockCarPosition ); /** * 强制刷新地图 diff --git a/libraries/mogo-map/src/main/java/com/mogo/map/MogoMapUIController.java b/libraries/mogo-map/src/main/java/com/mogo/map/MogoMapUIController.java index eba856a3f6..a75142cc95 100644 --- a/libraries/mogo-map/src/main/java/com/mogo/map/MogoMapUIController.java +++ b/libraries/mogo-map/src/main/java/com/mogo/map/MogoMapUIController.java @@ -91,6 +91,13 @@ public class MogoMapUIController implements IMogoMapUIController { } } + @Override + public void loseLockMode() { + if ( mDelegate != null ) { + mDelegate.loseLockMode(); + } + } + @Override public void setLockZoom(int var1) { if (mDelegate != null) { @@ -182,9 +189,9 @@ public class MogoMapUIController implements IMogoMapUIController { } @Override - public void showBounds( List< MogoLatLng > lonLats, Rect bound ) { + public void showBounds(String tag, MogoLatLng carPosition, List< MogoLatLng > lonLats, Rect bound, boolean lockCarPosition ) { if ( mDelegate != null ) { - mDelegate.showBounds( lonLats, bound ); + mDelegate.showBounds( tag, carPosition, lonLats, bound, lockCarPosition ); } }