Revert "删除lib/my_flutter_bmfmap-1.0.2,改用子模块"

This reverts commit 108aee4f2f.
This commit is contained in:
2022-05-07 21:19:20 +08:00
parent ecb8f2f1c2
commit ce7d7f5a79
207 changed files with 23580 additions and 6 deletions
@@ -0,0 +1,8 @@
*.iml
.gradle
/local.properties
/.idea/workspace.xml
/.idea/libraries
.DS_Store
/build
/captures
@@ -0,0 +1,46 @@
group 'com.baidu.flutter_bmfmap'
version '1.0'
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.0'
}
}
rootProject.allprojects {
repositories {
google()
jcenter()
}
}
apply plugin: 'com.android.library'
android {
compileSdkVersion 28
defaultConfig {
minSdkVersion 16
}
lintOptions {
disable 'InvalidPackage'
}
}
repositories {
mavenLocal()
}
dependencies {
implementation fileTree(includes: ['*.jar'], dir: 'libs')
implementation rootProject.findProject(":flutter_bmfbase")
implementation 'androidx.localbroadcastmanager:localbroadcastmanager:1.0.0'
}
@@ -0,0 +1,4 @@
org.gradle.jvmargs=-Xmx1536M
android.enableR8=true
android.useAndroidX=true
android.enableJetifier=true
@@ -0,0 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.2-all.zip
@@ -0,0 +1 @@
rootProject.name = 'flutter_bmfmap'
@@ -0,0 +1,3 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.baidu.flutter_bmfmap">
</manifest>
@@ -0,0 +1,38 @@
package com.baidu.flutter_bmfmap;
import android.content.Context;
import com.baidu.mapapi.map.MapView;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.EventChannel;
import io.flutter.plugin.common.MethodChannel;
public class BMFEventHandler<ViewType> implements EventChannel.StreamHandler {
private Context mContext;
private ViewType mMapView;
private BinaryMessenger mMessager;
private MethodChannel mMethodChannel;
private EventChannel mEventChannel;
public BMFEventHandler(Context context, ViewType mapView, MethodChannel methodChannel, EventChannel eventChannel){
mContext = context;
mMapView = mapView;
mMethodChannel = methodChannel;
mEventChannel = eventChannel;
}
@Override
public void onListen(Object arguments, EventChannel.EventSink events) {
}
@Override
public void onCancel(Object arguments) {
}
}
@@ -0,0 +1,36 @@
package com.baidu.flutter_bmfmap;
import android.content.Context;
import com.baidu.flutter_bmfmap.map.FlutterCommonMapView;
import io.flutter.plugin.common.EventChannel;
import io.flutter.plugin.common.MethodChannel;
public class BMFHandlerHelper <ViewType>{
private MethodChannel mMethodChannel;
private BMFMethodHandler mBMFMethodHandler;
private EventChannel mEventChannel;
private BMFEventHandler mBMFEventHandler;
public BMFHandlerHelper(Context context
, FlutterCommonMapView mapView
, MethodChannel methodChannel
, EventChannel eventChannel){
init(context, mapView, methodChannel, eventChannel);
}
private void init(Context context, FlutterCommonMapView mapView, MethodChannel methodChannel, EventChannel eventChannel){
mMethodChannel = methodChannel;
mBMFMethodHandler = new BMFMethodHandler(context, mapView, methodChannel, eventChannel);
mMethodChannel.setMethodCallHandler(mBMFMethodHandler);
mEventChannel = eventChannel;
mBMFEventHandler = new BMFEventHandler(context, mapView, methodChannel, eventChannel);
mEventChannel.setStreamHandler(mBMFEventHandler);
}
}
@@ -0,0 +1,62 @@
package com.baidu.flutter_bmfmap;
import android.content.Context;
import android.util.Log;
import com.baidu.flutter_bmfmap.map.mapHandler.BMapHandlerFactory;
import com.baidu.flutter_bmfmap.map.FlutterCommonMapView;
import com.baidu.flutter_bmfmap.map.overlayHandler.OverlayHandlerFactory;
import com.baidu.flutter_bmfmap.utils.Env;
import com.baidu.mapapi.map.BaiduMap;
import io.flutter.plugin.common.EventChannel;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
public class BMFMethodHandler implements MethodChannel.MethodCallHandler {
private static final String TAG = "BMFMethodHandler";
private Context mContext;
private FlutterCommonMapView mMapView;
private final BaiduMap mBaiduMap;
private MethodChannel mMethodChannel;
private EventChannel mEventChannel;
public BMFMethodHandler(Context context
,FlutterCommonMapView mapView
,MethodChannel methodChannel
,EventChannel eventChannel){
mContext = context;
mMapView = mapView;
mBaiduMap = mapView.getBaiduMap();
mMethodChannel = methodChannel;
mEventChannel = eventChannel;
}
@Override
public void onMethodCall(MethodCall call, MethodChannel.Result result) {
if(Env.DEBUG){
Log.d(TAG,"onMethodCall enter");
}
if(null == call || null == result){
Log.d(TAG,"null == call || null == result");
return;
}
if (null == mMapView || null == mBaiduMap) {
Log.d(TAG,"mMapView == call || mBaiduMap == result");
return;
}
boolean ret = OverlayHandlerFactory.getInstance(mBaiduMap).dispatchMethodHandler(call,
result);
if (!ret) {
BMapHandlerFactory.getInstance(mMapView).dispatchMethodHandler(mContext,call,
result, mMethodChannel);
}
}
}
@@ -0,0 +1,127 @@
package com.baidu.flutter_bmfmap;
import android.util.Log;
import androidx.annotation.NonNull;
import com.baidu.flutter_bmfmap.map.OfflineHandler;
import com.baidu.flutter_bmfmap.utils.Constants;
import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.embedding.engine.plugins.activity.ActivityAware;
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry.Registrar;
import io.flutter.plugin.platform.PlatformViewRegistry;
/** FlutterBmfmapPlugin */
public class FlutterBmfmapPlugin implements FlutterPlugin, ActivityAware, MethodCallHandler {
private static final String TAG = FlutterBmfmapPlugin.class.getSimpleName();
private OfflineHandler mOfflineHandler;
private PlatformViewRegistry mPlatformViewRegistry;
private BinaryMessenger mMessenger;
@Override
public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
if(null == flutterPluginBinding){
return;
}
mMessenger = flutterPluginBinding.getBinaryMessenger();
if (null == mMessenger) {
return;
}
mOfflineHandler = new OfflineHandler();
mOfflineHandler.init(mMessenger);
mPlatformViewRegistry = flutterPluginBinding.getPlatformViewRegistry();
}
// This static function is optional and equivalent to onAttachedToEngine. It supports the old
// pre-Flutter-1.12 Android projects. You are encouraged to continue supporting
// plugin registration via this function while apps migrate to use the new Android APIs
// post-flutter-1.12 via https://flutter.dev/go/android-project-migration.
//
// It is encouraged to share logic between onAttachedToEngine and registerWith to keep
// them functionally equivalent. Only one of onAttachedToEngine or registerWith will be called
// depending on the user's project. onAttachedToEngine or registerWith must both be defined
// in the same class.
public static void registerWith(Registrar registrar) {
OfflineHandler offlineHandler = new OfflineHandler();
offlineHandler.init(registrar.messenger());
registrar.platformViewRegistry().registerViewFactory(
Constants.ViewType.sMapView,
new MapViewFactory(registrar.activity()
, registrar.messenger()
, Constants.ViewType.sMapView));
registrar.platformViewRegistry().registerViewFactory(
Constants.ViewType.sTextureMapView,
new TextureMapViewFactory(registrar.activity()
, registrar.messenger()
, Constants.ViewType.sTextureMapView));
}
@Override
public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
if (call.method.equals("getPlatformVersion")) {
result.success("Android " + android.os.Build.VERSION.RELEASE);
}else{
}
}
@Override
public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
if(null == binding){
return;
}
BinaryMessenger binaryMessenger = binding.getBinaryMessenger();
if(null == binaryMessenger){
return;
}
mOfflineHandler.unInit(binding.getBinaryMessenger());
}
@Override
public void onAttachedToActivity(ActivityPluginBinding binding) {
if(null == binding || null == mPlatformViewRegistry || null == mMessenger){
return;
}
mPlatformViewRegistry.registerViewFactory(
Constants.ViewType.sMapView,
new MapViewFactory(binding.getActivity()
, mMessenger
, Constants.ViewType.sMapView));
mPlatformViewRegistry.registerViewFactory(
Constants.ViewType.sTextureMapView,
new TextureMapViewFactory(binding.getActivity()
, mMessenger
, Constants.ViewType.sTextureMapView));
}
@Override
public void onDetachedFromActivityForConfigChanges() {
Log.d(TAG, "onDetachedFromActivityForConfigChanges");
}
@Override
public void onReattachedToActivityForConfigChanges(ActivityPluginBinding binding) {
Log.d(TAG, "onReattachedToActivityForConfigChanges");
}
@Override
public void onDetachedFromActivity() {
Log.d(TAG, "onDetachedFromActivity");
}
}
@@ -0,0 +1,40 @@
package com.baidu.flutter_bmfmap;
import android.content.Context;
import android.util.Log;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.StandardMessageCodec;
import io.flutter.plugin.platform.PlatformView;
import io.flutter.plugin.platform.PlatformViewFactory;
import com.baidu.flutter_bmfmap.map.FlutterMapView;
import com.baidu.flutter_bmfmap.utils.Env;
public class MapViewFactory extends PlatformViewFactory {
private static final String TAG = "ViewFactory";
private BinaryMessenger mMessenger;
private Context mContext;
private String mViewType;
/**
* @param messenger the codec used to decode the args parameter of {@link #create}.
*/
public MapViewFactory(Context context, BinaryMessenger messenger, String viewType) {
super(StandardMessageCodec.INSTANCE);
if(Env.DEBUG){
Log.d(TAG, "ViewFactory");
}
mContext = context;
mMessenger = messenger;
mViewType = viewType;
}
@Override
public PlatformView create(Context context, int viewId, Object args) {
if(Env.DEBUG){
Log.d(TAG, "create");
}
return new FlutterMapView(mContext, mMessenger, viewId, args, mViewType);
}
}
@@ -0,0 +1,42 @@
package com.baidu.flutter_bmfmap;
import android.content.Context;
import android.util.Log;
import com.baidu.flutter_bmfmap.map.FlutterMapView;
import com.baidu.flutter_bmfmap.map.FlutterTextureMapView;
import com.baidu.flutter_bmfmap.utils.Env;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.MessageCodec;
import io.flutter.plugin.common.StandardMessageCodec;
import io.flutter.plugin.platform.PlatformView;
import io.flutter.plugin.platform.PlatformViewFactory;
public class TextureMapViewFactory extends PlatformViewFactory {
private static final String TAG = "ViewFactory";
private BinaryMessenger mMessenger;
private Context mContext;
private String mViewType;
/**
* @param messenger the codec used to decode the args parameter of {@link #create}.
*/
public TextureMapViewFactory(Context context, BinaryMessenger messenger, String viewType) {
super(StandardMessageCodec.INSTANCE);
if(Env.DEBUG){
Log.d(TAG, "ViewFactory");
}
mContext = context;
mMessenger = messenger;
mViewType = viewType;
}
@Override
public PlatformView create(Context context, int viewId, Object args) {
if(Env.DEBUG){
Log.d(TAG, "create");
}
return new FlutterTextureMapView(mContext, mMessenger, viewId, args, mViewType);
}
}
@@ -0,0 +1,27 @@
package com.baidu.flutter_bmfmap.map;
import java.util.Map;
public abstract class FlutterBaseMapView {
protected String mViewType;
protected boolean mResume = false;
protected int mGetViewCount = 0;
protected abstract void init(int viewId, Object args);
protected abstract void initMapView(Object args, FlutterCommonMapView flutterCommonMapView);
protected void initMapStatus(Map<String, Object> mapOptionsMap,
FlutterCommonMapView flutterCommonMapView) {
if (null == mapOptionsMap) {
return;
}
MapStateUpdateImp.getInstance()
.setCommView(flutterCommonMapView)
.updateMapState(mapOptionsMap);
}
}
@@ -0,0 +1,58 @@
package com.baidu.flutter_bmfmap.map;
import com.baidu.flutter_bmfmap.utils.Constants;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.TextureMapView;
import java.util.Map;
public abstract class FlutterCommonMapView{
protected String mViewType;
public String getViewType(){
return mViewType;
}
public void setmViewType(String viewType){
mViewType = viewType;
}
abstract public MapView getMapView();
abstract public TextureMapView getTextureMapView();
public BaiduMap getBaiduMap(){
BaiduMap baiduMap = null;
switch (mViewType){
case Constants.ViewType.sMapView:
baiduMap = getBaiduMapFromMapView();
break;
case Constants.ViewType.sTextureMapView:
baiduMap = getBaiduMapFromTextureMapView();
break;
default:
break;
}
return baiduMap;
}
private BaiduMap getBaiduMapFromMapView(){
MapView mapView = this.getMapView();
if(null == mapView){
return null;
}
return mapView.getMap();
}
private BaiduMap getBaiduMapFromTextureMapView(){
TextureMapView textureMapView = this.getTextureMapView();
if(null == textureMapView){
return null;
}
return textureMapView.getMap();
}
}
@@ -0,0 +1,170 @@
package com.baidu.flutter_bmfmap.map;
import static com.baidu.flutter_bmfmap.utils.Constants.MAX_GET_VIEW_CNT_BY_FLUTTER_RESIZE;
import java.util.Map;
import com.baidu.flutter_bmfmap.BMFHandlerHelper;
import com.baidu.flutter_bmfmap.map.mapHandler.BMapHandlerFactory;
import com.baidu.flutter_bmfmap.map.overlayHandler.OverlayHandlerFactory;
import com.baidu.flutter_bmfmap.utils.Constants;
import com.baidu.flutter_bmfmap.utils.Env;
import com.baidu.mapapi.map.MapView;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.EventChannel;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.platform.PlatformView;
public class FlutterMapView extends FlutterBaseMapView implements PlatformView {
private static final String TAG = "FlutterMapView";
private MapView mMapView;
private Context mContext;
private BinaryMessenger mMessager;
private BMFHandlerHelper mBMFHandlerHelper;
private MethodChannel mMethodChannel;
private EventChannel mEventChannel;
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (Constants.sConfigChangedAction.equals(action) && !mResume) {
mResume = true;
}
}
};
public FlutterMapView(Context context,
BinaryMessenger messenger,
int viewId,
Object args,
String viewType) {
if (Env.DEBUG) {
Log.d(TAG, "FlutterMapView");
}
mContext = context;
mMessager = messenger;
mViewType = viewType;
init(viewId, args);
}
protected void init(int viewId, Object args) {
if (Env.DEBUG) {
Log.d(TAG, "init");
}
mMapView = new MapView(mContext);
FlutterCommonMapView mapViewWrapper = new MapViewWrapper(this, mViewType);
initMapView(args, mapViewWrapper);
mMethodChannel = new MethodChannel(mMessager,
Constants.VIEW_METHOD_CHANNEL_PREFIX + (char) (viewId + 97));
mEventChannel = new EventChannel(mMessager,
Constants.VIEW_EVENT_CHANNEL_PREFIX + (char) (viewId + 97));
mBMFHandlerHelper =
new BMFHandlerHelper(mContext, mapViewWrapper, mMethodChannel, mEventChannel);
new MapListener(new MapViewWrapper(this, mViewType), mMethodChannel);
IntentFilter intentFilter = new IntentFilter(Constants.sConfigChangedAction);
LocalBroadcastManager.getInstance(mContext).registerReceiver(mReceiver, intentFilter);
if (Env.DEBUG) {
Log.d(TAG, "init success");
}
}
protected void initMapView(Object args, FlutterCommonMapView flutterCommonMapView) {
if (null == mContext) {
return;
}
Map<String, Object> mapOptionsMap = (Map<String, Object>) args;
if (null == mapOptionsMap) {
return;
}
initMapStatus(mapOptionsMap, flutterCommonMapView);
}
@Override
public View getView() {
if (Env.DEBUG) {
Log.d(TAG, "getView");
}
if (mResume) {
mGetViewCount++;
}
if (mGetViewCount >= MAX_GET_VIEW_CNT_BY_FLUTTER_RESIZE - 1) {
mMapView.onResume();
mResume = false;
mGetViewCount = 0;
}
return mMapView;
}
@Override
public void onFlutterViewAttached(@NonNull View flutterView) {
if (Env.DEBUG) {
Log.d(TAG, "onFlutterViewAttached");
}
if (null != mMapView) {
mMapView.onResume();
}
}
@Override
public void onFlutterViewDetached() {
if (Env.DEBUG) {
Log.d(TAG, "onFlutterViewDetached");
}
if (null != mMapView) {
mMapView.onPause();
}
}
@Override
public void dispose() {
if (Env.DEBUG) {
Log.d(TAG, "dispose");
}
LocalBroadcastManager.getInstance(mContext).unregisterReceiver(mReceiver);
BMapHandlerFactory.getInstance(null).clean();
OverlayHandlerFactory.getInstance(null).clean();
if (null != mMapView) {
mMapView.onDestroy();
}
}
public void setResumeState(boolean resume) {
mResume = true;
}
public MapView getMapView() {
return mMapView;
}
}
@@ -0,0 +1,152 @@
package com.baidu.flutter_bmfmap.map;
import static com.baidu.flutter_bmfmap.utils.Constants.MAX_GET_VIEW_CNT_BY_FLUTTER_RESIZE;
import java.util.Map;
import com.baidu.flutter_bmfmap.BMFHandlerHelper;
import com.baidu.flutter_bmfmap.utils.Constants;
import com.baidu.flutter_bmfmap.utils.Env;
import com.baidu.mapapi.map.TextureMapView;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.EventChannel;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.platform.PlatformView;
public class FlutterTextureMapView extends FlutterBaseMapView implements PlatformView {
private static final String TAG = "FlutterMapView";
private TextureMapView mTextureMapView;
private Context mContext;
private BinaryMessenger mMessager;
private BMFHandlerHelper mBMFHandlerHelper;
private MethodChannel mMethodChannel;
private EventChannel mEventChannel;
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (Constants.sConfigChangedAction.equals(action) && !mResume) {
mResume = true;
}
}
};
public FlutterTextureMapView(Context context,
BinaryMessenger messenger,
int viewId,
Object args,
String viewType) {
Log.d(TAG, "FlutterMapView");
mContext = context;
mMessager = messenger;
mViewType = viewType;
init(viewId, args);
}
protected void init(int viewId, Object args) {
if (Env.DEBUG) {
Log.d(TAG, "init");
}
mTextureMapView = new TextureMapView(mContext);
FlutterCommonMapView flutterCommonMapView =
new TextureMapViewWrapper(mTextureMapView, mViewType);
initMapView(args, flutterCommonMapView);
mMethodChannel = new MethodChannel(mMessager,
Constants.VIEW_METHOD_CHANNEL_PREFIX + (char) (viewId + 97));
mEventChannel = new EventChannel(mMessager,
Constants.VIEW_EVENT_CHANNEL_PREFIX + (char) (viewId + 97));
mBMFHandlerHelper =
new BMFHandlerHelper(mContext, flutterCommonMapView, mMethodChannel, mEventChannel);
new MapListener(new TextureMapViewWrapper(mTextureMapView, mViewType), mMethodChannel);
IntentFilter intentFilter = new IntentFilter(Constants.sConfigChangedAction);
LocalBroadcastManager.getInstance(mContext).registerReceiver(mReceiver, intentFilter);
if (Env.DEBUG) {
Log.d(TAG, "init success");
}
}
protected void initMapView(Object args, FlutterCommonMapView flutterCommonMapView) {
if (null == mContext) {
return;
}
Map<String, Object> mapOptionsMap = (Map<String, Object>) args;
if (null == mapOptionsMap) {
return;
}
initMapStatus(mapOptionsMap, flutterCommonMapView);
}
@Override
public View getView() {
if (Env.DEBUG) {
Log.d(TAG, "getView");
}
if (mResume) {
mGetViewCount++;
}
if (mGetViewCount >= MAX_GET_VIEW_CNT_BY_FLUTTER_RESIZE - 1) {
mTextureMapView.onResume();
mResume = false;
mGetViewCount = 0;
}
return mTextureMapView;
}
@Override
public void onFlutterViewAttached(@NonNull View flutterView) {
if (null != mTextureMapView) {
mTextureMapView.onResume();
}
}
@Override
public void onFlutterViewDetached() {
if (null != mTextureMapView) {
mTextureMapView.onPause();
}
}
@Override
public void dispose() {
if (Env.DEBUG) {
Log.d(TAG, "dispose");
}
LocalBroadcastManager.getInstance(mContext).unregisterReceiver(mReceiver);
if (null != mTextureMapView) {
mTextureMapView.onDestroy();
}
}
}
@@ -0,0 +1,696 @@
package com.baidu.flutter_bmfmap.map;
import android.graphics.Point;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import com.baidu.flutter_bmfmap.utils.Constants;
import com.baidu.flutter_bmfmap.utils.Env;
import android.os.Message;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapBaseIndoorMapInfo;
import com.baidu.mapapi.map.MapPoi;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.Polyline;
import com.baidu.mapapi.model.LatLng;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.os.Handler;
import javax.microedition.khronos.opengles.GL10;
import io.flutter.plugin.common.MethodChannel;
import com.baidu.flutter_bmfmap.utils.ThreadPoolUtil;
import com.baidu.mapapi.model.LatLngBounds;
@SuppressWarnings("unchecked")
public class MapListener implements BaiduMap.OnMapClickListener ,BaiduMap.OnMapLoadedCallback,
BaiduMap.OnMapStatusChangeListener ,BaiduMap.OnMapRenderCallback,BaiduMap.OnMapDrawFrameCallback,
BaiduMap.OnBaseIndoorMapListener ,BaiduMap.OnMarkerClickListener,BaiduMap.OnPolylineClickListener,
BaiduMap.OnMapDoubleClickListener,BaiduMap.OnMapLongClickListener,BaiduMap.OnMarkerDragListener,
BaiduMap.OnMapRenderValidDataListener,BaiduMap.OnMyLocationClickListener {
private static final int DRAW_FRAME_MESSAGE = 0;
private static final String TAG = "MapListener";
private BaiduMap mBaiduMap;
private MethodChannel mMethodChannel;
private int mReason;
private HashMap<String, HashMap> mStatusMap;
private final Handler mHandler = new Handler(){
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == DRAW_FRAME_MESSAGE) {
if (null != mStatusMap){
mMethodChannel.invokeMethod(
Constants.MethodProtocol.MapStateProtocol.sMapOnDrawMapFrameCallback,mStatusMap);
}
}
}
};
public MapListener(FlutterCommonMapView mapView, MethodChannel methodChannel) {
this.mMethodChannel = methodChannel;
if (null == mapView) {
return;
}
mBaiduMap = mapView.getBaiduMap();
initListener();
}
private void initListener() {
if (null == mBaiduMap) {
return;
}
mBaiduMap.setOnMapClickListener(this);
mBaiduMap.setOnMapLoadedCallback(this);
mBaiduMap.setOnMapStatusChangeListener(this);
mBaiduMap.setOnMapDrawFrameCallback(this);
mBaiduMap.setOnMapRenderCallbadk(this);
mBaiduMap.setOnBaseIndoorMapListener(this);
mBaiduMap.setOnMarkerClickListener(this);
mBaiduMap.setOnPolylineClickListener(this);
mBaiduMap.setOnMapDoubleClickListener(this);
mBaiduMap.setOnMapLongClickListener(this);
mBaiduMap.setOnMarkerDragListener(this);
mBaiduMap.setOnMapRenderValidDataListener(this);
mBaiduMap.setOnMyLocationClickListener(this);
}
@Override
public void onMapClick(LatLng latLng) {
if (null == latLng || mMethodChannel == null) {
return;
}
HashMap<String, HashMap> coordinateMap = new HashMap<>();
HashMap<String, Double> coord = new HashMap<>();
coord.put("latitude",latLng.latitude);
coord.put("longitude",latLng.longitude);
coordinateMap.put("coord",coord);
mMethodChannel.invokeMethod(Constants.MethodProtocol.MapStateProtocol.sMapOnClickedMapBlankCallback,coordinateMap);
}
@Override
public void onMapPoiClick(MapPoi mapPoi) {
if (null == mapPoi || mMethodChannel == null) {
return;
}
HashMap<String, Double> pt = new HashMap<>();
LatLng position = mapPoi.getPosition();
if (null != position) {
pt.put("latitude",mapPoi.getPosition().latitude);
pt.put("longitude",mapPoi.getPosition().longitude);
}
HashMap<String, HashMap> poiMap = new HashMap<>();
HashMap poi = new HashMap();
poi.put("text",mapPoi.getName());
poi.put("uid",mapPoi.getUid());
poi.put("pt",pt);
poiMap.put("poi",poi);
mMethodChannel.invokeMethod(Constants.MethodProtocol.MapStateProtocol.sMapOnClickedMapPoiCallback,poiMap);
}
@Override
public void onMapLoaded() {
mMethodChannel.invokeMethod(Constants.MethodProtocol.MapStateProtocol.sMapDidLoadCallback,"");
}
@Override
public void onMapStatusChangeStart(MapStatus mapStatus) {
if (null == mapStatus || mMethodChannel == null) {
return;
}
HashMap<String, Double> targetScreenMap = new HashMap<>();
Point targetScreen = mapStatus.targetScreen;
if (null == targetScreen) {
return;
}
targetScreenMap.put("x", (double) targetScreen.x);
targetScreenMap.put("y", (double) targetScreen.y);
HashMap<String, Double> targetMap = new HashMap<>();
LatLng latLng = mapStatus.target;
if (null == latLng){
return;
}
targetMap.put("latitude", latLng.latitude);
targetMap.put("longitude", latLng.longitude);
LatLngBounds bound = mapStatus.bound;
if (null == bound) {
return;
}
HashMap latLngBoundMap = latLngBounds(bound);
if (null == latLngBoundMap) {
return;
}
HashMap statusMap = new HashMap<>();
HashMap status = new HashMap();
status.put("fLevel",((double)mapStatus.zoom));
double rotate = mapStatus.rotate;
if (rotate > 180) {
rotate = rotate - 360;
}
status.put("fRotation", rotate);
status.put("fOverlooking",((double) mapStatus.overlook));
status.put("targetScreenPt",targetScreenMap);
status.put("targetGeoPt",targetMap);
status.put("visibleMapBounds",latLngBoundMap);
statusMap.put("mapStatus",status);
mMethodChannel.invokeMethod(Constants.MethodProtocol.MapStateProtocol.sMapRegionWillChangeCallback,statusMap);
}
@Override
public void onMapStatusChangeStart(MapStatus mapStatus, int reason) {
if (null == mapStatus || mMethodChannel == null) {
return;
}
mReason = reason;
HashMap<String, Double> targetScreenMap = new HashMap<>();
Point targetScreen = mapStatus.targetScreen;
if (null == targetScreen) {
return;
}
targetScreenMap.put("x", (double) targetScreen.x);
targetScreenMap.put("y", (double) targetScreen.y);
HashMap<String, Double> targetMap = new HashMap<>();
LatLng latLng = mapStatus.target;
if (null == latLng){
return;
}
targetMap.put("latitude", latLng.latitude);
targetMap.put("longitude", latLng.longitude);
LatLngBounds bound = mapStatus.bound;
if (null == bound) {
return;
}
HashMap latLngBoundMap = latLngBounds(bound);
if (null == latLngBoundMap) {
return;
}
HashMap statusMap = new HashMap<>();
HashMap status = new HashMap();
status.put("fLevel",((double)mapStatus.zoom));
double rotate = mapStatus.rotate;
if (rotate > 180) {
rotate = rotate - 360;
}
status.put("fRotation", rotate);
status.put("fOverlooking",((double) mapStatus.overlook));
status.put("targetScreenPt",targetScreenMap);
status.put("targetGeoPt",targetMap);
status.put("visibleMapBounds",latLngBoundMap);
statusMap.put("mapStatus",status);
statusMap.put("reason",mReason);
mMethodChannel.invokeMethod(Constants.MethodProtocol.MapStateProtocol.
sMapRegionWillChangeWithReasonCallback,statusMap);
}
@Override
public void onMapStatusChange(MapStatus mapStatus) {
if (null == mapStatus || mMethodChannel == null) {
return;
}
HashMap<String, Double> targetScreenMap = new HashMap<>();
Point targetScreen = mapStatus.targetScreen;
if (null == targetScreen) {
return;
}
targetScreenMap.put("x", (double) targetScreen.x);
targetScreenMap.put("y", (double) targetScreen.y);
HashMap<String, Double> targetMap = new HashMap<>();
LatLng latLng = mapStatus.target;
if (null == latLng){
return;
}
targetMap.put("latitude", latLng.latitude);
targetMap.put("longitude", latLng.longitude);
LatLngBounds bound = mapStatus.bound;
if (null == bound) {
return;
}
HashMap latLngBoundMap = latLngBounds(bound);
if (null == latLngBoundMap) {
return;
}
HashMap statusMap = new HashMap<>();
HashMap status = new HashMap();
status.put("fLevel",((double)mapStatus.zoom));
double rotate = mapStatus.rotate;
if (rotate > 180) {
rotate = rotate - 360;
}
status.put("fRotation", rotate);
status.put("fOverlooking",((double) mapStatus.overlook));
status.put("targetScreenPt",targetScreenMap);
status.put("targetGeoPt",targetMap);
status.put("visibleMapBounds",latLngBoundMap);
statusMap.put("mapStatus",status);
mMethodChannel.invokeMethod(
Constants.MethodProtocol.MapStateProtocol.sMapRegionDidChangeCallback,statusMap);
}
@Override
public void onMapStatusChangeFinish(MapStatus mapStatus) {
if (null == mapStatus || mMethodChannel == null) {
return;
}
HashMap<String, Double> targetScreenMap = new HashMap<>();
Point targetScreen = mapStatus.targetScreen;
if (null == targetScreen) {
return;
}
targetScreenMap.put("x", (double) targetScreen.x);
targetScreenMap.put("y", (double) targetScreen.y);
HashMap<String, Double> targetMap = new HashMap<>();
LatLng latLng = mapStatus.target;
if (null == latLng){
return;
}
targetMap.put("latitude", latLng.latitude);
targetMap.put("longitude", latLng.longitude);
LatLngBounds bound = mapStatus.bound;
if (null == bound) {
return;
}
HashMap latLngBoundMap = latLngBounds(bound);
if (null == latLngBoundMap) {
return;
}
HashMap statusMap = new HashMap<>();
HashMap status = new HashMap();
status.put("fLevel",((double)mapStatus.zoom));
double rotate = mapStatus.rotate;
if (rotate > 180) {
rotate = rotate - 360;
}
status.put("fRotation", rotate);
status.put("fOverlooking",((double) mapStatus.overlook));
status.put("targetScreenPt",targetScreenMap);
status.put("targetGeoPt",targetMap);
status.put("visibleMapBounds",latLngBoundMap);
statusMap.put("mapStatus",status);
statusMap.put("reason",mReason);
mMethodChannel.invokeMethod(Constants.MethodProtocol.MapStateProtocol.sMapRegionDidChangeWithReasonCallback,statusMap);
mMethodChannel.invokeMethod(Constants.MethodProtocol.MapStateProtocol.sMapStatusDidChangedCallback,"");
}
@Override
public void onMapRenderFinished() {
HashMap hashMap = new HashMap();
hashMap.put("success",true);
mMethodChannel.invokeMethod(Constants.MethodProtocol.MapStateProtocol.sMapDidFinishRenderCallback,hashMap);
}
@Override
public void onMapDrawFrame(GL10 gl10, MapStatus mapStatus) {
}
@Override
public void onMapDrawFrame(MapStatus mapStatus) {
if (null == mapStatus || mMethodChannel == null) {
return;
}
HashMap<String, Double> targetScreenMap = new HashMap<>();
Point targetScreen = mapStatus.targetScreen;
if (null == targetScreen) {
return;
}
targetScreenMap.put("x", (double) targetScreen.x);
targetScreenMap.put("y", (double) targetScreen.y);
HashMap<String, Double> targetMap = new HashMap<>();
LatLng latLng = mapStatus.target;
if (null == latLng){
return;
}
targetMap.put("latitude", latLng.latitude);
targetMap.put("longitude", latLng.longitude);
LatLngBounds bound = mapStatus.bound;
if (null == bound) {
return;
}
HashMap latLngBoundMap = latLngBounds(bound);
if (null == latLngBoundMap) {
return;
}
mStatusMap = new HashMap<>();
HashMap status = new HashMap();
status.put("fLevel",((double)mapStatus.zoom));
double rotate = mapStatus.rotate;
if (rotate > 180) {
rotate = rotate - 360;
}
status.put("fRotation", rotate);
status.put("fOverlooking",((double) mapStatus.overlook));
status.put("targetScreenPt",targetScreenMap);
status.put("targetGeoPt",targetMap);
status.put("visibleMapBounds",latLngBoundMap);
mStatusMap.put("mapStatus",status);
ThreadPoolUtil.getInstance().execute(new Runnable() {
@Override
public void run() {
Message msg = Message.obtain();
msg.arg1 = DRAW_FRAME_MESSAGE;
mHandler.sendMessage(msg);
}
});
}
@Override
public void onBaseIndoorMapMode(boolean isIndoorMap, MapBaseIndoorMapInfo mapBaseIndoorMapInfo) {
if (mMethodChannel == null) {
return;
}
HashMap indoorHashMap = new HashMap();
indoorHashMap.put("flag",isIndoorMap);
HashMap indoorMap = new HashMap();
if (isIndoorMap) {
if (null == mapBaseIndoorMapInfo) {
return;
}
String curFloor = mapBaseIndoorMapInfo.getCurFloor();
String id = mapBaseIndoorMapInfo.getID();
ArrayList<String> floors = mapBaseIndoorMapInfo.getFloors();
indoorMap.put("strFloor", curFloor);
indoorMap.put("strID", id);
indoorMap.put("listStrFloors", floors);
}
indoorHashMap.put("info",indoorMap);
mMethodChannel.invokeMethod(Constants.MethodProtocol.MapStateProtocol.sMapInOrOutBaseIndoorMapCallback
,indoorHashMap);
}
@Override
public boolean onMarkerClick(Marker marker) {
if(Env.DEBUG){
Log.d(TAG, "onMarkerClick");
}
if(null == mMethodChannel){
return false;
}
Bundle bundle = marker.getExtraInfo();
if(null == bundle){
if(Env.DEBUG){
Log.d(TAG, "bundle is null");
}
return false;
}
String id = bundle.getString("id");
if(TextUtils.isEmpty(id)){
if(Env.DEBUG){
Log.d(TAG, "marker id is null ");
}
return false;
}
Map<String, Object> clickMap = new HashMap<>();
clickMap.put("id", id);
mMethodChannel.invokeMethod(Constants.MethodProtocol.MarkerProtocol.sMapClickedmarkedMethod, clickMap);
return true;
}
@Override
public boolean onPolylineClick(Polyline polyline) {
Log.d("polyline", "polyline click");
HashMap hashMap = polylineClick(polyline);
HashMap<String, Object> polyLineMap = new HashMap<>();
polyLineMap.put("polyline", hashMap);
mMethodChannel.invokeMethod(Constants.MethodProtocol.PolylineProtocol.sMapOnClickedOverlayCallback, polyLineMap);
return true;
}
private HashMap polylineClick(Polyline polyline) {
if (null == polyline) {
return null;
}
Bundle bundle = polyline.getExtraInfo();
String id = bundle.getString("id");
HashMap polylineMap = new HashMap();
List<LatLng> points = polyline.getPoints();
List<Object> latlngLists = new ArrayList<>();
if (null != points){
for (int i = 0; i < points.size(); i++) {
HashMap<String, Double> latlngHashMap = new HashMap<>();
latlngHashMap.put("latitude",points.get(i).latitude);
latlngHashMap.put("longitude",points.get(i).longitude);
latlngLists.add(latlngHashMap);
}
}
polylineMap.put("id", id);
polylineMap.put("coordinates",latlngLists);
ArrayList<String> colorList = new ArrayList<>();
int[] colors = polyline.getColorList();
if(null != colors){
for(int i = 0; i < colors.length; i++){
colorList.add(Integer.toHexString(colors[i]));
}
}
polylineMap.put("colors", colorList);
polylineMap.put("color", polyline.getColor());
polylineMap.put("lineDashType", polyline.getDottedLineType());
polylineMap.put("lineCapType", 0);
polylineMap.put("lineJoinType", 0);
polylineMap.put("width", polyline.getWidth());
polylineMap.put("zIndex", polyline.getZIndex());
return polylineMap;
}
@Override
public void onMapDoubleClick(LatLng latLng) {
if (null == latLng || mMethodChannel == null) {
return;
}
HashMap<String, HashMap> coordinateMap = new HashMap<>();
HashMap<String, Double> coord = new HashMap<>();
coord.put("latitude",latLng.latitude);
coord.put("longitude",latLng.longitude);
coordinateMap.put("coord",coord);
mMethodChannel.invokeMethod(Constants.MethodProtocol.MapStateProtocol.sMapOnDoubleClickCallback,coordinateMap);
}
@Override
public void onMapLongClick(LatLng latLng) {
if (null == latLng || mMethodChannel == null) {
return;
}
HashMap<String, HashMap> coordinateMap = new HashMap<>();
HashMap<String, Double> coord = new HashMap<>();
coord.put("latitude",latLng.latitude);
coord.put("longitude",latLng.longitude);
coordinateMap.put("coord",coord);
mMethodChannel.invokeMethod(Constants.MethodProtocol.MapStateProtocol.sMapOnLongClickCallback,coordinateMap);
}
@Override
public void onMarkerDrag(Marker marker) {
if(Env.DEBUG){
Log.d(TAG, "onMarkerDrag");
}
if(null == mMethodChannel){
return;
}
Bundle bundle = marker.getExtraInfo();
if(null == bundle){
return;
}
String id = bundle.getString("id");
if(null == mMethodChannel){
return;
}
if(TextUtils.isEmpty(id)){
if(Env.DEBUG){
Log.d(TAG, "id is null");
}
return;
}
if(TextUtils.isEmpty(id)){
if(Env.DEBUG){
Log.d(TAG, "id is null");
}
return;
}
Map<String, Object> dragMap = new HashMap<>();
dragMap.put("id", id);
Map<String, Object> extraInfoMap = new HashMap<>();
extraInfoMap.put("state", Constants.MethodProtocol.MarkerProtocol.MarkerDragState.sDragging);
dragMap.put("extra", extraInfoMap);
mMethodChannel.invokeMethod(Constants.MethodProtocol.MarkerProtocol.sMapDragMarkerMethod, dragMap);
}
@Override
public void onMarkerDragEnd(Marker marker) {
if(Env.DEBUG){
Log.d(TAG, "onMarkerDrag");
}
if(null == mMethodChannel){
return;
}
Bundle bundle = marker.getExtraInfo();
if(null == bundle){
return;
}
String id = bundle.getString("id");
if(null == mMethodChannel){
return;
}
if(TextUtils.isEmpty(id)){
if(Env.DEBUG){
Log.d(TAG, "id is null");
}
return;
}
LatLng center = marker.getPosition();
if(null == center){
return;
}
Map<String, Object> dragMap = new HashMap<>();
dragMap.put("id", id);
Map<String, Double> centerMap = new HashMap<>();
centerMap.put("latitude", center.latitude);
centerMap.put("longitude", center.longitude);
Map<String, Object> extraInfoMap = new HashMap<>();
extraInfoMap.put("center", centerMap);
extraInfoMap.put("state", Constants.MethodProtocol.MarkerProtocol.MarkerDragState.sDragEnd);
dragMap.put("extra", extraInfoMap);
mMethodChannel.invokeMethod(Constants.MethodProtocol.MarkerProtocol.sMapDragMarkerMethod, dragMap);
}
@Override
public void onMarkerDragStart(Marker marker) {
if(Env.DEBUG){
Log.d(TAG, "onMarkerDrag");
}
Bundle bundle = marker.getExtraInfo();
if(null == bundle){
return;
}
String id = bundle.getString("id");
if(null == mMethodChannel){
return;
}
if(TextUtils.isEmpty(id)){
if(Env.DEBUG){
Log.d(TAG, "id is null");
}
return;
}
LatLng center = marker.getPosition();
if(null == center){
return;
}
Map<String, Object> dragMap = new HashMap<>();
dragMap.put("id", id);
Map<String, Double> centerMap = new HashMap<>();
centerMap.put("latitude", center.latitude);
centerMap.put("longitude", center.longitude);
Map<String, Object> extraInfoMap = new HashMap<>();
extraInfoMap.put("center", centerMap);
extraInfoMap.put("state", Constants.MethodProtocol.MarkerProtocol.MarkerDragState.sDragStart);
dragMap.put("extra", extraInfoMap);
mMethodChannel.invokeMethod(Constants.MethodProtocol.MarkerProtocol.sMapDragMarkerMethod, dragMap);
}
@Override
public void onMapRenderValidData(boolean isValid, int errorCode, String errorMessage) {
HashMap hashMap = new HashMap();
hashMap.put("isValid",isValid);
hashMap.put("errorCode",errorCode);
hashMap.put("errorMessage",errorMessage);
mMethodChannel.invokeMethod(Constants.MethodProtocol.MapStateProtocol.sMapRenderValidDataCallback,hashMap);
}
@Override
public boolean onMyLocationClick() {
return false;
}
private HashMap latLngBounds(LatLngBounds latLngBounds) {
if (null == latLngBounds) {
return null;
}
// 该地理范围东北坐标
LatLng northeast = latLngBounds.northeast;
// 该地理范围西南坐标
LatLng southwest = latLngBounds.southwest;
HashMap boundsMap = new HashMap();
HashMap northeastMap = new HashMap<String,Double>();
if (null == northeast){
return null;
}
northeastMap.put("latitude", northeast.latitude);
northeastMap.put("longitude",northeast.longitude);
HashMap southwestMap = new HashMap<String,Double>();
if (null == southwest) {
return null;
}
southwestMap.put("latitude",southwest.latitude);
southwestMap.put("longitude", southwest.longitude);
boundsMap.put("northeast",northeastMap);
boundsMap.put("southwest",southwestMap);
return boundsMap;
}
}
@@ -0,0 +1,363 @@
package com.baidu.flutter_bmfmap.map;
import java.util.Map;
import com.baidu.flutter_bmfmap.map.mapHandler.BMFMapStatus;
import com.baidu.flutter_bmfmap.utils.Constants;
import com.baidu.flutter_bmfmap.utils.Env;
import com.baidu.flutter_bmfmap.utils.converter.FlutterDataConveter;
import com.baidu.flutter_bmfmap.utils.converter.TypeConverter;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.LogoPosition;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.TextureMapView;
import com.baidu.mapapi.map.UiSettings;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.model.LatLngBounds;
import android.graphics.Point;
import android.text.TextUtils;
/**
* 地图状态更新
*/
public class MapStateUpdateImp {
private static MapStateUpdateImp sInstance = null;
private String mViewType;
private FlutterCommonMapView mFlutterCommonMapView;
private BaiduMap mBaiduMap;
private UiSettings mUiSettings;
public static MapStateUpdateImp getInstance() {
if (null == sInstance) {
sInstance = new MapStateUpdateImp();
}
return sInstance;
}
public MapStateUpdateImp setCommView(FlutterCommonMapView commonMapView) {
if(null == commonMapView){
return sInstance;
}
if( mFlutterCommonMapView == commonMapView){
return sInstance;
}
mFlutterCommonMapView = commonMapView;
mBaiduMap = commonMapView.getBaiduMap();
mViewType = mFlutterCommonMapView.getViewType();
mBaiduMap = mFlutterCommonMapView.getBaiduMap();
mUiSettings = mBaiduMap.getUiSettings();
return sInstance;
}
public boolean updateMapState(Map<String, Object> mapOptionsMap) {
if (null == mapOptionsMap) {
return false;
}
if (null == mFlutterCommonMapView ||
null == mBaiduMap ||
null == mUiSettings ||
TextUtils.isEmpty(mViewType)) {
return false;
}
// 设置地图类型
Integer mapType = new TypeConverter<Integer>().getValue(mapOptionsMap, "mapType");
if (null != mapType) {
setMapType(mapType);
}
// 设置指南针显示位置
Map<String, Object> compassPosMap = new TypeConverter<Map<String, Object>>().getValue(mapOptionsMap, "compassPosition");
Point compassPos = FlutterDataConveter.mapToPoint(compassPosMap);
if (null != compassPos) {
mBaiduMap.setCompassPosition(compassPos);
}
// 设置地图中心点
Map<String, Object> centerMap = new TypeConverter<Map<String, Object>>().getValue(mapOptionsMap, "center");
LatLng center = FlutterDataConveter.mapToLatlng(centerMap);
if (null != center) {
MapStatusUpdate mapStatusUpdate = MapStatusUpdateFactory.newLatLng(center);
mBaiduMap.setMapStatus(mapStatusUpdate);
}
// 设置地图缩放级别
Integer zoomLevel = new TypeConverter<Integer>().getValue(mapOptionsMap, "zoomLevel");
if (null != zoomLevel) {
MapStatusUpdate mapStatusUpdate = MapStatusUpdateFactory.zoomTo(zoomLevel.floatValue());
mBaiduMap.setMapStatus(mapStatusUpdate);
}
// 设置地图最大、最小缩放级别
Integer minZoomLevel = new TypeConverter<Integer>().getValue(mapOptionsMap, "minZoomLevel");
Integer maxZoomLevel = new TypeConverter<Integer>().getValue(mapOptionsMap, "maxZoomLevel");
if (null != minZoomLevel && null != maxZoomLevel) {
mBaiduMap.setMaxAndMinZoomLevel(maxZoomLevel.floatValue(), minZoomLevel.floatValue());
} else if (null == minZoomLevel && null != maxZoomLevel ) {
mBaiduMap.setMaxAndMinZoomLevel(maxZoomLevel.floatValue(),mBaiduMap.getMinZoomLevel());
} else if (null != minZoomLevel && null == maxZoomLevel) {
mBaiduMap.setMaxAndMinZoomLevel(mBaiduMap.getMaxZoomLevel(), minZoomLevel.floatValue());
}
// 设置地图旋转角度
Double rotation = new TypeConverter<Double>().getValue(mapOptionsMap, "rotation");
if (null != rotation) {
setRotation(rotation.floatValue());
}
// 设置地图俯仰角度
if (mapOptionsMap.containsKey("overlooking")) {
Double overlooking = (Double) mapOptionsMap.get("overlooking");
if (overlooking != null) {
MapStatus build = new MapStatus.Builder().overlook(overlooking.floatValue()).build();
MapStatusUpdate mapStatusUpdate = MapStatusUpdateFactory.newMapStatus(build);
mBaiduMap.setMapStatus(mapStatusUpdate);
}
}
// 是否显示3d建筑物
Boolean buildingsEnabled = new TypeConverter<Boolean>().getValue(mapOptionsMap, "buildingsEnabled");
if (null != buildingsEnabled) {
mBaiduMap.setBuildingsEnabled(buildingsEnabled);
}
// 设置是否显示poi信息
Boolean showMapPoi = new TypeConverter<Boolean>().getValue(mapOptionsMap, "showMapPoi");
if (null != showMapPoi) {
mBaiduMap.showMapPoi(showMapPoi);
}
// 设置是否显示路况信息
Boolean trafficEnabled = new TypeConverter<Boolean>().getValue(mapOptionsMap, "trafficEnabled");
if (null != trafficEnabled) {
mBaiduMap.setTrafficEnabled(trafficEnabled);
}
// 限制地图的显示范围
if (mapOptionsMap.containsKey("limitMapBounds")) {
Map<String, Object> limitMapRegion = (Map<String, Object>) mapOptionsMap.get("limitMapBounds");
if (null != limitMapRegion) {
setMapLimits(limitMapRegion);
}
}
// 设置是否显示百度自有热力图
Boolean baiduHeatMapEnabled = new TypeConverter<Boolean>().getValue(mapOptionsMap, "baiduHeatMapEnabled");
if (null != baiduHeatMapEnabled) {
mBaiduMap.setBaiduHeatMapEnabled(baiduHeatMapEnabled);
}
// 设置是否启用手势
Boolean gesturesEnabled = new TypeConverter<Boolean>().getValue(mapOptionsMap, "gesturesEnabled");
if (null != gesturesEnabled) {
mUiSettings.setAllGesturesEnabled(gesturesEnabled);
}
// 设置是否开启放大缩小
Boolean zoomEnabled = new TypeConverter<Boolean>().getValue(mapOptionsMap, "zoomEnabled");
if (null != zoomEnabled) {
mUiSettings.setZoomGesturesEnabled(zoomEnabled);
}
// 设置地图是否可滑动
Boolean scrollEnabled = new TypeConverter<Boolean>().getValue(mapOptionsMap, "scrollEnabled");
if (null != scrollEnabled) {
mUiSettings.setScrollGesturesEnabled(scrollEnabled);
}
// 设置是否开启俯仰角
Boolean overlookEnabled = new TypeConverter<Boolean>().getValue(mapOptionsMap, "overlookEnabled");
if (null != overlookEnabled) {
mUiSettings.setOverlookingGesturesEnabled(overlookEnabled);
}
// 设置是否开启旋转角
Boolean rotateEnabled = new TypeConverter<Boolean>().getValue(mapOptionsMap, "rotateEnabled");
if (null != rotateEnabled) {
mUiSettings.setRotateGesturesEnabled(rotateEnabled);
}
// 设置比例尺是否显示
Boolean showMapScaleBar = new TypeConverter<Boolean>().getValue(mapOptionsMap, "showMapScaleBar");
if (null != showMapScaleBar) {
showScaleControl(showMapScaleBar);
}
// 设置比例尺显示位置
Map<String, Object> mapScaleBarPosMap = new TypeConverter<Map<String, Object>>().getValue(mapOptionsMap, "mapScaleBarPosition");
Point mapScaleBarPos = FlutterDataConveter.mapToPoint(mapScaleBarPosMap);
if (null != mapScaleBarPos) {
setScaleControlPosition(mapScaleBarPos);
}
// 设置百度logo显示位置
Integer logoPosition = new TypeConverter<Integer>().getValue(mapOptionsMap, "logoPosition");
if (null != logoPosition
&& logoPosition >= LogoPosition.logoPostionleftBottom.ordinal()
&& logoPosition <= LogoPosition.logoPostionRightTop.ordinal()) {
setLogoPosition(LogoPosition.values()[logoPosition.intValue()]);
}
// 设置地图padding
Map<String, Double> mapPadding = new TypeConverter<Map<String, Double>>().getValue(mapOptionsMap, "mapPadding");
if (null != mapPadding) {
if (mapPadding.containsKey("top") && mapPadding.containsKey("left")
&& mapPadding.containsKey("bottom") && mapPadding.containsKey("right")) {
Double top = mapPadding.get("top");
Double left = mapPadding.get("left");
Double bottom = mapPadding.get("bottom");
Double right = mapPadding.get("right");
if (top != null && left != null && bottom != null && right != null) {
int iTop = top.intValue();
int iLeft = left.intValue();
int iBottom = bottom.intValue();
int iRight = right.intValue();
mBaiduMap.setViewPadding(iLeft, iTop, iRight, iBottom);
}
}
}
// 设置双击屏幕放大地图时,是否改变地图中心点为当前点击点
Boolean changeCenterWithDoubleTouchPointEnabled = new TypeConverter<Boolean>().getValue(mapOptionsMap, "changeCenterWithDoubleTouchPointEnabled");
if (null != changeCenterWithDoubleTouchPointEnabled) {
// 这个值,sdk好像取的是反的,这个设个反值
mUiSettings.setEnlargeCenterWithDoubleClickEnable(!changeCenterWithDoubleTouchPointEnabled);
}
// 设置是否开启室内图
Boolean baseIndoorMapEnabled = new TypeConverter<Boolean>().getValue(mapOptionsMap, "baseIndoorMapEnabled");
if (null != baseIndoorMapEnabled) {
mBaiduMap.setIndoorEnable(baseIndoorMapEnabled);
BMFMapStatus.getsInstance().setBaseIndoorEnable(baseIndoorMapEnabled);
}
// 设置是否开启室内图poi
Boolean showIndoorMapPoi = new TypeConverter<Boolean>().getValue(mapOptionsMap, "showIndoorMapPoi");
if (null != showIndoorMapPoi) {
mBaiduMap.showMapIndoorPoi(showIndoorMapPoi);
BMFMapStatus.getsInstance().setIndoorMapPoiEnable(showIndoorMapPoi);
}
// 设置地图可视区域
Map<String, Object> visibleMapBounds = new TypeConverter<Map<String, Object>>().getValue(mapOptionsMap, "visibleMapBounds");
LatLngBounds latLngBounds = FlutterDataConveter.mapToLatlngBounds(visibleMapBounds);
if (null != latLngBounds) {
MapStatusUpdate mapStatusUpdate = MapStatusUpdateFactory.newLatLngBounds(latLngBounds);
mBaiduMap.setMapStatus(mapStatusUpdate);
}
return true;
}
private void setMapType(Integer mapType) {
switch (mapType) {
case Env.MAP_TYPE_NONE:
mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NONE);
break;
case Env.MAP_TYPE_NORMAL:
mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
break;
case Env.MAP_TYPE_SATELLITE:
mBaiduMap.setMapType(BaiduMap.MAP_TYPE_SATELLITE);
break;
default:
break;
}
}
private void setRotation(float rotation) {
if (rotation < 0) {
rotation = rotation + 360;
}
MapStatus mapStatus = new MapStatus.Builder().rotate(rotation).build();
MapStatusUpdate mapStatusUpdate = MapStatusUpdateFactory.newMapStatus(mapStatus);
mBaiduMap.setMapStatus(mapStatusUpdate);
}
private void showScaleControl(boolean showScaleControl) {
switch(mViewType){
case Constants.ViewType.sMapView:
MapView mapView = mFlutterCommonMapView.getMapView();
if (null != mapView) {
mapView.showScaleControl(showScaleControl);
}
break;
case Constants.ViewType.sTextureMapView:
TextureMapView textureMapView = mFlutterCommonMapView.getTextureMapView();
if (null != textureMapView) {
textureMapView.showScaleControl(showScaleControl);
}
break;
default:
break;
}
}
private void setScaleControlPosition(Point mapScaleBarPos) {
switch (mViewType) {
case Constants.ViewType.sMapView:
MapView mapView = mFlutterCommonMapView.getMapView();
if (null != mapView) {
mapView.setScaleControlPosition(mapScaleBarPos);
}
break;
case Constants.ViewType.sTextureMapView:
TextureMapView textureMapView = mFlutterCommonMapView.getTextureMapView();
if (null != textureMapView) {
textureMapView.setScaleControlPosition(mapScaleBarPos);
}
break;
default:
break;
}
}
private void setLogoPosition(LogoPosition logoPos) {
switch (mViewType) {
case Constants.ViewType.sMapView:
MapView mapView = mFlutterCommonMapView.getMapView();
if(null != mapView){
mapView.setLogoPosition(logoPos);
}
break;
case Constants.ViewType.sTextureMapView:
TextureMapView textureMapView = mFlutterCommonMapView.getTextureMapView();
if (null != textureMapView) {
textureMapView.setLogoPosition(logoPos);
}
break;
default:
break;
}
}
/**
* 限制地图的显示范围
*/
private void setMapLimits(Map<String, Object> limitMapBounds) {
LatLngBounds latLngBounds = FlutterDataConveter.mapToLatlngBounds(limitMapBounds);
if (null == latLngBounds) {
return;
}
mBaiduMap.setMapStatusLimits(latLngBounds);
}
}
@@ -0,0 +1,27 @@
package com.baidu.flutter_bmfmap.map;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.TextureMapView;
public class MapViewWrapper extends FlutterCommonMapView {
FlutterMapView mFlutterMapView;
public MapViewWrapper(FlutterMapView mapView, String viewType) {
mFlutterMapView = mapView;
mViewType = viewType;
}
@Override
public MapView getMapView() {
return mFlutterMapView.getMapView();
}
public FlutterMapView getFlutterMapView() {
return mFlutterMapView;
}
@Override
public TextureMapView getTextureMapView() {
return null;
}
}
@@ -0,0 +1,451 @@
package com.baidu.flutter_bmfmap.map;
import android.text.TextUtils;
import com.baidu.flutter_bmfmap.utils.Constants;
import com.baidu.mapapi.map.offline.MKOLSearchRecord;
import com.baidu.mapapi.map.offline.MKOLUpdateElement;
import com.baidu.mapapi.map.offline.MKOfflineMap;
import com.baidu.mapapi.map.offline.MKOfflineMapListener;
import com.baidu.mapapi.model.LatLng;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
/**
* 离线地图handler
*/
public class OfflineHandler implements MethodChannel.MethodCallHandler {
private MKOfflineMap mMKOfflineMap;
private MethodChannel channel;
public void init(BinaryMessenger messenger) {
channel = new MethodChannel(messenger, "flutter_bmfmap/offlineMap");
channel.setMethodCallHandler(this);
}
public void unInit(BinaryMessenger messenger) {
}
@Override
public void onMethodCall(MethodCall call, MethodChannel.Result result) {
if (null == call) {
return;
}
String methodId = call.method;
if (TextUtils.isEmpty(methodId)) {
return;
}
switch (methodId) {
case Constants.MethodProtocol.BMFOfflineMethodId.sMapInitOfflineMethod:
initOfflineMap(result);
break;
case Constants.MethodProtocol.BMFOfflineMethodId.sMapStartOfflineMethod:
statOfflineMap(call,result);
break;
case Constants.MethodProtocol.BMFOfflineMethodId.sMapPauseOfflineMethod:
pauseOfflineMap(call,result);
break;
case Constants.MethodProtocol.BMFOfflineMethodId.sMapRemoveOfflineMethod:
removeOfflineMap(call,result);
break;
case Constants.MethodProtocol.BMFOfflineMethodId.sMapUpdateOfflineMethod:
updateOffline(call,result);
break;
case Constants.MethodProtocol.BMFOfflineMethodId.sMapDestroyOfflineMethod:
destroyOffline(result);
break;
case Constants.MethodProtocol.BMFOfflineMethodId.sMapGetHotCityListMethod:
getHotCityList(result);
break;
case Constants.MethodProtocol.BMFOfflineMethodId.sMapGetOfflineCityListMethod:
getOfflineCityList(result);
break;
case Constants.MethodProtocol.BMFOfflineMethodId.sMapSearchCityMethod:
seachCityList(call, result);
break;
case Constants.MethodProtocol.BMFOfflineMethodId.sMapGetAllUpdateInfoMethod:
getAllUpdateInfo(result);
break;
case Constants.MethodProtocol.BMFOfflineMethodId.sMapGetUpdateInfoMethod:
getUpdateInfo(call,result);
break;
default:
break;
}
}
/**
* 初始化
*/
private void initOfflineMap(MethodChannel.Result result) {
mMKOfflineMap = new MKOfflineMap();
mMKOfflineMap.init(new MKOfflineMapListener() {
@Override
public void onGetOfflineMapState(int type, int state) {
HashMap hashMap = new HashMap();
hashMap.put("type",type);
hashMap.put("state",state);
channel.invokeMethod(Constants.MethodProtocol.BMFOfflineMethodId.sMapOfflineCallBackMethod,hashMap);
}
});
result.success(true);
}
/**
* 销毁离线地图管理模块,不用时调用
*/
private void destroyOffline(MethodChannel.Result result) {
if (null == mMKOfflineMap) {
result.success(false);
return;
}
mMKOfflineMap.destroy();
result.success(true);
}
/**
* 启动更新指定城市ID的离线地图
*/
private void updateOffline(MethodCall call, MethodChannel.Result result) {
Map<String, Object> argument = call.arguments();
if (null == argument || null == mMKOfflineMap) {
result.success(false);
return;
}
if (!argument.containsKey("cityID")) {
result.success(false);
return;
}
Integer cityID = (Integer) argument.get("cityID");
if (null != cityID) {
boolean update = mMKOfflineMap.update(cityID);
result.success(update);
}
}
/**
* 删除指定城市ID的离线地图
*/
private void removeOfflineMap(MethodCall call, MethodChannel.Result result) {
Map<String, Object> argument = call.arguments();
if (null == argument || null == mMKOfflineMap) {
result.success(false);
return;
}
if (!argument.containsKey("cityID")) {
result.success(false);
return;
}
Integer cityID = (Integer) argument.get("cityID");
if (null != cityID) {
boolean remove = mMKOfflineMap.remove(cityID);
result.success(remove);
}
}
/**
* 暂停下载或更新指定城市ID的离线地图
*/
private void pauseOfflineMap(MethodCall call, MethodChannel.Result result) {
Map<String, Object> argument = call.arguments();
if (null == argument || null == mMKOfflineMap) {
result.success(false);
return;
}
if (!argument.containsKey("cityID")) {
result.success(false);
return;
}
Integer cityID = (Integer) argument.get("cityID");
if (null != cityID) {
boolean pause = mMKOfflineMap.pause(cityID);
result.success(pause);
}
}
/**
* 启动下载指定城市ID的离线地图,或在暂停更新某城市后继续更新下载某城市离线地图
*/
private void statOfflineMap(MethodCall call,MethodChannel.Result result) {
Map<String, Object> argument = call.arguments();
if (null == argument || null == mMKOfflineMap) {
result.success(false);
return;
}
if (!argument.containsKey("cityID")) {
result.success(false);
return;
}
Integer cityID = (Integer) argument.get("cityID");
if (null != cityID) {
boolean start = mMKOfflineMap.start(cityID);
result.success(start);
}
}
/**
* 返回指定城市ID离线地图更新信息
*/
private void getUpdateInfo(MethodCall call, MethodChannel.Result result) {
Map<String, Object> argument = call.arguments();
if (null == argument || null == mMKOfflineMap) {
result.success(null);
return;
}
if (!argument.containsKey("cityID")) {
result.success(null);
return;
}
Integer id = (Integer) argument.get("cityID");
MKOLUpdateElement updateInfo = mMKOfflineMap.getUpdateInfo(id);
if (null != id && null != updateInfo) {
Map map = new HashMap();
int cityID = updateInfo.cityID;
int ratio = updateInfo.ratio;
int status = updateInfo.status;
String cityName = updateInfo.cityName;
int size = updateInfo.size;
int serversize = updateInfo.serversize;
int level = updateInfo.level;
boolean update = updateInfo.update;
LatLng latLng = updateInfo.geoPt;
HashMap<String, Double> geoPt = new HashMap<>();
geoPt.put("latitude", latLng.latitude);
geoPt.put("longitude", latLng.longitude);
map.put("cityID", cityID);
map.put("ratio", ratio);
map.put("status", status);
map.put("cityName", cityName);
map.put("geoPt", geoPt);
map.put("size", size);
map.put("serversize", serversize);
map.put("level", level);
map.put("update", update);
result.success(map);
} else {
result.success(null);
return;
}
}
/**
* 返回各城市离线地图更新信息
*/
private void getAllUpdateInfo(MethodChannel.Result result) {
if (null == mMKOfflineMap) {
result.success(null);
return;
}
ArrayList<MKOLUpdateElement> allUpdateInfo = mMKOfflineMap.getAllUpdateInfo();
if (null == allUpdateInfo || allUpdateInfo.size() == 0) {
result.success(null);
return;
}
ArrayList<Map> arrayMap = new ArrayList<>();
HashMap<String, ArrayList> offlineCityMap = new HashMap<>();
for (int i = 0; i < allUpdateInfo.size(); i++) {
Map map = new HashMap();
int cityID = allUpdateInfo.get(i).cityID;
int ratio = allUpdateInfo.get(i).ratio;
String cityName = allUpdateInfo.get(i).cityName;
int size = allUpdateInfo.get(i).size;
int serversize = allUpdateInfo.get(i).serversize;
int level = allUpdateInfo.get(i).level;
boolean update = allUpdateInfo.get(i).update;
LatLng latLng = allUpdateInfo.get(i).geoPt;
HashMap<String, Double> geoPt = new HashMap<>();
geoPt.put("latitude",latLng.latitude);
geoPt.put("longitude",latLng.longitude);
map.put("cityID",cityID);
map.put("ratio",ratio);
map.put("cityName",cityName);
map.put("geoPt",geoPt);
map.put("size",size);
map.put("serversize",serversize);
map.put("level",level);
map.put("update",update);
arrayMap.add(map);
}
offlineCityMap.put("updateElements", arrayMap);
result.success(offlineCityMap);
}
/**
* 根据城市名搜索该城市离线地图记录
*/
private void seachCityList(MethodCall call,MethodChannel.Result result) {
if (null == mMKOfflineMap) {
result.success(null);
return;
}
Map<String, Object> argument = call.arguments();
if (null == argument || null == mMKOfflineMap) {
result.success(null);
return;
}
if (!argument.containsKey("cityName")) {
result.success(null);
return;
}
String sCityName = (String) argument.get("cityName");
if (null == sCityName) {
result.success(null);
return;
}
ArrayList<MKOLSearchRecord> seachCityList = mMKOfflineMap.searchCity(sCityName);
if (null == seachCityList){
result.success(null);
return;
}
ArrayList<Map> arrayMap = new ArrayList<>();
HashMap<String, ArrayList> offlineCityMap = new HashMap<>();
for (int i = 0; i < seachCityList.size(); i++) {
Map map = new HashMap();
int cityID = seachCityList.get(i).cityID;
int cityType = seachCityList.get(i).cityType;
int dataSize = (int) seachCityList.get(i).dataSize;
String cityName = seachCityList.get(i).cityName;
ArrayList<MKOLSearchRecord> childCities = seachCityList.get(i).childCities;
ArrayList<Map> childArray = new ArrayList<>();
if (null != childCities && childCities.size() > 0) {
for (int j = 0; j < childCities.size(); j++) {
HashMap childMap = new HashMap();
int childCityID = childCities.get(j).cityID;
int childCityType = childCities.get(j).cityType;
int childDataSize = (int) childCities.get(j).dataSize;
String childCityName = childCities.get(j).cityName;
childMap.put("cityID",childCityID);
childMap.put("cityType",childCityType);
childMap.put("dataSize",childDataSize);
childMap.put("cityName",childCityName);
childArray.add(childMap);
}
}
map.put("cityID",cityID);
map.put("dataSize",dataSize);
map.put("cityName",cityName);
map.put("cityType",cityType);
map.put("childCities",childArray);
arrayMap.add(map);
}
offlineCityMap.put("searchCityRecord", arrayMap);
result.success(offlineCityMap);
}
/**
* 返回热门城市列表
*/
private void getHotCityList(MethodChannel.Result result) {
if (null == mMKOfflineMap) {
result.success(null);
return;
}
ArrayList<MKOLSearchRecord> hotCityList = mMKOfflineMap.getHotCityList();
if (null == hotCityList){
result.success(null);
return;
}
ArrayList<Map> arrayMap = new ArrayList<>();
HashMap<String, ArrayList> hotCityMap = new HashMap<>();
for (int i = 0; i < hotCityList.size(); i++) {
Map map = new HashMap();
int cityID = hotCityList.get(i).cityID;
int cityType = hotCityList.get(i).cityType;
int dataSize = (int) hotCityList.get(i).dataSize;
String cityName = hotCityList.get(i).cityName;
ArrayList<MKOLSearchRecord> childCities = hotCityList.get(i).childCities;
ArrayList<Map> childArray = new ArrayList<>();
if (null != childCities && childCities.size() > 0) {
for (int j = 0; j < childCities.size(); j++) {
HashMap childMap = new HashMap();
int childCityID = childCities.get(j).cityID;
int childCityType = childCities.get(j).cityType;
int childDataSize = (int) childCities.get(j).dataSize;
String childCityName = childCities.get(j).cityName;
childMap.put("cityID",childCityID);
childMap.put("cityType",childCityType);
childMap.put("dataSize",childDataSize);
childMap.put("cityName",childCityName);
childArray.add(childMap);
}
}
map.put("cityID",cityID);
map.put("dataSize",dataSize);
map.put("cityName",cityName);
map.put("cityType",cityType);
map.put("childCities",childArray);
arrayMap.add(map);
}
hotCityMap.put("searchCityRecord", arrayMap);
result.success(hotCityMap);
}
/**
* 返回支持离线地图城市列表
*/
private void getOfflineCityList(MethodChannel.Result result) {
if (null == mMKOfflineMap) {
result.success(null);
return;
}
ArrayList<MKOLSearchRecord> offlineCityList = mMKOfflineMap.getOfflineCityList();
if (null == offlineCityList){
result.success(null);
return;
}
ArrayList<Map> arrayMap = new ArrayList<>();
HashMap<String, ArrayList> offlineCityMap = new HashMap<>();
for (int i = 0; i < offlineCityList.size(); i++) {
Map map = new HashMap();
int cityID = offlineCityList.get(i).cityID;
int cityType = offlineCityList.get(i).cityType;
int dataSize = (int) offlineCityList.get(i).dataSize;
String cityName = offlineCityList.get(i).cityName;
ArrayList<MKOLSearchRecord> childCities = offlineCityList.get(i).childCities;
ArrayList<Map> childArray = new ArrayList<>();
if (null != childCities && childCities.size() > 0) {
for (int j = 0; j < childCities.size(); j++) {
HashMap childMap = new HashMap();
int childCityID = childCities.get(j).cityID;
int childCityType = childCities.get(j).cityType;
int childDataSize = (int) childCities.get(j).dataSize;
String childCityName = childCities.get(j).cityName;
childMap.put("cityID",childCityID);
childMap.put("cityType",childCityType);
childMap.put("dataSize",childDataSize);
childMap.put("cityName",childCityName);
childArray.add(childMap);
}
}
map.put("cityID",cityID);
map.put("dataSize",dataSize);
map.put("cityName",cityName);
map.put("cityType",cityType);
map.put("childCities",childArray);
arrayMap.add(map);
}
offlineCityMap.put("searchCityRecord", arrayMap);
result.success(offlineCityMap);
}
}
@@ -0,0 +1,24 @@
package com.baidu.flutter_bmfmap.map;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.TextureMapView;
class TextureMapViewWrapper extends FlutterCommonMapView {
private TextureMapView mTextureMapView;
public TextureMapViewWrapper(TextureMapView textureMapView, String viewType){
mTextureMapView = textureMapView;
mViewType = viewType;
}
@Override
public MapView getMapView() {
return null;
}
@Override
public TextureMapView getTextureMapView() {
return mTextureMapView;
}
}
@@ -0,0 +1,45 @@
package com.baidu.flutter_bmfmap.map.mapHandler;
/**
* 地图有些属性没有get接口,这个lei就用来暂存这些属性状态,供flutter端获取
*/
public class BMFMapStatus {
private static volatile BMFMapStatus sInstance;
public static BMFMapStatus getsInstance(){
if (null == sInstance) {
synchronized(BMFMapStatus.class) {
if (null == sInstance) {
sInstance = new BMFMapStatus();
}
}
}
return sInstance;
}
public boolean isBaseIndoorEnable() {
return mBaseIndoorEnable;
}
public void setBaseIndoorEnable(boolean mBaseIndoorEnable) {
this.mBaseIndoorEnable = mBaseIndoorEnable;
}
public boolean isIndoorMapPoiEnable() {
return mIndoorMapPoiEnable;
}
public void setIndoorMapPoiEnable(boolean mIndoorMapPoiEnable) {
this.mIndoorMapPoiEnable = mIndoorMapPoiEnable;
}
/**
* 室内图状态
*/
private boolean mBaseIndoorEnable = false;
private boolean mIndoorMapPoiEnable = true;
}
@@ -0,0 +1,24 @@
package com.baidu.flutter_bmfmap.map.mapHandler;
import android.content.Context;
import com.baidu.flutter_bmfmap.map.FlutterCommonMapView;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
public abstract class BMapHandler{
protected FlutterCommonMapView mMapView;
public BMapHandler(FlutterCommonMapView mapView){
this.mMapView = mapView;
}
public abstract void handlerMethodCallResult(Context context,MethodCall call, MethodChannel.Result result);
public void updateMapView(FlutterCommonMapView mapView){
mMapView = mapView;
}
public void clean(){}
}
@@ -0,0 +1,200 @@
package com.baidu.flutter_bmfmap.map.mapHandler;
import android.content.Context;
import android.util.Log;
import com.baidu.flutter_bmfmap.map.FlutterCommonMapView;
import com.baidu.flutter_bmfmap.utils.Constants;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import com.baidu.flutter_bmfmap.utils.Constants.MethodProtocol.CustomMapProtocol;
import com.baidu.flutter_bmfmap.utils.Constants.MethodProtocol.MapStateProtocol;
import com.baidu.flutter_bmfmap.utils.Constants.MethodProtocol.HeatMapProtocol;
import com.baidu.flutter_bmfmap.utils.Constants.MethodProtocol.TileMapProtocol;
import com.baidu.flutter_bmfmap.utils.Constants.MethodProtocol.MarkerProtocol;
import com.baidu.flutter_bmfmap.utils.Constants.MethodProtocol.InfoWindowProtocol;
import com.baidu.flutter_bmfmap.utils.Constants.MethodProtocol.ProjectionMethodId;
import com.baidu.flutter_bmfmap.utils.Env;
public class BMapHandlerFactory{
private static volatile BMapHandlerFactory sInstance;
private HashMap<Integer, BMapHandler> mMapHandlerHashMap;
public static BMapHandlerFactory getInstance(FlutterCommonMapView mapView) {
if (null == sInstance) {
synchronized (BMapHandlerFactory.class) {
if (null == sInstance) {
sInstance = new BMapHandlerFactory(mapView);
} else {
sInstance.updateMapView(mapView);
}
}
} else {
sInstance.updateMapView(mapView);
}
return sInstance;
}
private void updateMapView(FlutterCommonMapView mapView) {
if (null == mapView) {
return;
}
if(null == mMapHandlerHashMap || mMapHandlerHashMap.isEmpty()){
init(mapView);
}
Iterator it = mMapHandlerHashMap.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<Integer, BMapHandler> entry = (Map.Entry<Integer, BMapHandler>) it.next();
BMapHandler bMapHandler = entry.getValue();
if (null != bMapHandler) {
bMapHandler.updateMapView(mapView);
}
}
}
private BMapHandlerFactory(FlutterCommonMapView mapView) {
init(mapView);
}
private void init(FlutterCommonMapView mapView) {
if (null == mapView) {
return;
}
mMapHandlerHashMap = new HashMap<>();
mMapHandlerHashMap.put(Constants.BMapHandlerType.CUSTOM_MAP,new CustomMapHandler(mapView));
mMapHandlerHashMap.put(Constants.BMapHandlerType.MAP_STATE,new MapStateHandler(mapView));
mMapHandlerHashMap.put(Constants.BMapHandlerType.INDOOR_MAP, new IndoorMapHandler(mapView));
mMapHandlerHashMap.put(Constants.BMapHandlerType.MAP_UPDATE, new MapUpdateHandler(mapView));
mMapHandlerHashMap.put(Constants.BMapHandlerType.HEAT_MAP, new HeatMapHandler(mapView));
mMapHandlerHashMap.put(Constants.BMapHandlerType.TILE_MAP, new TileMapHandler(mapView));
mMapHandlerHashMap.put(Constants.BMapHandlerType.INFOWINDOW_HANDLER, new InfoWindowHandler(mapView));
mMapHandlerHashMap.put(Constants.BMapHandlerType.MARKER_HANDLER, new MarkerHandler(mapView));
mMapHandlerHashMap.put(Constants.BMapHandlerType.LOCATION_LAYER, new LocationLayerHandler(mapView));
mMapHandlerHashMap.put(Constants.BMapHandlerType.PROJECTION, new ProjectionHandler(mapView));
}
public boolean dispatchMethodHandler(Context context, MethodCall call, MethodChannel.Result result,
MethodChannel methodChannel) {
if (null == call) {
return false;
}
String methodId = call.method;
if(Env.DEBUG){
Log.d("BMapHandlerFactory", "dispatchMethodHandler: " + methodId);
}
BMapHandler bMapHandler = null;
switch (methodId) {
case CustomMapProtocol.sMapSetCustomMapStyleEnableMethod:
case CustomMapProtocol.sMapSetCustomMapStylePathMethod:
case CustomMapProtocol.sMapSetCustomMapStyleWithOptionMethod:
bMapHandler = mMapHandlerHashMap.get(Constants.BMapHandlerType.CUSTOM_MAP);
break;
case Constants.MethodProtocol.IndoorMapProtocol.sShowBaseIndoorMapMethod:
case Constants.MethodProtocol.IndoorMapProtocol.sShowBaseIndoorMapPoiMethod:
case Constants.MethodProtocol.IndoorMapProtocol.sSwitchBaseIndoorMapFloorMethod:
case Constants.MethodProtocol.IndoorMapProtocol.sGetFocusedBaseIndoorMapInfoMethod:
bMapHandler = mMapHandlerHashMap.get(Constants.BMapHandlerType.INDOOR_MAP);
break;
case MapStateProtocol.sMapUpdateMethod:
case MapStateProtocol.sMapSetVisibleMapBoundsMethod:
case MapStateProtocol.sMapSetVisibleMapBoundsWithPaddingMethod:
case MapStateProtocol.sMapSetCompassImageMethod:
case MapStateProtocol.sMapSetCustomTrafficColorMethod:
case MapStateProtocol.sMapTakeSnapshotMethod:
case MapStateProtocol.sMapTakeSnapshotWithRectMethod:
case MapStateProtocol.sMapDidUpdateWidget:
case MapStateProtocol.sMapReassemble:
bMapHandler = mMapHandlerHashMap.get(Constants.BMapHandlerType.MAP_STATE);
break;
case MapStateProtocol.sMapZoomInMethod:
case MapStateProtocol.sMapZoomOutMethod:
case MapStateProtocol.sMapSetCenterCoordinateMethod:
case MapStateProtocol.sMapSetCenterZoomMethod:
case MapStateProtocol.sMapSetMapStatusMethod:
case MapStateProtocol.sMapSetScrollByMethod:
case MapStateProtocol.sMapSetZoomByMethod:
case MapStateProtocol.sMapSetZoomPointByMethod:
case MapStateProtocol.sMapSetZoomToMethod:
case MapStateProtocol.sMapGetMapStatusMethod:
bMapHandler = mMapHandlerHashMap.get(Constants.BMapHandlerType.MAP_UPDATE);
break;
case HeatMapProtocol.sMapAddHeatMapMethod:
case HeatMapProtocol.sMapRemoveHeatMapMethod:
case HeatMapProtocol.sShowHeatMapMethod:
bMapHandler = mMapHandlerHashMap.get(Constants.BMapHandlerType.HEAT_MAP);
break;
case TileMapProtocol.sAddTileMapMethod:
case TileMapProtocol.sRemoveTileMapMethod:
bMapHandler = mMapHandlerHashMap.get(Constants.BMapHandlerType.TILE_MAP);
break;
case MarkerProtocol.sMapAddMarkerMethod:
case MarkerProtocol.sMapAddMarkersMethod:
case MarkerProtocol.sMapRemoveMarkerMethod:
case MarkerProtocol.sMapRemoveMarkersMethod:
case MarkerProtocol.sMapDidSelectMarkerMethod:
case MarkerProtocol.sMapDidDeselectMarkerMethod:
case MarkerProtocol.sMapCleanAllMarkersMethod:
case MarkerProtocol.sMapUpdateMarkerMemberMethod:
bMapHandler = mMapHandlerHashMap.get(Constants.BMapHandlerType.MARKER_HANDLER);
break;
case InfoWindowProtocol.sAddInfoWindowMapMethod:
case InfoWindowProtocol.sRemoveInfoWindowMapMethod:
case InfoWindowProtocol.sAddInfoWindowsMapMethod:
bMapHandler = mMapHandlerHashMap.get(Constants.BMapHandlerType.INFOWINDOW_HANDLER);
break;
case Constants.LocationLayerMethodId.sMapShowUserLocationMethod:
case Constants.LocationLayerMethodId.sMapUpdateLocationDataMethod:
case Constants.LocationLayerMethodId.sMapUserTrackingModeMethod:
case Constants.LocationLayerMethodId.sMapUpdateLocationDisplayParamMethod:
bMapHandler = mMapHandlerHashMap.get(Constants.BMapHandlerType.LOCATION_LAYER);
break;
case ProjectionMethodId.sFromScreenLocation:
case ProjectionMethodId.sToScreenLocation:
bMapHandler = mMapHandlerHashMap.get(Constants.BMapHandlerType.PROJECTION);
break;
default:
if(methodId.startsWith("flutter_bmfmap/map/get")){
bMapHandler = mMapHandlerHashMap.get(Constants.BMapHandlerType.MAP_UPDATE);
}
break;
}
if (null == bMapHandler) {
return false;
}
bMapHandler.handlerMethodCallResult(context,call, result);
return true;
}
public void clean(){
if (null == mMapHandlerHashMap || mMapHandlerHashMap.size() == 0) {
return;
}
BMapHandler bMapHandler = null;
Iterator iterator = mMapHandlerHashMap.values().iterator();
while (iterator.hasNext()){
bMapHandler = (BMapHandler) iterator.next();
if(null == bMapHandler){
continue;
}
bMapHandler.clean();
}
}
}
@@ -0,0 +1,437 @@
package com.baidu.flutter_bmfmap.map.mapHandler;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import com.baidu.flutter_bmfmap.map.FlutterCommonMapView;
import com.baidu.flutter_bmfmap.utils.Constants;
import com.baidu.mapapi.map.CustomMapStyleCallBack;
import com.baidu.mapapi.map.MapCustomStyleOptions;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.TextureMapView;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.view.FlutterMain;
public class CustomMapHandler extends BMapHandler {
private MapViewCustomMapHandler mMapViewCustomMapHandler;
private TextureMapViewCustomMapHandler mTextureMapViewCustomMapHandler;
public CustomMapHandler(FlutterCommonMapView mapView) {
super(mapView);
mMapViewCustomMapHandler = new MapViewCustomMapHandler(mMapView);
mTextureMapViewCustomMapHandler = new TextureMapViewCustomMapHandler(mMapView);
}
@Override
public void updateMapView(FlutterCommonMapView mapView){
mMapView = mapView;
mMapViewCustomMapHandler.updateMapView(mapView);
mTextureMapViewCustomMapHandler.updateMapView(mapView);
}
@Override
public void handlerMethodCallResult(Context context,MethodCall call, MethodChannel.Result result) {
switch (mMapView.getViewType()){
case Constants.ViewType.sMapView:
mMapViewCustomMapHandler.handlerMethodCallResult(context, call, result);
break;
case Constants.ViewType.sTextureMapView:
mTextureMapViewCustomMapHandler.handlerMethodCallResult(context, call, result);
break;
default:
break;
}
}
class MapViewCustomMapHandler extends BMapHandler {
private MapView mRealMapView;
public MapViewCustomMapHandler(FlutterCommonMapView mapView) {
super(mapView);
mRealMapView = mMapView.getMapView();
}
@Override
public void updateMapView(FlutterCommonMapView mapView){
mMapView = mapView;
if(null != mMapView){
mRealMapView = mMapView.getMapView();
}
}
@Override
public void handlerMethodCallResult(Context context, MethodCall call, MethodChannel.Result result) {
if (null == call) {
result.success(false);
return;
}
String methodId = call.method;
if (TextUtils.isEmpty(methodId)) {
result.success(false);
return;
}
switch (methodId) {
case Constants.MethodProtocol.CustomMapProtocol.sMapSetCustomMapStyleEnableMethod:
setCustomMapStyleEnable(call, result);
break;
case Constants.MethodProtocol.CustomMapProtocol.sMapSetCustomMapStylePathMethod:
setCustomMapStylePath(context,call,result);
break;
case Constants.MethodProtocol.CustomMapProtocol.sMapSetCustomMapStyleWithOptionMethod:
setMapCustomStyle(call,result);
break;
default:
break;
}
}
/**
* 个性化地图开关
*/
private void setCustomMapStyleEnable(MethodCall call, MethodChannel.Result result) {
Map<String, Object> argument = call.arguments();
if (null == argument || null == mMapView) {
result.success(false);
return;
}
if (!argument.containsKey("enable")) {
result.success(false);
return;
}
boolean enable = (boolean) argument.get("enable");
mRealMapView.setMapCustomStyleEnable(enable);
result.success(true);
}
/**
* 设置个性化地图样式文件的路径
*/
private void setCustomMapStylePath(Context context, MethodCall call, MethodChannel.Result result) {
Map<String, Object> argument = call.arguments();
if (null == argument || null == mMapView || context == null) {
result.success(false);
return;
}
if (!argument.containsKey("path") || !argument.containsKey("mode")) {
result.success(false);
return;
}
String path = (String) argument.get("path");
if (path.isEmpty()) {
result.success(false);
return;
}
String customStyleFilePath = getCustomStyleFilePath(context, path);
if(TextUtils.isEmpty(customStyleFilePath)){
result.success(false);
return;
}
mRealMapView.setMapCustomStylePath(customStyleFilePath);
result.success(true);
}
private String getCustomStyleFilePath(Context context, String customStyleFilePath) {
if (customStyleFilePath.isEmpty()) {
return null;
}
FileOutputStream outputStream = null;
InputStream inputStream = null;
String parentPath = null;
String customStyleFileName = null;
try {
customStyleFileName = FlutterMain.getLookupKeyForAsset(customStyleFilePath);
inputStream = context.getAssets().open(customStyleFileName);
byte[] buffer = new byte[inputStream.available()];
inputStream.read(buffer);
parentPath = context.getCacheDir().getAbsolutePath();
String substr = customStyleFileName.substring(0, customStyleFileName.lastIndexOf("/"));
File customStyleFile = new File(parentPath + "/" + customStyleFileName);
if (customStyleFile.exists()) {
customStyleFile.delete();
}
File dirFile = new File(parentPath + "/" + substr);
if (!dirFile.exists()) {
dirFile.mkdirs();
}
customStyleFile.createNewFile();
outputStream = new FileOutputStream(customStyleFile);
outputStream.write(buffer);
} catch (IOException e) {
Log.e("TAG", "Copy file failed", e);
} finally {
try {
if (inputStream != null) {
inputStream.close();
}
if (outputStream != null) {
outputStream.close();
}
} catch (IOException e) {
Log.e("TAG", "Close stream failed", e);
}
}
return parentPath + "/" + customStyleFileName;
}
/**
* 在线个性化样式加载状态回调接口
*/
private void setMapCustomStyle(MethodCall call, final MethodChannel.Result result) {
Map<String, Object> argument = call.arguments();
if (null == argument || null == mMapView) {
result.success(false);
return;
}
if (!argument.containsKey("customMapStyleOption")) {
return;
}
Map<String, Object> customMapStyleOption = (Map<String, Object>) argument.get("customMapStyleOption");
if (!customMapStyleOption.containsKey("customMapStyleID")
|| !customMapStyleOption.containsKey("customMapStyleFilePath")) {
return;
}
String customMapStyleID = (String) customMapStyleOption.get("customMapStyleID");
String customMapStyleFilePath = (String) customMapStyleOption.get("customMapStyleFilePath");
if (customMapStyleID.isEmpty() && customMapStyleFilePath.isEmpty()) {
return;
}
MapCustomStyleOptions mapCustomStyleOptions = new MapCustomStyleOptions();
mapCustomStyleOptions.customStyleId(customMapStyleID);
mapCustomStyleOptions.localCustomStylePath(customMapStyleFilePath);
final HashMap<String, String> reslutMap = new HashMap<>();
mRealMapView.setMapCustomStyle(mapCustomStyleOptions, new CustomMapStyleCallBack() {
@Override
public boolean onPreLoadLastCustomMapStyle(String path) {
reslutMap.put("preloadPath", path);
result.success(reslutMap);
return false;
}
@Override
public boolean onCustomMapStyleLoadSuccess(boolean b, String path) {
// TODO: 2020-03-05 回调的 boolean 类型没有返回之后补齐
reslutMap.put("successPath", path);
result.success(reslutMap);
return false;
}
@Override
public boolean onCustomMapStyleLoadFailed(int status, String message, String path) {
String sStatus = String.valueOf(status);
reslutMap.put("errorCode", sStatus);
reslutMap.put("successPath", path);
result.success(reslutMap);
return false;
}
});
}
}
class TextureMapViewCustomMapHandler extends BMapHandler {
private TextureMapView mTextureMapView;
public TextureMapViewCustomMapHandler(FlutterCommonMapView mapView) {
super(mapView);
mTextureMapView = mMapView.getTextureMapView();
}
@Override
public void updateMapView(FlutterCommonMapView mapView){
mMapView = mapView;
if(null != mMapView){
mTextureMapView = mMapView.getTextureMapView();
}
}
@Override
public void handlerMethodCallResult(Context context, MethodCall call, MethodChannel.Result result) {
if (null == call) {
return;
}
String methodId = call.method;
if (TextUtils.isEmpty(methodId)) {
return;
}
switch (methodId) {
case Constants.MethodProtocol.CustomMapProtocol.sMapSetCustomMapStyleEnableMethod:
setCustomMapStyleEnable(call, result);
break;
case Constants.MethodProtocol.CustomMapProtocol.sMapSetCustomMapStylePathMethod:
setCustomMapStylePath(context,call,result);
break;
case Constants.MethodProtocol.CustomMapProtocol.sMapSetCustomMapStyleWithOptionMethod:
setMapCustomStyle(context, call,result);
break;
default:
break;
}
}
/**
* 个性化地图开关
*/
private void setCustomMapStyleEnable(MethodCall call, MethodChannel.Result result) {
Map<String, Object> argument = call.arguments();
if (null == argument || null == mMapView) {
result.success(false);
return;
}
if (!argument.containsKey("enable")) {
result.success(false);
return;
}
boolean enable = (boolean) argument.get("enable");
mTextureMapView.setMapCustomStyleEnable(enable);
result.success(true);
}
/**
* 设置个性化地图样式文件的路径
*/
private void setCustomMapStylePath(Context context, MethodCall call, MethodChannel.Result result) {
Map<String, Object> argument = call.arguments();
if (null == argument || null == mMapView || context == null) {
result.success(false);
return;
}
if (!argument.containsKey("path") || !argument.containsKey("mode")) {
result.success(false);
return;
}
String path = (String) argument.get("path");
if (path.isEmpty()) {
result.success(false);
return;
}
String customStyleFilePath = getCustomStyleFilePath(context, path);
mTextureMapView.setMapCustomStylePath(customStyleFilePath);
result.success(true);
}
private String getCustomStyleFilePath(Context context, String customStyleFilePath) {
if (customStyleFilePath.isEmpty()) {
return null;
}
FileOutputStream outputStream = null;
InputStream inputStream = null;
String parentPath = null;
String customStyleFileName = null;
try {
customStyleFileName = FlutterMain.getLookupKeyForAsset(customStyleFilePath);
inputStream = context.getAssets().open(customStyleFileName);
byte[] buffer = new byte[inputStream.available()];
inputStream.read(buffer);
parentPath = context.getCacheDir().getAbsolutePath();
String substr = customStyleFileName.substring(0, customStyleFileName.lastIndexOf("/"));
File customStyleFile = new File(parentPath + "/" + customStyleFileName);
if (customStyleFile.exists()) {
customStyleFile.delete();
}
File dirFile = new File(parentPath + "/" + substr);
if (!dirFile.exists()) {
dirFile.mkdirs();
}
customStyleFile.createNewFile();
outputStream = new FileOutputStream(customStyleFile);
outputStream.write(buffer);
} catch (IOException e) {
Log.e("TAG", "Copy file failed", e);
} finally {
try {
if (inputStream != null) {
inputStream.close();
}
if (outputStream != null) {
outputStream.close();
}
} catch (IOException e) {
Log.e("TAG", "Close stream failed", e);
}
}
return parentPath + "/" + customStyleFileName;
}
/**
* 在线个性化样式加载状态回调接口
*/
private void setMapCustomStyle(Context context, MethodCall call,
final MethodChannel.Result result) {
Map<String, Object> argument = call.arguments();
if (null == argument || null == mMapView) {
result.success(false);
return;
}
if (!argument.containsKey("customMapStyleOption")) {
return;
}
Map<String, Object> customMapStyleOption = (Map<String, Object>) argument.get("customMapStyleOption");
if (!customMapStyleOption.containsKey("customMapStyleID")
|| !customMapStyleOption.containsKey("customMapStyleFilePath")) {
return;
}
String customMapStyleID = (String) customMapStyleOption.get("customMapStyleID");
String customMapStyleFilePath = (String) customMapStyleOption.get("customMapStyleFilePath");
customMapStyleFilePath = getCustomStyleFilePath(context, customMapStyleFilePath);
if (customMapStyleID.isEmpty() && customMapStyleFilePath.isEmpty()) {
return;
}
MapCustomStyleOptions mapCustomStyleOptions = new MapCustomStyleOptions();
mapCustomStyleOptions.customStyleId(customMapStyleID);
mapCustomStyleOptions.localCustomStylePath(customMapStyleFilePath);
final HashMap<String, String> reslutMap = new HashMap<>();
mTextureMapView.setMapCustomStyle(mapCustomStyleOptions, new CustomMapStyleCallBack() {
@Override
public boolean onPreLoadLastCustomMapStyle(String path) {
reslutMap.put("preloadPath", path);
result.success(reslutMap);
return false;
}
@Override
public boolean onCustomMapStyleLoadSuccess(boolean b, String path) {
// TODO: 2020-03-05 回调的 boolean 类型没有返回之后补齐
reslutMap.put("successPath", path);
result.success(reslutMap);
return false;
}
@Override
public boolean onCustomMapStyleLoadFailed(int status, String message, String path) {
String sStatus = String.valueOf(status);
reslutMap.put("errorCode", sStatus);
reslutMap.put("successPath", path);
result.success(reslutMap);
return false;
}
});
}
}
}
@@ -0,0 +1,270 @@
package com.baidu.flutter_bmfmap.map.mapHandler;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import com.baidu.flutter_bmfmap.map.FlutterCommonMapView;
import com.baidu.flutter_bmfmap.utils.Env;
import com.baidu.flutter_bmfmap.utils.converter.FlutterDataConveter;
import com.baidu.flutter_bmfmap.utils.converter.TypeConverter;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.Gradient;
import com.baidu.mapapi.map.HeatMap;
import com.baidu.mapapi.map.WeightedLatLng;
import java.util.List;
import java.util.Map;
import java.util.Iterator;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import com.baidu.flutter_bmfmap.utils.Constants.MethodProtocol.HeatMapProtocol;
class HeatMapHandler extends BMapHandler {
private static final String TAG = "HeapMapHandler";
HeatMap mHeatMap = null;
public HeatMapHandler(FlutterCommonMapView mapView) {
super(mapView);
}
@Override
public void handlerMethodCallResult(Context context,MethodCall call, MethodChannel.Result result) {
if (null == call) {
result.success(false);
return;
}
String methodId = call.method;
if (TextUtils.isEmpty(methodId)) {
result.success(false);
return;
}
boolean ret = false;
switch (methodId) {
case HeatMapProtocol.sMapAddHeatMapMethod:
ret = addHeapMap(context, call);
break;
case HeatMapProtocol.sMapRemoveHeatMapMethod:
ret = switchHeatMap(context, call);
break;
case HeatMapProtocol.sShowHeatMapMethod:
ret = isShowBaiduHeatMap(call);
break;
default:
break;
}
result.success(ret);
}
/**
* 是否显示百度热力图
*/
public boolean isShowBaiduHeatMap(MethodCall call) {
Map<String, Object> argument = call.arguments();
if (argument == null || !argument.containsKey("show")) {
return false;
}
Boolean show = (Boolean) argument.get("show");
if (null == show) {
return false;
}
if (null == mMapView) {
return false;
}
BaiduMap baiduMap = mMapView.getBaiduMap();
baiduMap.setBaiduHeatMapEnabled(show);
return true;
}
public boolean addHeapMap(Context context,MethodCall call){
if(Env.DEBUG){
Log.d(TAG, "addHeapMap enter");
}
Map<String, Object> argument = call.arguments();
if(null == argument){
if(Env.DEBUG){
Log.d(TAG, "argument is null");
}
return false;
}
Object heatMapObj = argument.get("heatMap");
if(null == heatMapObj){
if(Env.DEBUG){
Log.d(TAG, "null == heatMapObj");
}
return false;
}
Map<String, Object> heatMapMap = (Map<String, Object>)heatMapObj;
if(null == heatMapMap){
if(Env.DEBUG){
Log.d(TAG, "null == heatMapMap");
}
return false;
}
if(!heatMapMap.containsKey("data")
|| !heatMapMap.containsKey("radius")
|| !heatMapMap.containsKey("opacity")
|| !heatMapMap.containsKey("gradient")) {
if (Env.DEBUG) {
Log.d(TAG, "argument does not contain"+ argument.toString());
}
return false;
}
HeatMap.Builder builder = new HeatMap.Builder();
List<WeightedLatLng> weightedLatLngList = getData(heatMapMap);
if(null == weightedLatLngList){
if(Env.DEBUG){
Log.d(TAG, "null == weightedLatLngList");
}
return false;
}
builder.weightedData(weightedLatLngList);
Object gradientObj = heatMapMap.get("gradient");
if(null == gradientObj){
if(Env.DEBUG){
Log.d(TAG, "null == gradientObj");
}
return false;
}
Map<String, Object> gradientMap = (Map<String, Object>)gradientObj;
if(null == gradientMap){
if(Env.DEBUG){
Log.d(TAG, "null == gradientMap");
}
return false;
}
Gradient gradient = getGradient(gradientMap);
builder.gradient(gradient);
Double opacity = new TypeConverter<Double>().getValue(heatMapMap, "opacity");
if(null == opacity){
if(Env.DEBUG){
Log.d(TAG, "null == opacity");
}
return false;
}
builder.opacity(opacity);
Integer radius = new TypeConverter<Integer>().getValue(heatMapMap, "radius");
if(null == radius) {
if(Env.DEBUG){
Log.d(TAG, "null == radius");
}
return false;
}
builder.radius(radius);
mHeatMap = builder.build();
if(null == mHeatMap){
if(Env.DEBUG){
Log.d(TAG, "null == mHeatMap");
}
return false;
}
BaiduMap baiduMap = mMapView.getBaiduMap();
if(null == baiduMap){
return false;
}
baiduMap.addHeatMap(mHeatMap);
return true;
}
private List<WeightedLatLng> getData(Map<String, Object> heatMapMap){
List<WeightedLatLng> weightedLatLngList = null;
Object dataObj = heatMapMap.get("data");
if(null == dataObj){
return null;
}
List<Map<String, Object> > dataList = (List<Map<String, Object> >)dataObj;
if(null == dataList){
return null;
}
weightedLatLngList = FlutterDataConveter.mapToWeightedLatLngList(dataList);
return weightedLatLngList;
}
private Gradient getGradient(Map<String, Object> heatMapMap){
if(!heatMapMap.containsKey("colors") || !heatMapMap.containsKey("startPoints")){
return null;
}
Object colorsObj = heatMapMap.get("colors");
Object startPointsObj = heatMapMap.get("startPoints");
if(null == colorsObj || null == startPointsObj){
return null;
}
List<String> colorsList = (List<String>)colorsObj;
List<Double> startPointsList = (List<Double>)startPointsObj;
if(null == colorsList || null == startPointsList){
return null;
}
int[] intColors = new int[colorsList.size()];
Iterator<String> itr = colorsList.iterator();
int i = 0;
while (itr.hasNext()){
String colorStr = itr.next();
int color = FlutterDataConveter.strColorToInteger(colorStr);
intColors[i++] = color;
}
float[] startPoints = new float[startPointsList.size()];
Iterator<Double> startPointsItr = startPointsList.iterator();
i = 0;
while (startPointsItr.hasNext()){
startPoints[i++] = startPointsItr.next().floatValue();
}
Gradient gradient = new Gradient(intColors, startPoints);
return gradient;
}
public boolean switchHeatMap(Context context,MethodCall call){
if(Env.DEBUG){
Log.d(TAG, "switchHeatMap enter");
}
if(null == mHeatMap){
return false;
}
BaiduMap baiduMap = mMapView.getBaiduMap();
if(null == baiduMap){
if (Env.DEBUG) {
Log.d(TAG, "baiduMap is null");
}
return false;
}
mHeatMap.removeHeatMap();
mHeatMap = null;
return true;
}
}
@@ -0,0 +1,195 @@
package com.baidu.flutter_bmfmap.map.mapHandler;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import com.baidu.flutter_bmfmap.map.FlutterCommonMapView;
import com.baidu.flutter_bmfmap.utils.Constants;
import com.baidu.flutter_bmfmap.utils.Env;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapBaseIndoorMapInfo;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
public class IndoorMapHandler extends BMapHandler {
private static final String TAG = "IndoorMapHandler";
private BaiduMap mBaiduMap;
public IndoorMapHandler(FlutterCommonMapView mapView) {
super(mapView);
if(null != mapView){
mBaiduMap = mapView.getBaiduMap();
}
}
@Override
public void updateMapView(FlutterCommonMapView mapView) {
super.updateMapView(mapView);
if(null != mapView){
mBaiduMap = mapView.getBaiduMap();
}
}
@Override
public void handlerMethodCallResult(Context context, MethodCall call, MethodChannel.Result result) {
if(Env.DEBUG){
Log.d(TAG, "handlerMethodCallResult enter");
}
if (null == call) {
result.success(false);
return;
}
String methodId = call.method;
if (TextUtils.isEmpty(methodId)) {
result.success(false);
return;
}
switch (methodId) {
case Constants.MethodProtocol.IndoorMapProtocol.sShowBaseIndoorMapMethod:
setIndoorMap(call, result);
break;
case Constants.MethodProtocol.IndoorMapProtocol.sShowBaseIndoorMapPoiMethod:
setIndoorMapPoi(call, result);
break;
case Constants.MethodProtocol.IndoorMapProtocol.sSwitchBaseIndoorMapFloorMethod:
switchIndoorMapFloor(call, result);
break;
case Constants.MethodProtocol.IndoorMapProtocol.sGetFocusedBaseIndoorMapInfoMethod:
getFocusedBaseIndoorMapInfo(call, result);
break;
default:
break;
}
}
/**
* 室内图开关
*/
private void setIndoorMap(MethodCall call, MethodChannel.Result result) {
Map<String, Object> argument = call.arguments();
if (null == argument || null == mBaiduMap) {
result.success(false);
return;
}
if (!argument.containsKey("show")) {
result.success(false);
}
boolean showIndoorMap = (boolean) argument.get("show");
mBaiduMap.setIndoorEnable(showIndoorMap);
BMFMapStatus.getsInstance().setBaseIndoorEnable(showIndoorMap);
result.success(true);
}
/**
* 室内图poi开关
*/
private void setIndoorMapPoi(MethodCall call, MethodChannel.Result result) {
Map<String, Object> argument = call.arguments();
if (null == argument || null == mMapView) {
result.success(false);
return;
}
if(null == mBaiduMap){
return;
}
if (!argument.containsKey("showIndoorPoi")) {
result.success(false);
}
boolean showIndoorPoi = (boolean) argument.get("showIndoorPoi");
mBaiduMap.showMapIndoorPoi(showIndoorPoi);
BMFMapStatus.getsInstance().setIndoorMapPoiEnable(showIndoorPoi);
result.success(true);
}
/**
* 室内图楼层切换
*/
private void switchIndoorMapFloor(MethodCall call, MethodChannel.Result result) {
HashMap<String, Integer> errorMap = new HashMap<>();
int switchIndoorFloorSuccess = Constants.SwitchIndoorFloorError.FAILED;
Map<String, Object> argument = call.arguments();
if (null == argument || null == mBaiduMap) {
errorMap.put("result", switchIndoorFloorSuccess);
result.success(errorMap);
return;
}
if (!argument.containsKey("floorId") || !argument.containsKey("indoorId")) {
errorMap.put("result", switchIndoorFloorSuccess);
result.success(errorMap);
return;
}
String floorId = (String) argument.get("floorId");
String indoorId = (String) argument.get("indoorId");
if (floorId.isEmpty() || indoorId.isEmpty()) {
return;
}
MapBaseIndoorMapInfo.SwitchFloorError switchFloorError = mBaiduMap.switchBaseIndoorMapFloor(floorId, indoorId);
switch (switchFloorError) {
case SWITCH_OK:
switchIndoorFloorSuccess = Constants.SwitchIndoorFloorError.SUCCESS;
break;
case SWITCH_ERROR:
switchIndoorFloorSuccess = Constants.SwitchIndoorFloorError.FAILED;
break;
case FOCUSED_ID_ERROR:
switchIndoorFloorSuccess = Constants.SwitchIndoorFloorError.NOT_FOCUSED;
break;
case FLOOR_OVERLFLOW:
switchIndoorFloorSuccess = Constants.SwitchIndoorFloorError.NOT_EXIST;
break;
case FLOOR_INFO_ERROR:
switchIndoorFloorSuccess = Constants.SwitchIndoorFloorError.SWICH_FLOOR_INFO_ERROR;
break;
default:
break;
}
errorMap.put("result", switchIndoorFloorSuccess);
result.success(errorMap);
}
/**
* 获取当前聚焦的室内图信息
*/
private void getFocusedBaseIndoorMapInfo(MethodCall call, MethodChannel.Result result) {
if (null == mBaiduMap) {
return;
}
MapBaseIndoorMapInfo focusedBaseIndoorMapInfo = mBaiduMap.getFocusedBaseIndoorMapInfo();
BMFBaseIndoorMapInfo bmfBaseIndoorMapInfo = new BMFBaseIndoorMapInfo();
if (null != focusedBaseIndoorMapInfo) {
bmfBaseIndoorMapInfo.strFloor = focusedBaseIndoorMapInfo.getCurFloor();
bmfBaseIndoorMapInfo.strID = focusedBaseIndoorMapInfo.getID();
bmfBaseIndoorMapInfo.listStrFloors = focusedBaseIndoorMapInfo.getFloors();
}
HashMap<String, Object> stringObjectHashMap = new HashMap<>();
stringObjectHashMap.put("listStrFloors", bmfBaseIndoorMapInfo.listStrFloors);
stringObjectHashMap.put("strFloor", bmfBaseIndoorMapInfo.strFloor);
stringObjectHashMap.put("strID", bmfBaseIndoorMapInfo.strID);
result.success(stringObjectHashMap);
}
class BMFBaseIndoorMapInfo {
private String strID = "";
private String strFloor = "";
private ArrayList<String> listStrFloors;
}
}
@@ -0,0 +1,306 @@
package com.baidu.flutter_bmfmap.map.mapHandler;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import com.baidu.flutter_bmfmap.map.mapHandler.BMapHandler;
import com.baidu.flutter_bmfmap.map.FlutterCommonMapView;
import com.baidu.flutter_bmfmap.utils.Env;
import com.baidu.flutter_bmfmap.utils.converter.FlutterDataConveter;
import com.baidu.flutter_bmfmap.utils.converter.TypeConverter;
import com.baidu.mapapi.map.BaiduMap;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import com.baidu.flutter_bmfmap.utils.Constants.MethodProtocol.InfoWindowProtocol;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.InfoWindow;
import com.baidu.mapapi.model.LatLng;
public class InfoWindowHandler extends BMapHandler{
private static final String TAG = "InfoWindowHandler";
private HashMap<String, InfoWindow> mInfoWindows;
private HashMap<String, BitmapDescriptor> mBitmapMap = new HashMap<>();
private MethodChannel mMethodChannel;
private BaiduMap mBaiduMap;
public InfoWindowHandler(FlutterCommonMapView mapView){
super(mapView);
if(null != mMapView){
mBaiduMap = mMapView.getBaiduMap();
}
mInfoWindows = new HashMap<>();
}
@Override
public void updateMapView(FlutterCommonMapView mapView){
mMapView = mapView;
if(null != mMapView){
mBaiduMap = mMapView.getBaiduMap();
}
}
@Override
public void handlerMethodCallResult(Context context, MethodCall call, MethodChannel.Result result) {
if(Env.DEBUG){
Log.d(TAG, "handlerMethodCallResult enter");
}
if(null == mBaiduMap){
if(Env.DEBUG){
Log.d(TAG, "mBaidumap is null");
}
return;
}
String methodId = call.method;
switch (methodId){
case InfoWindowProtocol.sAddInfoWindowMapMethod:
addInfoWindow(call, result);
break;
case InfoWindowProtocol.sAddInfoWindowsMapMethod:
addInfoWindows(call,result);
break;
case InfoWindowProtocol.sRemoveInfoWindowMapMethod:
removeInfoWindow(call, result);
break;
default:
break;
}
}
/**
* 添加单个infoWindow
* @param call
* @param result
*/
private void addInfoWindow(MethodCall call, MethodChannel.Result result){
if(Env.DEBUG){
Log.d(TAG, "addInfoWindow enter");
}
Map<String, Object> argument = call.arguments();
if(null == argument){
if(Env.DEBUG){
Log.d(TAG, "argument is null");
}
return;
}
addOneInfoWindowImp(argument);
}
/**
* 具体添加一个infowindow
* @param infoWindowMap
*/
private void addOneInfoWindowImp(Map<String, Object> infoWindowMap){
AbstractMap.SimpleEntry<String,InfoWindow> infoWindowEntry = MaptoInfoWindowEntry(infoWindowMap);
if(null == infoWindowEntry){
return;
}
mInfoWindows.put(infoWindowEntry.getKey(), infoWindowEntry.getValue());
mBaiduMap.showInfoWindow(infoWindowEntry.getValue());
}
private AbstractMap.SimpleEntry<String, InfoWindow> MaptoInfoWindowEntry(Map<String, Object> infoWindowMap){
if(null == infoWindowMap){
return null;
}
if(!infoWindowMap.containsKey("id")){
if(Env.DEBUG){
Log.d(TAG, "argument does not contain");
}
return null;
}
final String id = new TypeConverter<String>().getValue(infoWindowMap, "id");
if(TextUtils.isEmpty(id)){
if(Env.DEBUG){
Log.d(TAG, "TextUtils.isEmpty(id)");
}
return null;
}
if(mInfoWindows.containsKey(id)){
if(Env.DEBUG){
Log.d(TAG, "infowindow already added");
}
return null;
}
String image = new TypeConverter<String>().getValue(infoWindowMap, "image");
if(TextUtils.isEmpty(image)){
if(Env.DEBUG){
Log.d(TAG, "TextUtils.isEmpty(image)");
}
return null;
}
Map<String, Object> latLngMap = new TypeConverter<Map<String, Object>>().getValue(infoWindowMap, "coordinate");
LatLng latLng = FlutterDataConveter.mapToLatlng(latLngMap);
if(null == latLng){
if(Env.DEBUG){
Log.d(TAG, "null == latLng");
}
return null;
}
Double yOffSet = new TypeConverter<Double>().getValue(infoWindowMap, "yOffset");
if(null == yOffSet){
if(Env.DEBUG){
Log.d(TAG, "null == yOffSet");
}
return null;
}
Boolean isAddWithBitmapDescriptor = new TypeConverter<Boolean>().getValue(infoWindowMap, "isAddWithBitmapDescriptor");
if(null == isAddWithBitmapDescriptor){
if(Env.DEBUG){
Log.d(TAG, "null == isAddWithBitmapDescriptor");
}
return null;
}
BitmapDescriptor bitmap = BitmapDescriptorFactory.fromAsset("flutter_assets/" + image);
if(null == bitmap){
if(Env.DEBUG){
Log.d(TAG, "null == bitmap");
}
return null;
}
mBitmapMap.put(id, bitmap);
InfoWindow infoWindow = new InfoWindow(bitmap, latLng, yOffSet.intValue(), new InfoWindow.OnInfoWindowClickListener(){
@Override
public void onInfoWindowClick() {
if(null == mMethodChannel){
return;
}
Map<String, Object> infoWindowMap = new HashMap<>();
infoWindowMap.put("id", id);
mMethodChannel.invokeMethod(InfoWindowProtocol.sMapDidClickedInfoWindowMethod, infoWindowMap);
}
} );
return new AbstractMap.SimpleEntry<String, InfoWindow>(id, infoWindow);
}
/**
* 批量添加infowindow
* @param call
* @param result
*/
private void addInfoWindows(MethodCall call, MethodChannel.Result result) {
if(Env.DEBUG){
Log.d(TAG, "addInfoWindows enter");
}
List<Object> arguments = (List<Object>)call.arguments;
if(null == arguments){
if(Env.DEBUG){
Log.d(TAG, "arguments is null");
}
return;
}
List<InfoWindow> infoWindowList = new ArrayList<>();
Iterator itr = arguments.iterator();
while (itr.hasNext()){
Map<String, Object> infoWindowMap = (Map<String, Object> )itr.next();
if(null == infoWindowMap){
continue;
}
AbstractMap.SimpleEntry<String,InfoWindow> infoWindowEntry = MaptoInfoWindowEntry(infoWindowMap);
if(null == infoWindowEntry){
continue;
}
infoWindowList.add(infoWindowEntry.getValue());
mInfoWindows.put(infoWindowEntry.getKey(), infoWindowEntry.getValue());
}
if(infoWindowList.size() > 0){
mBaiduMap.showInfoWindows(infoWindowList);
}
}
private void removeInfoWindow(MethodCall call, MethodChannel.Result result){
if(null == mBaiduMap){
return;
}
Map<String, Object> argument = call.arguments();
if(null == argument){
if(Env.DEBUG){
Log.d(TAG, "argument is null");
}
return;
}
String id = new TypeConverter<String>().getValue(argument, "id");
if(TextUtils.isEmpty(id)){
if(Env.DEBUG){
Log.d(TAG, "TextUtils.isEmpty(id)");
}
return;
}
InfoWindow infoWindow = mInfoWindows.get(id);
if(null == infoWindow){
if(Env.DEBUG){
Log.d(TAG, "null == infoWindow");
}
return;
}
if(Env.DEBUG){
Log.d(TAG, "removeInfoWindow success");
}
mBaiduMap.hideInfoWindow(infoWindow);
mInfoWindows.remove(id);
BitmapDescriptor bitmapDescriptor = mBitmapMap.get(id);
if(null != bitmapDescriptor){
bitmapDescriptor.recycle();
}
}
@Override
public void clean(){
super.clean();
Iterator iterator = mBitmapMap.values().iterator();
BitmapDescriptor bitmapDescriptor;
while (iterator.hasNext()){
bitmapDescriptor = (BitmapDescriptor)iterator.next();
if(null != bitmapDescriptor){
bitmapDescriptor.recycle();
}
}
if(null != mInfoWindows) {
mInfoWindows.clear();
}
}
}
@@ -0,0 +1,330 @@
package com.baidu.flutter_bmfmap.map.mapHandler;
import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import com.baidu.flutter_bmfmap.map.FlutterCommonMapView;
import com.baidu.flutter_bmfmap.utils.Constants;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MyLocationConfiguration;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.flutter_bmfmap.utils.Env;
import java.util.Map;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
public class LocationLayerHandler extends BMapHandler {
private BaiduMap mBaiduMap;
public LocationLayerHandler(FlutterCommonMapView mapView) {
super(mapView);
if (null != mapView) {
mBaiduMap = mapView.getBaiduMap();
}
}
@Override
public void updateMapView(FlutterCommonMapView mapView) {
super.updateMapView(mapView);
if (null != mapView) {
mBaiduMap = mapView.getBaiduMap();
}
}
@Override
public void handlerMethodCallResult(Context context, MethodCall call, MethodChannel.Result result) {
if (null == call) {
result.success(false);
return;
}
String methodId = call.method;
if (TextUtils.isEmpty(methodId)) {
result.success(false);
return;
}
switch (methodId) {
case Constants.LocationLayerMethodId.sMapShowUserLocationMethod:
setLocationEnabled(call, result);
break;
case Constants.LocationLayerMethodId.sMapUpdateLocationDataMethod:
setUpdateLocationData(call, result);
break;
case Constants.LocationLayerMethodId.sMapUserTrackingModeMethod:
setLoctype(call, result);
break;
case Constants.LocationLayerMethodId.sMapUpdateLocationDisplayParamMethod:
setCustomLocation(call, result);
break;
default:
break;
}
}
/**
* 自定义定位图层
*/
private void setCustomLocation(MethodCall call, MethodChannel.Result result) {
Map<String, Object> argument = call.arguments();
if (null == argument || null == mBaiduMap) {
result.success(false);
return;
}
if (!argument.containsKey("userlocationDisplayParam")) {
result.success(false);
return;
}
Map<String, Object> locationDisplayParam = (Map<String, Object>) argument.get("userlocationDisplayParam");
if (null == locationDisplayParam) {
result.success(false);
return;
}
if (!locationDisplayParam.containsKey("userTrackingMode")
|| !locationDisplayParam.containsKey("enableDirection")
|| !locationDisplayParam.containsKey("accuracyCircleStrokeColor")
|| !locationDisplayParam.containsKey("accuracyCircleFillColor")
|| !locationDisplayParam.containsKey("locationViewImage")
|| !locationDisplayParam.containsKey("locationViewHierarchy")) {
result.success(false);
return;
}
Integer userTrackingMode = (Integer) locationDisplayParam.get("userTrackingMode");
Boolean enableDirection = (Boolean) locationDisplayParam.get("enableDirection");
String locationViewImage = (String) locationDisplayParam.get("locationViewImage");
String accuracyCircleStrokeColor = (String) locationDisplayParam.get("accuracyCircleStrokeColor");
String accuracyCircleFillColor = (String) locationDisplayParam.get("accuracyCircleFillColor");
BitmapDescriptor bitmap = null;
if (!TextUtils.isEmpty(locationViewImage)) {
bitmap = BitmapDescriptorFactory.fromAsset("flutter_assets/" + locationViewImage);
}
int strokeColor = 0;
String color = "#";
if (!TextUtils.isEmpty(accuracyCircleStrokeColor)) {
strokeColor = Color.parseColor(color.concat(accuracyCircleStrokeColor));
}
int fillColor = 0;
if (!TextUtils.isEmpty(accuracyCircleFillColor)) {
fillColor = Color.parseColor(color.concat(accuracyCircleFillColor));
}
if (null != userTrackingMode && null != enableDirection && null != bitmap
&& strokeColor != 0 && fillColor != 0) {
switch (userTrackingMode) {
case Env.LocationMode.NORMAL:
case Env.LocationMode.MODEHEADING:
mBaiduMap.setMyLocationConfiguration(new MyLocationConfiguration(
MyLocationConfiguration.LocationMode.NORMAL, enableDirection,
bitmap,fillColor,strokeColor));
break;
case Env.LocationMode.FOLLOWING:
mBaiduMap.setMyLocationConfiguration(new MyLocationConfiguration(
MyLocationConfiguration.LocationMode.FOLLOWING, enableDirection,
bitmap,fillColor,strokeColor));
break;
case Env.LocationMode.COMPASS:
mBaiduMap.setMyLocationConfiguration(new MyLocationConfiguration(
MyLocationConfiguration.LocationMode.COMPASS, enableDirection,
bitmap,fillColor,strokeColor));
break;
default:
break;
}
} else if (null != userTrackingMode && null != enableDirection && strokeColor != 0 && fillColor != 0) {
switch (userTrackingMode) {
case Env.LocationMode.NORMAL:
case Env.LocationMode.MODEHEADING:
mBaiduMap.setMyLocationConfiguration(new MyLocationConfiguration(
MyLocationConfiguration.LocationMode.NORMAL, enableDirection,
null,fillColor,strokeColor));
break;
case Env.LocationMode.FOLLOWING:
mBaiduMap.setMyLocationConfiguration(new MyLocationConfiguration(
MyLocationConfiguration.LocationMode.FOLLOWING, enableDirection,
null,fillColor,strokeColor));
break;
case Env.LocationMode.COMPASS:
mBaiduMap.setMyLocationConfiguration(new MyLocationConfiguration(
MyLocationConfiguration.LocationMode.COMPASS, enableDirection,
null,fillColor,strokeColor));
break;
default:
break;
}
}
}
/**
* 设置定位模式
*/
private void setLoctype(MethodCall call, MethodChannel.Result result) {
Map<String, Object> argument = call.arguments();
if (null == argument || null == mBaiduMap) {
result.success(false);
return;
}
if (!argument.containsKey("userTrackingMode")
|| !argument.containsKey("enableDirection")
|| !argument.containsKey("customMarker")) {
result.success(false);
return;
}
Integer userTrackingMode = (Integer) argument.get("userTrackingMode");
Boolean enableDirection = (Boolean) argument.get("enableDirection");
String customMarker = (String) argument.get("customMarker");
BitmapDescriptor bitmap = BitmapDescriptorFactory.fromAsset("flutter_assets/" + customMarker);
if (null != userTrackingMode && null != enableDirection && null != bitmap) {
switch (userTrackingMode) {
case Env.LocationMode.NORMAL:
case Env.LocationMode.MODEHEADING:
mBaiduMap.setMyLocationConfiguration(new MyLocationConfiguration(
MyLocationConfiguration.LocationMode.NORMAL, enableDirection, bitmap));
break;
case Env.LocationMode.FOLLOWING:
mBaiduMap.setMyLocationConfiguration(new MyLocationConfiguration(
MyLocationConfiguration.LocationMode.FOLLOWING, enableDirection, bitmap));
break;
case Env.LocationMode.COMPASS:
mBaiduMap.setMyLocationConfiguration(new MyLocationConfiguration(
MyLocationConfiguration.LocationMode.COMPASS, enableDirection, bitmap));
break;
default:
break;
}
result.success(true);
} else if (null != userTrackingMode && null != enableDirection) {
switch (userTrackingMode) {
case Env.LocationMode.NORMAL:
case Env.LocationMode.MODEHEADING:
mBaiduMap.setMyLocationConfiguration(new MyLocationConfiguration(
MyLocationConfiguration.LocationMode.NORMAL, enableDirection, null));
break;
case Env.LocationMode.FOLLOWING:
mBaiduMap.setMyLocationConfiguration(new MyLocationConfiguration(
MyLocationConfiguration.LocationMode.FOLLOWING, enableDirection, null));
break;
case Env.LocationMode.COMPASS:
mBaiduMap.setMyLocationConfiguration(new MyLocationConfiguration(
MyLocationConfiguration.LocationMode.COMPASS, enableDirection, null));
break;
default:
break;
}
result.success(true);
}
result.success(false);
}
/**
* 定位数据
*/
private void setUpdateLocationData(MethodCall call, MethodChannel.Result result) {
Map<String, Object> argument = call.arguments();
MyLocationData.Builder builder = new MyLocationData.Builder();
if (null == argument || null == mBaiduMap) {
result.success(false);
return;
}
if (!argument.containsKey("userLocation")) {
result.success(false);
return;
}
Map<String,Object> userLocation = (Map<String, Object>) argument.get("userLocation");
if (null == userLocation) {
result.success(false);
return;
}
if (!userLocation.containsKey("location")) {
result.success(false);
return;
}
Map<String,Object> location = (Map<String, Object>) userLocation.get("location");
if (null == location) {
result.success(false);
return;
}
if (!location.containsKey("coordinate") || !location.containsKey("course")
|| !location.containsKey("speed") || !location.containsKey("accuracy")
|| !location.containsKey("satellitesNum")) {
result.success(false);
return;
}
Map<String,Double> coordinate = (Map<String, Double>) location.get("coordinate");
if (null != coordinate) {
if (coordinate.containsKey("latitude") && coordinate.containsKey("longitude")) {
Double latitude = coordinate.get("latitude");
Double longitude = coordinate.get("longitude");
if (null != latitude && null != longitude) {
builder.latitude(latitude);
builder.longitude(longitude);
}
}
}
Double course = (Double) location.get("course");
if (null != course) {
builder.accuracy(course.floatValue());
}
Double speed = (Double) location.get("speed");
if (null != speed) {
builder.speed(speed.floatValue());
}
Double accuracy = (Double) location.get("accuracy");
if (null != accuracy) {
builder.speed(accuracy.floatValue());
}
Integer satellitesNum = (Integer) location.get("satellitesNum");
if (null != satellitesNum) {
builder.satellitesNum(satellitesNum);
}
mBaiduMap.setMyLocationData(builder.build());
result.success(true);
}
/**
* 开启定位图层
*/
private void setLocationEnabled(MethodCall call, MethodChannel.Result result) {
Map<String, Object> argument = call.arguments();
if (null == argument || null == mBaiduMap) {
result.success(false);
return;
}
if (!argument.containsKey("show")) {
result.success(false);
return;
}
Boolean show = (Boolean) argument.get("show");
if (null == show) {
result.success(false);
return;
}
mBaiduMap.setMyLocationEnabled(show);
result.success(true);
}
}
@@ -0,0 +1,409 @@
package com.baidu.flutter_bmfmap.map.mapHandler;
import java.io.ByteArrayOutputStream;
import java.util.HashMap;
import java.util.Map;
import com.baidu.flutter_bmfmap.map.FlutterCommonMapView;
import com.baidu.flutter_bmfmap.map.FlutterMapView;
import com.baidu.flutter_bmfmap.map.MapStateUpdateImp;
import com.baidu.flutter_bmfmap.map.MapViewWrapper;
import com.baidu.flutter_bmfmap.utils.Constants;
import com.baidu.flutter_bmfmap.utils.converter.FlutterDataConveter;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.TextureMapView;
import com.baidu.mapapi.map.WinRound;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.model.LatLngBounds;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.text.TextUtils;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
public class MapStateHandler extends BMapHandler {
private static final String TAG = MapStateHandler.class.getSimpleName();
private BaiduMap mBaiduMap;
private String mViewType;
public MapStateHandler(FlutterCommonMapView mapView) {
super(mapView);
mViewType = mapView.getViewType();
mBaiduMap = mapView.getBaiduMap();
}
@Override
public void updateMapView(FlutterCommonMapView mapView) {
super.updateMapView(mapView);
if (null != mapView) {
mBaiduMap = mapView.getBaiduMap();
}
}
@Override
public void handlerMethodCallResult(Context context, MethodCall call,
MethodChannel.Result result) {
if (null == call) {
return;
}
String methodId = call.method;
if (TextUtils.isEmpty(methodId)) {
return;
}
switch (methodId) {
case Constants.MethodProtocol.MapStateProtocol.sMapUpdateMethod:
setMapUpdate(call, result);
break;
case Constants.MethodProtocol.MapStateProtocol.sMapTakeSnapshotMethod:
mapSnapshot(result);
break;
case Constants.MethodProtocol.MapStateProtocol.sMapTakeSnapshotWithRectMethod:
snapShotRect(call, result);
break;
case Constants.MethodProtocol.MapStateProtocol.sMapSetCompassImageMethod:
setCompassImage(call, result);
break;
case Constants.MethodProtocol.MapStateProtocol.sMapSetCustomTrafficColorMethod:
setCustomTrafficColor(call, result);
break;
case Constants.MethodProtocol.MapStateProtocol.sMapSetVisibleMapBoundsMethod:
setNewCoordinateBounds(call, result);
break;
case Constants.MethodProtocol.MapStateProtocol.sMapSetVisibleMapBoundsWithPaddingMethod:
setVisibleMapBoundsWithPaddingMethod(call, result);
break;
case Constants.MethodProtocol.MapStateProtocol.sMapDidUpdateWidget:
case Constants.MethodProtocol.MapStateProtocol.sMapReassemble:
resumeMap();
break;
default:
break;
}
}
/**
* 自定义路况颜色
*/
private void setCustomTrafficColor(MethodCall call, MethodChannel.Result result) {
Map<String, Object> argument = call.arguments();
if (null == argument || null == mBaiduMap) {
result.success(false);
return;
}
if (!argument.containsKey("smooth") || !argument.containsKey("slow")
|| !argument.containsKey("congestion") || !argument
.containsKey("severeCongestion")) {
result.success(false);
return;
}
String smooth = (String) argument.get("smooth");
String slow = (String) argument.get("slow");
String congestion = (String) argument.get("congestion");
String severeCongestion = (String) argument.get("severeCongestion");
if (smooth == null || slow == null || congestion == null || severeCongestion == null) {
result.success(false);
return;
}
String color = "#";
String severeCongestionColor = color.concat(severeCongestion);
String congestionColor = color.concat(congestion);
String slowColor = color.concat(slow);
String smoothColor = color.concat(smooth);
mBaiduMap.setCustomTrafficColor(severeCongestionColor, congestionColor, slowColor,
smoothColor);
result.success(true);
}
/**
* 设置罗盘图片
*/
private void setCompassImage(MethodCall call, MethodChannel.Result result) {
Map<String, Object> argument = call.arguments();
if (null == argument || null == mBaiduMap) {
result.success(false);
return;
}
if (!argument.containsKey("imagePath")) {
result.success(false);
return;
}
String imagePath = (String) argument.get("imagePath");
if (imagePath == null) {
result.success(false);
return;
}
BitmapDescriptor bitmapDescriptor =
BitmapDescriptorFactory.fromAsset("flutter_assets/" + imagePath);
Bitmap bitmap = bitmapDescriptor.getBitmap();
mBaiduMap.setCompassIcon(bitmap);
result.success(true);
}
/**
* 选取区域截图
*/
private void snapShotRect(MethodCall call, final MethodChannel.Result result) {
Map<String, Object> argument = call.arguments();
if (null == argument || null == mBaiduMap) {
result.success(null);
return;
}
if (!argument.containsKey("rect")) {
result.success(null);
return;
}
Map<String, Object> rect = (Map<String, Object>) argument.get("rect");
WinRound winRound = FlutterDataConveter.BMFRectToWinRound(rect);
if (null == winRound) {
result.success(null);
return;
}
if (winRound.left > winRound.right || winRound.top > winRound.bottom) {
result.success(null);
return;
}
if (winRound.right - winRound.left > getMapViewWidth()
|| winRound.bottom - winRound.top > getMapViewHeight()) {
result.success(null);
return;
}
// 矩形区域保证left <= right top <= bottom 否则截屏失败
Rect recta = new Rect(winRound.left, winRound.top, winRound.right, winRound.bottom);
mBaiduMap.snapshotScope(recta, new BaiduMap.SnapshotReadyCallback() {
@Override
public void onSnapshotReady(Bitmap bitmap) {
if (null == bitmap) {
result.success(null);
return;
}
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
result.success(byteArrayOutputStream.toByteArray());
}
});
}
private int getMapViewWidth() {
int width = 0;
switch (mViewType) {
case Constants.ViewType.sMapView:
MapView mapView = mMapView.getMapView();
width = null != mapView ? mapView.getWidth() : 0;
break;
case Constants.ViewType.sTextureMapView:
TextureMapView textureMapView = mMapView.getTextureMapView();
width = null != textureMapView ? textureMapView.getWidth() : 0;
break;
default:
break;
}
return width;
}
private int getMapViewHeight() {
int height = 0;
switch (mViewType) {
case Constants.ViewType.sMapView:
MapView mapView = mMapView.getMapView();
height = null != mapView ? mapView.getHeight() : 0;
break;
case Constants.ViewType.sTextureMapView:
TextureMapView textureMapView = mMapView.getTextureMapView();
height = null != textureMapView ? textureMapView.getHeight() : 0;
break;
default:
break;
}
return height;
}
/**
* 截图 全部地图展示区域
*/
private void mapSnapshot(final MethodChannel.Result result) {
if (null == mBaiduMap) {
result.success(null);
return;
}
mBaiduMap.snapshot(new BaiduMap.SnapshotReadyCallback() {
@Override
public void onSnapshotReady(Bitmap bitmap) {
if (null == bitmap) {
result.success(null);
return;
}
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
result.success(byteArrayOutputStream.toByteArray());
}
});
}
/**
* 更新地图
*/
private void setMapUpdate(MethodCall call, MethodChannel.Result result) {
Map<String, Object> argument = call.arguments();
if (null == argument || null == mBaiduMap) {
result.success(false);
return;
}
boolean ret = MapStateUpdateImp.getInstance()
.setCommView(mMapView)
.updateMapState(argument);
result.success(ret);
}
/**
* 设置显示在屏幕中的地图地理范围
*/
private void setNewCoordinateBounds(MethodCall call, MethodChannel.Result result) {
Map<String, Object> argument = call.arguments();
if (null == argument || null == mBaiduMap) {
result.success(false);
return;
}
if (!argument.containsKey("visibleMapBounds")) {
result.success(false);
return;
}
Map<String, Object> visibleMapBounds =
(Map<String, Object>) argument.get("visibleMapBounds");
if (null == visibleMapBounds) {
result.success(false);
return;
}
LatLngBounds latLngBounds = visibleMapBoundsImp(visibleMapBounds);
if (null == latLngBounds) {
result.success(false);
return;
}
mBaiduMap.setMapStatus(MapStatusUpdateFactory.newLatLngBounds(latLngBounds));
result.success(true);
}
private LatLngBounds visibleMapBoundsImp(Map<String, Object> visibleMapBounds) {
if (!visibleMapBounds.containsKey("northeast") || !visibleMapBounds
.containsKey("southwest")) {
return null;
}
HashMap<String, Double> northeast =
(HashMap<String, Double>) visibleMapBounds.get("northeast");
HashMap<String, Double> southwest =
(HashMap<String, Double>) visibleMapBounds.get("southwest");
if (null == northeast || null == southwest) {
return null;
}
if (!northeast.containsKey("latitude") || !northeast.containsKey("longitude")
|| !southwest.containsKey("latitude") || !southwest.containsKey("longitude")) {
return null;
}
Double northeastLatitude = northeast.get("latitude");
Double northeastLongitude = northeast.get("longitude");
Double southwestLatitude = southwest.get("latitude");
Double southwestLongitude = southwest.get("longitude");
if (null == northeastLatitude || null == northeastLongitude
|| null == southwestLatitude || null == southwestLongitude) {
return null;
}
LatLng northeastLatLng = new LatLng(northeastLatitude, northeastLongitude);
LatLng southwestLatLng = new LatLng(southwestLatitude, southwestLongitude);
LatLngBounds.Builder builder = new LatLngBounds.Builder();
builder.include(northeastLatLng);
builder.include(southwestLatLng);
return builder.build();
}
/**
* 根据Padding设置地理范围的合适缩放级别
*/
private void setVisibleMapBoundsWithPaddingMethod(MethodCall call,
MethodChannel.Result result) {
Map<String, Object> argument = call.arguments();
if (null == argument || null == mBaiduMap) {
result.success(false);
return;
}
if (!argument.containsKey("visibleMapBounds") || !argument.containsKey("insets")) {
result.success(false);
return;
}
Map<String, Object> visibleMapBounds =
(Map<String, Object>) argument.get("visibleMapBounds");
Map<String, Double> insets = (Map<String, Double>) argument.get("insets");
if (null == visibleMapBounds || null == insets) {
result.success(false);
return;
}
LatLngBounds latLngBounds = visibleMapBoundsImp(visibleMapBounds);
if (null == latLngBounds) {
result.success(false);
return;
}
if (!insets.containsKey("left") || !insets.containsKey("top")
|| !insets.containsKey("right") || !insets.containsKey("bottom")) {
result.success(false);
return;
}
Double left = insets.get("left");
Double top = insets.get("top");
Double right = insets.get("right");
Double bottom = insets.get("bottom");
if (null == left || null == top || null == right || null == bottom) {
result.success(false);
return;
}
MapStatusUpdate mapStatusUpdate = MapStatusUpdateFactory.newLatLngBounds(latLngBounds,
left.intValue(), top.intValue(), right.intValue(), bottom.intValue());
mBaiduMap.setMapStatus(mapStatusUpdate);
result.success(true);
}
private void updateMap() {
MapStatus.Builder builder = new MapStatus.Builder();
MapStatusUpdate mapStatusUpdate = MapStatusUpdateFactory.newMapStatus(builder.build());
mBaiduMap.setMapStatus(mapStatusUpdate);
}
private void resumeMap() {
MapViewWrapper mapViewWrapper = (MapViewWrapper) mMapView;
FlutterMapView flutterMapView = mapViewWrapper.getFlutterMapView();
flutterMapView.setResumeState(true);
}
}
@@ -0,0 +1,616 @@
package com.baidu.flutter_bmfmap.map.mapHandler;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import com.baidu.flutter_bmfmap.map.FlutterCommonMapView;
import com.baidu.flutter_bmfmap.map.mapHandler.BMapHandler;
import com.baidu.flutter_bmfmap.utils.Constants;
import com.baidu.flutter_bmfmap.utils.Env;
import com.baidu.flutter_bmfmap.utils.converter.FlutterDataConveter;
import com.baidu.flutter_bmfmap.utils.converter.TypeConverter;
import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.Overlay;
import com.baidu.mapapi.model.LatLng;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Point;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
public class MarkerHandler extends BMapHandler {
private static final String TAG = "MarkerHandler";
private HashMap<String, Overlay> mOverlayMap = new HashMap<>();
private HashMap<String, BitmapDescriptor> mMarkerBitmapMap = new HashMap<>();
private BaiduMap mBaiduMap;
public MarkerHandler(FlutterCommonMapView mapView) {
super(mapView);
if (null != mMapView) {
mBaiduMap = mMapView.getBaiduMap();
}
}
@Override
public void updateMapView(FlutterCommonMapView mapView) {
mMapView = mapView;
if (null != mMapView) {
mBaiduMap = mMapView.getBaiduMap();
}
}
@Override
public void clean() {
super.clean();
Iterator iterator = mMarkerBitmapMap.values().iterator();
BitmapDescriptor bitmapDescriptor;
while (iterator.hasNext()) {
bitmapDescriptor = (BitmapDescriptor) iterator.next();
if (null != bitmapDescriptor) {
bitmapDescriptor.recycle();
}
}
mMarkerBitmapMap.clear();
mOverlayMap.clear();
}
@Override
public void handlerMethodCallResult(Context context, MethodCall call,
MethodChannel.Result result) {
if (null == call) {
result.success(false);
return;
}
if (null == mBaiduMap) {
result.success(false);
return;
}
String methodId = call.method;
if (TextUtils.isEmpty(methodId)) {
result.success(false);
return;
}
boolean ret = false;
switch (methodId) {
case Constants.MethodProtocol.MarkerProtocol.sMapAddMarkerMethod:
ret = addMarker(call);
break;
case Constants.MethodProtocol.MarkerProtocol.sMapAddMarkersMethod:
ret = addMarkers(call);
break;
case Constants.MethodProtocol.MarkerProtocol.sMapRemoveMarkerMethod:
ret = removeMarker(call);
break;
case Constants.MethodProtocol.MarkerProtocol.sMapRemoveMarkersMethod:
ret = removeMarkers(call);
break;
case Constants.MethodProtocol.MarkerProtocol.sMapDidSelectMarkerMethod:
break;
case Constants.MethodProtocol.MarkerProtocol.sMapDidDeselectMarkerMethod:
break;
case Constants.MethodProtocol.MarkerProtocol.sMapCleanAllMarkersMethod:
ret = cleanAllMarker(call);
break;
case Constants.MethodProtocol.MarkerProtocol.sMapUpdateMarkerMemberMethod:
ret = updateMarkerMember(call, result);
break;
default:
break;
}
result.success(ret);
return;
}
private boolean addMarker(MethodCall call) {
Map<String, Object> argument = call.arguments();
if (null == argument) {
return false;
}
return addMarkerImp(argument);
}
private boolean addMarkerImp(Map<String, Object> argument) {
if (Env.DEBUG) {
Log.d(TAG, "addMarkerImp enter");
}
if (null == argument) {
return false;
}
if (!argument.containsKey("id")
|| !argument.containsKey("position")
|| !argument.containsKey("icon")) {
return false;
}
String id = new TypeConverter<String>().getValue(argument, "id");
if (TextUtils.isEmpty(id)) {
return false;
}
if (mOverlayMap.containsKey(id)) {
return false;
}
Map<String, Object> latlngMap = (Map<String, Object>) argument.get("position");
String title = (String) argument.get("title");
String subTitle = (String) argument.get("subtitle");
LatLng latLng = FlutterDataConveter.mapToLatlng(latlngMap);
if (null == latLng) {
if (Env.DEBUG) {
Log.d(TAG, "latLng is null");
}
return false;
}
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(latLng);
setScreenLockPoint(argument, markerOptions);
if (!setMarkerOptions(argument, markerOptions, id)) {
return false;
}
Overlay overlay = mBaiduMap.addOverlay(markerOptions);
Bundle bundle = new Bundle();
bundle.putString("id", id);
overlay.setExtraInfo(bundle);
mOverlayMap.put(id, overlay);
return true;
}
private boolean setScreenLockPoint(Map<String, Object> argumentMap,
MarkerOptions markerOptions) {
if (null == argumentMap || null == markerOptions) {
return false;
}
Boolean isLockedToScreen =
new TypeConverter<Boolean>().getValue(argumentMap, "isLockedToScreen");
if (null == isLockedToScreen || false == isLockedToScreen) {
return false;
}
Map<String, Object> screenPointToLockMap =
new TypeConverter<Map<String, Object>>().getValue(argumentMap, "screenPointToLock");
if (null == screenPointToLockMap
|| !screenPointToLockMap.containsKey("x")
|| !screenPointToLockMap.containsKey("y")) {
return false;
}
Double x = new TypeConverter<Double>().getValue(screenPointToLockMap, "x");
Double y = new TypeConverter<Double>().getValue(screenPointToLockMap, "y");
if (null == x || null == y) {
return false;
}
Point point = new Point(x.intValue(), y.intValue());
markerOptions.fixedScreenPosition(point);
return true;
}
/// Add Begin
/*
http://blog.csdn.net/alex_zhuang/article/details/7340901
对以下错误:
Java.lang.RuntimeException: java.lang.IllegalArgumentException: File /data/data/com.alex.datasave/files/user.txt contains a path separator
原先代码:
fis = this.context.openFileInput("/data/data/com.alex.datasave/files/user.txt");
// 正确代码:
File file = new File("/data/data/com.alex.datasave/files/user.txt");
fis = new FileInputStream(file); // 注意: 1.FileInputStream 与 openFileInput
*/
public BitmapDescriptor my_fromFile(String var0) {
// System.out.println("my_fromFile var0 = " + var0);
if (var0 != null && !var0.equals("")) {
Context var1 = BMapManager.getContext();
if (null == var1) {
return null;
} else {
try {
// FileInputStream var2 = var1.openFileInput(var0);
// System.out.print("my_fromFile : var0 = %s, var1 = %s", var0, var1);
// System.out.println("my_fromFile in var0 = " + var0);
// System.out.println("my_fromFile in var1 = " + var1);
// 正确代码:
File file = new File(var0);
FileInputStream var2 = new FileInputStream(file); // 注意: 1.FileInputStream 与 openFileInput
Bitmap var3 = BitmapFactory.decodeStream(var2);
var2.close();
if (var3 != null) {
BitmapDescriptor var4 = BitmapDescriptorFactory.fromBitmap(var3);
var3.recycle();
return var4;
}
} catch (FileNotFoundException var5) {
Log.e("my_fromFile", "FileNotFoundException happened", var5);
} catch (IOException var6) {
Log.e("my_fromFile", "IOException happened", var6);
}
return null;
}
} else {
return null;
}
}
/// Add End
/**
* 解析并设置markertions里的信息
*
* @return
*/
private boolean setMarkerOptions(Map<String, Object> markerOptionsMap,
MarkerOptions markerOptions, String id) {
//icon是必须的
String icon = new TypeConverter<String>().getValue(markerOptionsMap, "icon");
if (TextUtils.isEmpty(icon)) {
return false;
}
/// Add Begin
BitmapDescriptor bitmapDescriptor;
// System.out.println("my log:" + icon.substring(0, 1));
if (icon.substring(0, 1).equals("/")) {
// System.out.println("my log in");
// bitmapDescriptor = BitmapDescriptorFactory.fromAsset("flutter_assets/" + icon);
bitmapDescriptor = my_fromFile(icon);
} else {
bitmapDescriptor = BitmapDescriptorFactory.fromAsset("flutter_assets/" + icon);
}
/// Add End
// BitmapDescriptor bitmapDescriptor =
// BitmapDescriptorFactory.fromAsset("flutter_assets/" + icon);
if (null == bitmapDescriptor) {
return false;
}
markerOptions.icon(bitmapDescriptor);
mMarkerBitmapMap.put(id, bitmapDescriptor);
//centerOffset
Map<String, Object> centerOffset =
new TypeConverter<Map<String, Object>>().getValue(markerOptionsMap, "centerOffset");
if (null != centerOffset) {
Double y = new TypeConverter<Double>().getValue(centerOffset, "y");
if (null != y) {
markerOptions.yOffset(y.intValue());
}
}
Boolean enable = new TypeConverter<Boolean>().getValue(markerOptionsMap, "enabled");
if (markerOptionsMap.containsKey("enabled")) {
if (Env.DEBUG) {
Log.d(TAG, "enbale" + enable);
}
markerOptions.clickable(enable);
}
Boolean draggable = new TypeConverter<Boolean>().getValue(markerOptionsMap, "draggable");
if (null != draggable) {
markerOptions.draggable(draggable);
}
Integer zIndex = new TypeConverter<Integer>().getValue(markerOptionsMap, "zIndex");
if (null != zIndex) {
markerOptions.zIndex(zIndex);
}
Boolean visible = new TypeConverter<Boolean>().getValue(markerOptionsMap, "visible");
if (null != visible) {
markerOptions.visible(visible);
}
Double scaleX = new TypeConverter<Double>().getValue(markerOptionsMap, "scaleX");
if (null != scaleX) {
markerOptions.scaleX(scaleX.floatValue());
}
Double scaleY = new TypeConverter<Double>().getValue(markerOptionsMap, "scaleY");
if (null != scaleY) {
markerOptions.scaleX(scaleY.floatValue());
}
Double alpha = new TypeConverter<Double>().getValue(markerOptionsMap, "alpha");
if (null != alpha) {
markerOptions.alpha(alpha.floatValue());
}
Boolean isPerspective = new TypeConverter<Boolean>().getValue(markerOptionsMap, "isPerspective");
if (null != isPerspective) {
markerOptions.perspective(isPerspective);
}
return true;
}
private boolean addMarkers(MethodCall call) {
if (Env.DEBUG) {
Log.d(TAG, "addMarkers enter");
}
if (null == call) {
return false;
}
List<Object> arguments = call.arguments();
if (null == arguments) {
return false;
}
Iterator itr = arguments.iterator();
while (itr.hasNext()) {
Map<String, Object> argument = (Map<String, Object>) itr.next();
addMarkerImp(argument);
}
return true;
}
private boolean removeMarker(MethodCall call) {
Map<String, Object> argument = call.arguments();
if (null == argument) {
return false;
}
removeMarkerImp(argument);
return true;
}
private boolean removeMarkerImp(Map<String, Object> argument) {
String id = new TypeConverter<String>().getValue(argument, "id");
Overlay overlay = mOverlayMap.get(id);
BitmapDescriptor bitmapDescriptor = mMarkerBitmapMap.get(id);
boolean ret = true;
if (null != overlay) {
overlay.remove();
mOverlayMap.remove(id);
} else {
ret = false;
}
if (null != bitmapDescriptor) {
bitmapDescriptor.recycle();
mMarkerBitmapMap.remove(id);
} else {
ret = false;
}
return ret;
}
private boolean removeMarkers(MethodCall call) {
List<Object> markersList = call.arguments();
if (null == markersList) {
return false;
}
Iterator itr = markersList.iterator();
while (itr.hasNext()) {
Map<String, Object> marker = (Map<String, Object>) itr.next();
if (null != marker) {
removeMarkerImp(marker);
}
}
return true;
}
private boolean selectMarker(MethodCall call) {
return true;
}
private boolean deSelectMarker(MethodCall call) {
return true;
}
private boolean cleanAllMarker(MethodCall call) {
mBaiduMap.clear();
this.clean();
return true;
}
/**
* 更新marker属性
*
* @param call
* @param result
* @return
*/
private boolean updateMarkerMember(MethodCall call, MethodChannel.Result result) {
Map<String, Object> argument = call.arguments();
if (null == argument) {
return false;
}
String id = new TypeConverter<String>().getValue(argument, "id");
if (TextUtils.isEmpty(id)) {
return false;
}
if (!mMarkerBitmapMap.containsKey(id)) {
return false;
}
Marker marker = (Marker) mOverlayMap.get(id);
if (null == marker) {
return false;
}
String member = new TypeConverter<String>().getValue(argument, "member");
if (TextUtils.isEmpty(member)) {
return false;
}
Object value = argument.get("value");
if (null == value) {
return false;
}
boolean ret = false;
switch (member) {
case "title":
String titile = (String) value;
if (!TextUtils.isEmpty(titile)) {
marker.setTitle(titile);
ret = true;
}
break;
case "position":
Map<String, Object> position = (Map<String, Object>) value;
LatLng latLng = FlutterDataConveter.mapToLatlng(position);
if (null != latLng) {
marker.setPosition(latLng);
ret = true;
}
break;
case "isLockedToScreen":
Boolean isLockedToScreen = (Boolean) value;
if (null != isLockedToScreen && isLockedToScreen) {
Map<String, Object> pointMap =
new TypeConverter<Map<String, Object>>().getValue(argument,
"screenPointToLock");
Point point = FlutterDataConveter.mapToPoint(pointMap);
if (null != point) {
marker.setFixedScreenPosition(point);
ret = true;
}
}
break;
case "icon":
String icon = (String) value;
BitmapDescriptor bitmapDescriptor = mMarkerBitmapMap.get(id);
if (null != bitmapDescriptor) {
bitmapDescriptor.recycle();
}
bitmapDescriptor = BitmapDescriptorFactory.fromAsset("flutter_assets/" + icon);
if (null != bitmapDescriptor) {
marker.setIcon(bitmapDescriptor);
mMarkerBitmapMap.put(id, bitmapDescriptor);
ret = true;
}
break;
case "centerOffset":
Map<String, Object> centerOffset = (Map<String, Object>) value;
if (null != centerOffset) {
Double y = new TypeConverter<Double>().getValue(centerOffset, "y");
if (null != y) {
marker.setYOffset(y.intValue());
ret = true;
}
}
break;
case "enabled":
Boolean enabled = (Boolean) value;
if (null != enabled) {
marker.setClickable(enabled);
ret = true;
}
break;
case "draggable":
Boolean draggable = (Boolean) value;
if (null != draggable) {
marker.setDraggable(draggable);
ret = true;
}
break;
case "visible":
Boolean visible = (Boolean) value;
if (null != visible) {
marker.setVisible(visible);
ret = true;
}
break;
case "zIndex":
Integer zIndex = (Integer) value;
if (null != zIndex) {
marker.setZIndex(zIndex);
ret = true;
}
break;
case "scaleX":
Double scaleX = (Double) value;
if (null != scaleX) {
marker.setScaleX(scaleX.floatValue());
ret = true;
}
break;
case "scaleY":
Double scaleY = (Double) value;
if (null != scaleY) {
marker.setScaleY(scaleY.floatValue());
ret = true;
}
break;
case "alpha":
Double alpha = (Double) value;
if (null != alpha) {
marker.setAlpha(alpha.floatValue());
ret = true;
}
break;
case "isPerspective":
Boolean isPerspective = (Boolean) value;
if (null != isPerspective) {
marker.setPerspective(isPerspective);
ret = true;
}
break;
default:
break;
}
return ret;
}
}
@@ -0,0 +1,212 @@
package com.baidu.flutter_bmfmap.map.mapHandler;
import android.content.Context;
import android.graphics.Point;
import android.text.TextUtils;
import android.util.Log;
import com.baidu.flutter_bmfmap.map.FlutterCommonMapView;
import com.baidu.flutter_bmfmap.utils.Constants;
import com.baidu.flutter_bmfmap.utils.Env;
import com.baidu.flutter_bmfmap.utils.converter.FlutterDataConveter;
import com.baidu.mapapi.map.Projection;
import com.baidu.mapapi.model.LatLng;
import java.util.HashMap;
import java.util.Map;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import static com.baidu.flutter_bmfmap.utils.Constants.ErrorCode;
class ProjectionHandler extends BMapHandler {
private static final String TAG = "ProjectionHandler";
private Projection mProjection = null;
public ProjectionHandler(FlutterCommonMapView mapView) {
super(mapView);
if(null != mapView && null != mapView.getBaiduMap()){
mProjection = mapView.getBaiduMap().getProjection();
}
}
@Override
public void handlerMethodCallResult(Context context, MethodCall call, MethodChannel.Result result){
if(Env.DEBUG){
Log.d(TAG, "handlerMethodCallResult");
}
if (null == call) {
result.success(null);
return;
}
String methodId = call.method;
if (TextUtils.isEmpty(methodId)) {
if(Env.DEBUG){
Log.d(TAG, "methodId is null");
}
result.success(null);
return;
}
switch (methodId) {
case Constants.MethodProtocol.ProjectionMethodId.sFromScreenLocation:
fromScreenLocation(call, result);
break;
case Constants.MethodProtocol.ProjectionMethodId.sToScreenLocation:
toScreenLocation(call, result);
break;
default:
break;
}
}
/**
* 将屏幕坐标转换成地理坐标
*
* @param call
* @param result
* @return 地理坐标
*/
public boolean fromScreenLocation(MethodCall call, MethodChannel.Result result) {
if(Env.DEBUG){
Log.d(TAG, "fromScreenLocation enter");
}
Map<String, Object> argument = call.arguments();
if(null == argument){
if(Env.DEBUG){
Log.d(TAG, "argument is null");
}
result.error(String.valueOf(ErrorCode.sErrorNullFlutterParam)
, "MethodCall arguments is null"
,null);
return false;
}
Map<String, Object> pointMap = (Map<String, Object> )argument.get("point");
Point point = FlutterDataConveter.mapToPoint(pointMap);
if(null == point){
result.error(String.valueOf(ErrorCode.sErrorParamConvertFailed)
, "conver pointMap failed"
,null);
if(Env.DEBUG){
Log.d(TAG, "conver pointMap failed");
}
return false;
}
LatLng latLng = mProjection.fromScreenLocation(point);
if(null == latLng){
result.error(String.valueOf(ErrorCode.sErrorEngineError)
, "引擎调用失败"
,null);
if(Env.DEBUG){
Log.d(TAG, "fromScreenLocation failed");
}
return false;
}
final Map<String, Double> resultMap = FlutterDataConveter.latLngToMap(latLng);
if(Env.DEBUG){
Log.d(TAG, "handlerMethodCallResult success");
}
result.success(new HashMap<String, Object>(){
{
put("coordinate",resultMap);
}
});
return true;
}
/**
* 将地理坐标转换成屏幕坐标
*
* @return 屏幕坐标
*/
public boolean toScreenLocation(MethodCall call, MethodChannel.Result result) {
Map<String, Object> argument = call.arguments();
if(null == argument){
if(Env.DEBUG){
Log.d(TAG, "argument is null");
}
result.error(String.valueOf(ErrorCode.sErrorNullFlutterParam)
, "MethodCall arguments is null"
,null);
return false;
}
Map<String, Object> coordinateMap = (Map<String, Object> )argument.get("coordinate");
LatLng latLng = FlutterDataConveter.mapToLatlng(coordinateMap);
if(null == latLng){
result.error(String.valueOf(ErrorCode.sErrorParamConvertFailed)
, "MethodCall arguments is null"
,null);
if(Env.DEBUG){
Log.d(TAG, "null == latLng");
}
return false;
}
Point point = mProjection.toScreenLocation(latLng);
if(null == point){
result.error(String.valueOf(ErrorCode.sErrorEngineError)
, "MethodCall arguments is null"
,null);
if(Env.DEBUG){
Log.d(TAG, "null == point");
}
return false;
}
final Map<String, Double> pointMap = FlutterDataConveter.pointToMap(point);
if(Env.DEBUG){
Log.d(TAG, "toScreenLocation success");
}
result.success(new HashMap<String, Object>(){
{
put("point",pointMap);
}
});
return true;
}
// /**
// * 该方法把以米为计量单位的距离(沿赤道)在当前缩放水平下转换到一个以像素(水平)为计量单位的距离。 在默认的Mercator投影变换下,对于给定的距离,当远离赤道时,变换后确切的像素数量会增加。
// *
// * @param meters 以米为单位的距离
// * @return 相对给定距离的像素数量。在当前的缩放水平,如果沿赤道测量,返回值可能是个近似值
// */
// public float metersToEquatorPixels(MethodCall call, MethodChannel.Result result) {
// if (meters <= 0) {
// return 0;
// }
//
// return (float) (meters / (mBaseMap.getZoomUnitsInMeter()));
// }
public void updateMapView(FlutterCommonMapView mapView){
mMapView = mapView;
if(null != mapView && null != mapView.getBaiduMap()){
mProjection = mapView.getBaiduMap().getProjection();
}
}
}
@@ -0,0 +1,378 @@
package com.baidu.flutter_bmfmap.map.mapHandler;
import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import android.util.Log;
import com.baidu.flutter_bmfmap.map.FlutterCommonMapView;
import com.baidu.flutter_bmfmap.utils.Env;
import com.baidu.flutter_bmfmap.utils.IOStreamUtils;
import com.baidu.flutter_bmfmap.utils.converter.TypeConverter;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.FileTileProvider;
import com.baidu.mapapi.map.Projection;
import com.baidu.mapapi.map.Tile;
import com.baidu.mapapi.map.TileOverlay;
import com.baidu.mapapi.map.TileOverlayOptions;
import com.baidu.mapapi.map.TileProvider;
import com.baidu.mapapi.map.UrlTileProvider;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.Map;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import com.baidu.flutter_bmfmap.utils.Constants.MethodProtocol.TileMapProtocol;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.model.LatLngBounds;
class TileMapHandler extends BMapHandler {
private Tile mOfflineTile;
// 设置瓦片图的在线缓存大小,默认为20 M
private static final int TILE_TMP = 20 * 1024 * 1024;
private static final int MAX_LEVEL = 21;
private static final int MIN_LEVEL = 4;
private int mMaxLevel = MAX_LEVEL;
private int mMinLevel = MIN_LEVEL;
private int mTileTmp = TILE_TMP;
private String mUrlString;
private Projection mProjection;
private BaiduMap mBaiduMap;
private HashMap<String, TileOverlay> mTileOverlayMap = new HashMap<>();
public static class TileType{
public static final int URL_TILE_PROVIDER = 0;
public static final int FILE_TILE_PROVIDER_ASYNC = 1;
public static final int FILE_TILE_PROVIDER_SYNC = 2;
}
private static final String TAG = "TileMapHandler";
public TileMapHandler(FlutterCommonMapView mapView) {
super(mapView);
if(null != mapView){
mBaiduMap = mapView.getBaiduMap();
if(null != mBaiduMap){
mProjection = mBaiduMap.getProjection();
}
}
}
@Override
public void updateMapView(FlutterCommonMapView mapView){
mMapView = mapView;
if(null != mMapView){
mBaiduMap = mMapView.getBaiduMap();
if(null != mBaiduMap){
mProjection = mBaiduMap.getProjection();
}
}
}
@Override
public void handlerMethodCallResult(Context context, MethodCall call, MethodChannel.Result result) {
if(Env.DEBUG){
Log.d(TAG, "handlerMethodCallResult enter");
}
String methodID = call.method;
switch (methodID){
case TileMapProtocol.sAddTileMapMethod:
addTile(context, call, result);
break;
case TileMapProtocol.sRemoveTileMapMethod:
removeTile(context, call, result);
break;
default:
break;
}
}
private void addTile(Context context, MethodCall call, MethodChannel.Result result){
if(Env.DEBUG){
Log.d(TAG, "addTile enter");
}
if(null == mProjection || null == mBaiduMap){
if(Env.DEBUG){
Log.d(TAG, "null == mProjection || null == mBaiduMap");
}
result.success(false);
return;
}
Map<String, Object> argument = call.arguments();
if(null == argument){
if(Env.DEBUG){
Log.d(TAG, "argument is null");
}
result.success(false);
return;
}
TileOverlayOptions tileOverlayOptions = new TileOverlayOptions();
String id = new TypeConverter<String>().getValue(argument, "id");
if(null == id){
result.success(false);
return;
}
Integer maxZoom = new TypeConverter<Integer>().getValue(argument, "maxZoom");
if(null != maxZoom){
mMaxLevel = maxZoom.intValue();
if(Env.DEBUG) {
Log.d(TAG, "maxZoom:" + maxZoom);
}
}
Integer minZoom = new TypeConverter<Integer>().getValue(argument, "minZoom");
if(null != maxZoom){
mMinLevel = minZoom.intValue();
if(Env.DEBUG) {
Log.d(TAG, "minZoom:" + minZoom);
}
}
Integer maxTileTmp = new TypeConverter<Integer>().getValue(argument, "maxTileTmp");
if(null != maxTileTmp){
mTileTmp = maxTileTmp.intValue();
}
Map<String, Object> visibleMapBounds = new TypeConverter<Map<String, Object>>().getValue(argument,
"visibleMapBounds");
if(null == visibleMapBounds){
if(Env.DEBUG){
Log.d(TAG, "null == visibleMapBounds");
}
return;
}
LatLngBounds latLngBounds = visibleMapBoundsImp(visibleMapBounds);
if (null == latLngBounds) {
if(Env.DEBUG){
Log.d(TAG, "null == latLngBounds");
}
return;
}
tileOverlayOptions.setPositionFromBounds(latLngBounds);
TileProvider tileProvider = getTileProvider(context, argument);
if(null == tileProvider){
if(Env.DEBUG){
Log.d(TAG, "null == tileProvider");
}
result.success(false);
return;
}
tileOverlayOptions.tileProvider(tileProvider);
if(Env.DEBUG){
Log.d(TAG, "addTile success");
}
TileOverlay tileOverlay = mBaiduMap.addTileLayer(tileOverlayOptions);
mTileOverlayMap.put(id, tileOverlay);
result.success(true);
}
private LatLngBounds visibleMapBoundsImp(Map<String, Object> visibleMapBounds) {
if (!visibleMapBounds.containsKey("northeast") || !visibleMapBounds.containsKey("southwest")) {
return null;
}
HashMap<String,Double> northeast = (HashMap<String, Double>) visibleMapBounds.get("northeast");
HashMap<String,Double> southwest = (HashMap<String, Double>) visibleMapBounds.get("southwest");
if (null == northeast || null == southwest) {
return null;
}
if (!northeast.containsKey("latitude") || !northeast.containsKey("longitude")
|| !southwest.containsKey("latitude") || !southwest.containsKey("longitude")) {
return null;
}
Double northeastLatitude = northeast.get("latitude");
Double northeastLongitude = northeast.get("longitude");
Double southwestLatitude = southwest.get("latitude");
Double southwestLongitude = southwest.get("longitude");
if (null == northeastLatitude || null == northeastLongitude
|| null == southwestLatitude || null == southwestLongitude) {
return null;
}
LatLng northeastLatLng = new LatLng(northeastLatitude, northeastLongitude);
LatLng southwestLatLng = new LatLng(southwestLatitude, southwestLongitude);
LatLngBounds.Builder builder = new LatLngBounds.Builder();
builder.include(northeastLatLng);
builder.include(southwestLatLng);
return builder.build();
}
private TileProvider getTileProvider(Context context, Map<String, Object> tileProviderMap){
Integer tileType = new TypeConverter<Integer>().getValue(tileProviderMap, "tileLoadType");
if(null == tileType){
return null;
}
TileProvider tileProvider = null;
switch (tileType){
case TileType.FILE_TILE_PROVIDER_ASYNC:
case TileType.FILE_TILE_PROVIDER_SYNC:
tileProvider = getFileTileProvider(context);
break;
case TileType.URL_TILE_PROVIDER:
tileProvider = getUrlTileProvider(context, tileProviderMap);
break;
default:
break;
}
return tileProvider;
}
private TileProvider getFileTileProvider(final Context context){
TileProvider tileProvider = new FileTileProvider() {
@Override
public Tile getTile(int x, int y, int z) {
// 根据地图某一状态下x、y、z加载指定的瓦片图
String filedir = "flutter_assets/resoures/bmflocaltileimage/" + z + "/" + z + "_" + x + "_" + y + ".jpg";
// FlutterMain.getLookupKeyForAsset();
Bitmap bm = getFromAssets(context, filedir);
if (bm == null) {
return null;
}
// 瓦片图尺寸必须满足256 * 256
mOfflineTile = new Tile(bm.getWidth(), bm.getHeight(), toRawData(bm));
bm.recycle();
return mOfflineTile;
}
@Override
public int getMaxDisLevel() {
return 0;
}
@Override
public int getMinDisLevel() {
return 0;
}
};
return tileProvider;
}
private TileProvider getUrlTileProvider(Context context, Map<String, Object> tileProviderMap){
TileProvider tileProvider = null;
if(tileProviderMap.containsKey("url")){
Object urlObj = tileProviderMap.get("url");
if(null != urlObj){
mUrlString = (String)urlObj;
/*定义瓦片图的在线Provider,并实现相关接口
MAX_LEVEL、MIN_LEVEL 表示地图显示瓦片图的最大、最小级别
urlString 表示在线瓦片图的URL地址*/
tileProvider = new UrlTileProvider() {
@Override
public int getMaxDisLevel() {
return mMaxLevel;
}
@Override
public int getMinDisLevel() {
return mMinLevel;
}
@Override
public String getTileUrl() {
return mUrlString;
}
};
}
}
return tileProvider;
}
/**
* 瓦片文件解析为Bitmap
*
* @param fileName
* @return 瓦片文件的Bitmap
*/
public Bitmap getFromAssets(Context context, String fileName) {
AssetManager assetManager = context.getAssets();
InputStream inputStream = null;
Bitmap bitmap;
try {
if(Env.DEBUG){
Log.d(TAG, fileName);
}
inputStream = assetManager.open(fileName);
bitmap = BitmapFactory.decodeStream(inputStream);
return bitmap;
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
IOStreamUtils.closeSilently(inputStream);
}
}
/**
* 解析Bitmap
*
* @param bitmap
* @return
*/
byte[] toRawData(Bitmap bitmap) {
ByteBuffer buffer = ByteBuffer.allocate(bitmap.getWidth() * bitmap.getHeight() * 4);
bitmap.copyPixelsToBuffer(buffer);
byte[] data = buffer.array();
buffer.clear();
return data;
}
private void removeTile(Context context, MethodCall call, MethodChannel.Result result){
if(Env.DEBUG){
Log.d(TAG, "removeTile enter");
}
Map<String, Object> argument = call.arguments();
if(null == argument){
if(Env.DEBUG){
Log.d(TAG, "argument is null");
}
result.success(false);
return;
}
String id = new TypeConverter<String>().getValue(argument, "id");
if(TextUtils.isEmpty(id)){
result.success(false);
return;
}
TileOverlay tileOverlay = mTileOverlayMap.get(id);
if(null != tileOverlay){
if(Env.DEBUG){
Log.d(TAG, "remove tile success");
}
tileOverlay.removeTileOverlay();
result.success(true);
}else {
result.success(false);
}
}
}
@@ -0,0 +1,102 @@
package com.baidu.flutter_bmfmap.map.overlayHandler;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.baidu.flutter_bmfmap.utils.Env;
import com.baidu.flutter_bmfmap.utils.converter.FlutterDataConveter;
import com.baidu.mapapi.map.ArcOptions;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.Overlay;
import com.baidu.mapapi.model.LatLng;
import android.text.TextUtils;
import android.util.Log;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
public class ArcLineHandler extends OverlayHandler {
private static final String TAG = "ArcLineHandler";
public ArcLineHandler(BaiduMap baiduMap) {
super(baiduMap);
}
@Override
public Map<String, Overlay> handlerMethodCall(MethodCall call, MethodChannel.Result result) {
Map<String, Object> argument = call.arguments();
if (null == argument) {
return null;
}
if (!argument.containsKey("id")
|| !argument.containsKey("coordinates")) {
if (Env.DEBUG) {
Log.d(TAG, "argument does not contain");
}
return null;
}
ArcOptions arcOptions = new ArcOptions();
final String id = (String) argument.get("id");
if (TextUtils.isEmpty(id)) {
return null;
}
List<Map<String, Object>> coordinates =
(List<Map<String, Object>>) argument.get("coordinates");
if (coordinates.size() < 3) {
if (Env.DEBUG) {
Log.d(TAG, "atlngs.size() < 3");
}
return null;
}
LatLng latLngStart = FlutterDataConveter.mapToLatlng(coordinates.get(0));
LatLng latLngMiddle = FlutterDataConveter.mapToLatlng(coordinates.get(1));
LatLng latLngEnd = FlutterDataConveter.mapToLatlng(coordinates.get(2));
if (null == latLngStart
|| null == latLngMiddle
|| null == latLngEnd) {
if (Env.DEBUG) {
Log.d(TAG, "null == latLngStart\n" +
" || null == latLngMiddle\n" +
" || null == latLngEnd");
}
return null;
}
arcOptions.points(latLngStart, latLngMiddle, latLngEnd);
if (argument.containsKey("width")) {
int width = (Integer) argument.get("width");
arcOptions.width(width);
}
if (argument.containsKey("color")) {
String strokeColorStr = (String) argument.get("color");
int strokeColor = FlutterDataConveter.strColorToInteger(strokeColorStr);
arcOptions.color(strokeColor);
}
if (argument.containsKey("zIndex")) {
int zIndex = (Integer) argument.get("zIndex");
arcOptions.zIndex(zIndex);
}
if (argument.containsKey("visible")) {
boolean visible = (Boolean) argument.get("visible");
arcOptions.visible(visible);
}
final Overlay overlay = mBaiduMap.addOverlay(arcOptions);
return new HashMap<String, Overlay>() {
{
put(id, overlay);
}
};
}
}
@@ -0,0 +1,130 @@
package com.baidu.flutter_bmfmap.map.overlayHandler;
import java.util.HashMap;
import java.util.Map;
import com.baidu.flutter_bmfmap.utils.Env;
import com.baidu.flutter_bmfmap.utils.converter.FlutterDataConveter;
import com.baidu.flutter_bmfmap.utils.converter.TypeConverter;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.CircleDottedStrokeType;
import com.baidu.mapapi.map.CircleOptions;
import com.baidu.mapapi.map.Overlay;
import com.baidu.mapapi.map.Stroke;
import com.baidu.mapapi.model.LatLng;
import android.text.TextUtils;
import android.util.Log;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
public class CircleHandler extends OverlayHandler {
private static final String TAG = "CircleHandler";
public CircleHandler(BaiduMap baiduMap) {
super(baiduMap);
}
@Override
public Map<String, Overlay> handlerMethodCall(MethodCall call, MethodChannel.Result result) {
Map<String, Object> argument = call.arguments();
if (null == argument) {
if (Env.DEBUG) {
Log.d(TAG, "argument is null");
}
return null;
}
if (!argument.containsKey("id")
|| !argument.containsKey("center")
|| !argument.containsKey("radius")) {
if (Env.DEBUG) {
Log.d(TAG, "argument does not contain");
}
return null;
}
CircleOptions circleOptions = new CircleOptions();
final String id = new TypeConverter<String>().getValue(argument, "id");
if (TextUtils.isEmpty(id)) {
return null;
}
Map<String, Object> centerMap = (Map<String, Object>) argument.get("center");
LatLng center = FlutterDataConveter.mapToLatlng(centerMap);
if (null != center) {
circleOptions.center(center);
}
double radius = (Double) argument.get("radius");
circleOptions.radius((int) radius);
if (argument.containsKey("width") && argument.containsKey("strokeColor")) {
int width = (Integer) argument.get("width");
String strokeColorStr = (String) argument.get("strokeColor");
if (!TextUtils.isEmpty(strokeColorStr)) {
int strokeColor = FlutterDataConveter.strColorToInteger(strokeColorStr);
Stroke stroke = new Stroke(width, strokeColor);
circleOptions.stroke(stroke);
}
}
if (argument.containsKey("fillColor")) {
String fillColorStr = (String) argument.get("fillColor");
int fillColor = FlutterDataConveter.strColorToInteger(fillColorStr);
circleOptions.fillColor(fillColor);
}
if (argument.containsKey("zIndex")) {
int zIndex = (Integer) argument.get("zIndex");
circleOptions.zIndex(zIndex);
}
if (argument.containsKey("visible")) {
boolean visible = (Boolean) argument.get("visible");
circleOptions.visible(visible);
}
setLineDashType(argument, circleOptions);
final Overlay overlay = mBaiduMap.addOverlay(circleOptions);
return new HashMap<String, Overlay>() {
{
put(id, overlay);
}
};
}
private void setLineDashType(Map<String, Object> circleOptionsMap,
CircleOptions circleOptions) {
if (null == circleOptionsMap || null == circleOptions) {
return;
}
Integer lineDashType =
new TypeConverter<Integer>().getValue(circleOptionsMap, "lineDashType");
if (null == lineDashType) {
return;
}
switch (lineDashType) {
case OverlayCommon.LineDashType.sLineDashTypeNone:
circleOptions.dottedStroke(false);
break;
case OverlayCommon.LineDashType.sLineDashTypeSquare:
circleOptions.dottedStroke(true);
circleOptions.dottedStrokeType(CircleDottedStrokeType.DOTTED_LINE_SQUARE);
break;
case OverlayCommon.LineDashType.sLineDashTypeDot:
circleOptions.dottedStroke(true);
circleOptions.dottedStrokeType(CircleDottedStrokeType.DOTTED_LINE_CIRCLE);
break;
default:
break;
}
}
}
@@ -0,0 +1,99 @@
package com.baidu.flutter_bmfmap.map.overlayHandler;
import java.util.HashMap;
import java.util.Map;
import com.baidu.flutter_bmfmap.utils.Env;
import com.baidu.flutter_bmfmap.utils.converter.FlutterDataConveter;
import com.baidu.flutter_bmfmap.utils.converter.TypeConverter;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.DotOptions;
import com.baidu.mapapi.map.Overlay;
import com.baidu.mapapi.model.LatLng;
import android.text.TextUtils;
import android.util.Log;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
public class DotHandler extends OverlayHandler {
public static final String TAG = "DotHandler";
public DotHandler(BaiduMap baiduMap) {
super(baiduMap);
}
@Override
public Map<String, Overlay> handlerMethodCall(MethodCall call, MethodChannel.Result result) {
Map<String, Object> argument = call.arguments();
if (null == argument) {
if (Env.DEBUG) {
Log.d(TAG, "argument is null");
}
return null;
}
if (!argument.containsKey("id")) {
if (Env.DEBUG) {
Log.d(TAG, "argument does not contain" + argument.toString());
}
return null;
}
final String id = new TypeConverter<String>().getValue(argument, "id");
if (TextUtils.isEmpty(id)) {
return null;
}
DotOptions dotOptions = new DotOptions();
Map<String, Object> centerMap =
new TypeConverter<Map<String, Object>>().getValue(argument, "center");
LatLng center = FlutterDataConveter.mapToLatlng(centerMap);
if (null == center) {
if (Env.DEBUG) {
Log.d(TAG, "center is null");
}
return null;
}
dotOptions.center(center);
Double radius = new TypeConverter<Double>().getValue(argument, "radius");
if (null == radius) {
if (Env.DEBUG) {
Log.d(TAG, "radius is null");
}
return null;
}
dotOptions.radius(radius.intValue());
String colorStr = new TypeConverter<String>().getValue(argument, "color");
if (TextUtils.isEmpty(colorStr)) {
if (Env.DEBUG) {
Log.d(TAG, "colorStr is null");
}
return null;
}
int color = FlutterDataConveter.strColorToInteger(colorStr);
dotOptions.color(color);
Integer zIndex = new TypeConverter<Integer>().getValue(argument, "zIndex");
if (null != zIndex) {
dotOptions.zIndex(zIndex);
}
Boolean visible = new TypeConverter<Boolean>().getValue(argument, "visible");
if (null != visible) {
dotOptions.visible(visible);
}
final Overlay overlay = mBaiduMap.addOverlay(dotOptions);
return new HashMap<String, Overlay>() {
{
put(id, overlay);
}
};
}
}
@@ -0,0 +1,177 @@
package com.baidu.flutter_bmfmap.map.overlayHandler;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import com.baidu.flutter_bmfmap.utils.Env;
import com.baidu.flutter_bmfmap.utils.converter.FlutterDataConveter;
import com.baidu.flutter_bmfmap.utils.converter.TypeConverter;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.GroundOverlay;
import com.baidu.mapapi.map.GroundOverlayOptions;
import com.baidu.mapapi.map.Overlay;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.model.LatLngBounds;
import android.text.TextUtils;
import android.util.Log;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
class GroundHandler extends OverlayHandler {
private static final String TAG = "GroundHandler";
private HashMap<String, BitmapDescriptor> mBitmapMap = new HashMap<>();
public GroundHandler(BaiduMap baiduMap) {
super(baiduMap);
}
@Override
public Map<String, Overlay> handlerMethodCall(MethodCall call, MethodChannel.Result result) {
if (Env.DEBUG) {
Log.d(TAG, "handlerMethodCall enter");
}
Map<String, Object> argument = call.arguments();
if (null == argument) {
if (Env.DEBUG) {
Log.d(TAG, "argument is null");
}
return null;
}
if (!argument.containsKey("id")) {
if (Env.DEBUG) {
Log.d(TAG, "argument does not contain" + argument.toString());
}
return null;
}
final String id = new TypeConverter<String>().getValue(argument, "id");
if (TextUtils.isEmpty(id)) {
return null;
}
GroundOverlayOptions groundOverlayOptions = new GroundOverlayOptions();
setGroundOptions(id, argument, groundOverlayOptions);
final Overlay overlay = mBaiduMap.addOverlay(groundOverlayOptions);
return new HashMap<String, Overlay>() {
{
put(id, overlay);
}
};
}
/**
*
*/
private void setGroundOptions(String id, Map<String, Object> groundOptionsMap,
GroundOverlayOptions groundOverlayOptions) {
if (null == groundOptionsMap) {
return;
}
String image = new TypeConverter<String>().getValue(groundOptionsMap, "image");
if (!TextUtils.isEmpty(image)) {
BitmapDescriptor bitmap = BitmapDescriptorFactory.fromAsset("flutter_assets/" + image);
if (null != bitmap) {
if (Env.DEBUG) {
Log.d(TAG, "image");
}
groundOverlayOptions.image(bitmap);
mBitmapMap.put(id, bitmap);
}
}
Double anchorX = new TypeConverter<Double>().getValue(groundOptionsMap, "anchorX");
Double anchorY = new TypeConverter<Double>().getValue(groundOptionsMap, "anchorY");
if (null != anchorX && null != anchorY) {
groundOverlayOptions.anchor(anchorX.floatValue(), anchorY.floatValue());
}
Map<String, Object> centerMap =
new TypeConverter<Map<String, Object>>().getValue(groundOptionsMap, "position");
if (null != centerMap) {
LatLng center = FlutterDataConveter.mapToLatlng(centerMap);
if (null != center) {
if (Env.DEBUG) {
Log.d(TAG, "position");
}
groundOverlayOptions.position(center);
}
}
Double width = new TypeConverter<Double>().getValue(groundOptionsMap, "width");
Double height = new TypeConverter<Double>().getValue(groundOptionsMap, "height");
if(null != width && null != height){
groundOverlayOptions.dimensions(width.intValue(), height.intValue());
}
Map<String, Object> boundsMap =
new TypeConverter<Map<String, Object>>().getValue(groundOptionsMap, "bounds");
LatLngBounds latLngBounds = FlutterDataConveter.mapToLatlngBounds(boundsMap);
if (null != latLngBounds) {
if (Env.DEBUG) {
Log.d(TAG, "bounds");
}
groundOverlayOptions.positionFromBounds(latLngBounds);
}
Double transparency =
new TypeConverter<Double>().getValue(groundOptionsMap, "transparency");
if (null != transparency) {
groundOverlayOptions.transparency(transparency.floatValue());
}
Integer zIndex = new TypeConverter<Integer>().getValue(groundOptionsMap, "zIndex");
if (null != zIndex) {
groundOverlayOptions.zIndex(zIndex);
}
Boolean visible = new TypeConverter<Boolean>().getValue(groundOptionsMap, "visible");
if (null != visible) {
groundOverlayOptions.visible(visible);
}
}
public void clean(){
super.clean();
Iterator iterator = mBitmapMap.values().iterator();
BitmapDescriptor bitmapDescriptor;
while (iterator.hasNext()){
bitmapDescriptor = (BitmapDescriptor)iterator.next();
if(null != bitmapDescriptor){
bitmapDescriptor.recycle();
}
}
mBitmapMap.clear();
}
public void clean(String id) {
if (TextUtils.isEmpty(id)) {
return;
}
BitmapDescriptor bitmapDescriptor = mBitmapMap.get(id);
if (null != bitmapDescriptor) {
bitmapDescriptor.recycle();
}
mBitmapMap.remove(id);
}
}
@@ -0,0 +1,21 @@
package com.baidu.flutter_bmfmap.map.overlayHandler;
public class OverlayCommon{
public static class LineDashType{
/**
* 实折线
*/
public static final int sLineDashTypeNone = 0;
/**
* 方块样式
*/
public static final int sLineDashTypeSquare = 1;
/**
* 圆点样式
*/
public static final int sLineDashTypeDot = 2;
}
}
@@ -0,0 +1,43 @@
package com.baidu.flutter_bmfmap.map.overlayHandler;
import java.util.Map;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.Overlay;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
public abstract class OverlayHandler {
protected BaiduMap mBaiduMap;
protected Overlay mCurrentOverlay;
public OverlayHandler(BaiduMap baiduMap) {
this.mBaiduMap = baiduMap;
}
public abstract Map<String, Overlay> handlerMethodCall(MethodCall call,
MethodChannel.Result result);
public void updateBaiduMap(BaiduMap baiduMap) {
mBaiduMap = baiduMap;
}
public void setCurrentOverlay(Overlay overlay){
mCurrentOverlay = overlay;
}
/**
* 清理所有
*/
public void clean(){}
/**
* 清理指定id的overlay
* @param id
*/
public void clean(String id) {
}
}
@@ -0,0 +1,235 @@
package com.baidu.flutter_bmfmap.map.overlayHandler;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import com.baidu.flutter_bmfmap.utils.Constants.MethodProtocol.ArclineProtocol;
import com.baidu.flutter_bmfmap.utils.Constants.MethodProtocol.CirclelineProtocol;
import com.baidu.flutter_bmfmap.utils.Constants.MethodProtocol.DotProtocol;
import com.baidu.flutter_bmfmap.utils.Constants.MethodProtocol.GroundProtocol;
import com.baidu.flutter_bmfmap.utils.Constants.MethodProtocol.OverlayProtocol;
import com.baidu.flutter_bmfmap.utils.Constants.MethodProtocol.PolygonProtocol;
import com.baidu.flutter_bmfmap.utils.Constants.MethodProtocol.PolylineProtocol;
import com.baidu.flutter_bmfmap.utils.Constants.MethodProtocol.TextProtocol;
import com.baidu.flutter_bmfmap.utils.Constants.OverlayHandlerType;
import com.baidu.flutter_bmfmap.utils.Env;
import com.baidu.mapapi.map.Arc;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.Circle;
import com.baidu.mapapi.map.Dot;
import com.baidu.mapapi.map.GroundOverlay;
import com.baidu.mapapi.map.Overlay;
import com.baidu.mapapi.map.Polygon;
import com.baidu.mapapi.map.Polyline;
import com.baidu.mapapi.map.Text;
import android.util.Log;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
public class OverlayHandlerFactory {
private static final String TAG = "OverlayHandlerFactory";
private static volatile OverlayHandlerFactory sInstance;
private HashMap<Integer, OverlayHandler> overlayHandlerHashMap;
private OverlayManagerHandler mOverlayManagerHandler;
private OverlayHandlerFactory(BaiduMap baiduMap) {
init(baiduMap);
}
public static OverlayHandlerFactory getInstance(BaiduMap baiduMap) {
if (null == sInstance) {
synchronized(OverlayHandlerFactory.class) {
if (null == sInstance) {
sInstance = new OverlayHandlerFactory(baiduMap);
} else {
sInstance.updateBaiduMap(baiduMap);
}
}
} else {
sInstance.updateBaiduMap(baiduMap);
}
return sInstance;
}
private void updateBaiduMap(BaiduMap baiduMap) {
if (null == baiduMap) {
return;
}
if(null == overlayHandlerHashMap || overlayHandlerHashMap.isEmpty()){
init(baiduMap);
}
Iterator it = overlayHandlerHashMap.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<Integer, OverlayHandler> entry =
(Map.Entry<Integer, OverlayHandler>) it.next();
OverlayHandler overlayHandler = entry.getValue();
if (null != overlayHandler) {
overlayHandler.updateBaiduMap(baiduMap);
}
}
}
private void init(BaiduMap baiduMap) {
if (null == baiduMap) {
return;
}
mOverlayManagerHandler = new OverlayManagerHandler(baiduMap);
overlayHandlerHashMap = new HashMap<>();
overlayHandlerHashMap.put(OverlayHandlerType.CIRCLE_HANDLER, new CircleHandler(baiduMap));
overlayHandlerHashMap.put(OverlayHandlerType.DOT_HANDLER, new DotHandler(baiduMap));
overlayHandlerHashMap.put(OverlayHandlerType.POLYGON_HANDLER, new PolygonHandler(baiduMap));
overlayHandlerHashMap
.put(OverlayHandlerType.POLYLINE_HANDLER, new PolylineHandler(baiduMap));
overlayHandlerHashMap.put(OverlayHandlerType.TEXT_HANDLER, new TextHandler(baiduMap));
overlayHandlerHashMap.put(OverlayHandlerType.ARCLINE_HANDLER, new ArcLineHandler(baiduMap));
overlayHandlerHashMap.put(OverlayHandlerType.CIRCLE_HANDLER, new CircleHandler(baiduMap));
overlayHandlerHashMap.put(OverlayHandlerType.GROUND_HANDLER, new GroundHandler(baiduMap));
}
public boolean dispatchMethodHandler(MethodCall call, MethodChannel.Result result) {
if (null == call) {
if (Env.DEBUG) {
Log.d(TAG, "dispatchMethodHandler: null == call");
}
return false;
}
String methodId = call.method;
Log.d(TAG, "dispatchMethodHandler: " + methodId);
OverlayHandler overlayHandler = null;
Overlay overlay;
int handlerType = -1;
switch (methodId) {
case ArclineProtocol.sMapAddArclinelineMethod:
overlayHandler = overlayHandlerHashMap.get(OverlayHandlerType.ARCLINE_HANDLER);
break;
case PolygonProtocol.sMapAddPolygonMethod:
overlayHandler = overlayHandlerHashMap.get(OverlayHandlerType.POLYGON_HANDLER);
break;
case CirclelineProtocol.sMapAddCirclelineMethod:
overlayHandler = overlayHandlerHashMap.get(OverlayHandlerType.CIRCLE_HANDLER);
break;
case PolylineProtocol.sMapAddPolylineMethod:
overlayHandler = overlayHandlerHashMap.get(OverlayHandlerType.POLYLINE_HANDLER);
break;
case DotProtocol.sMapAddDotMethod:
overlayHandler = overlayHandlerHashMap.get(OverlayHandlerType.DOT_HANDLER);
break;
case TextProtocol.sMapAddTextMethod:
overlayHandler = overlayHandlerHashMap.get(OverlayHandlerType.TEXT_HANDLER);
break;
case GroundProtocol.sMapAddGroundMethod:
overlayHandler = overlayHandlerHashMap.get(OverlayHandlerType.GROUND_HANDLER);
break;
case OverlayProtocol.sMapRemoveOverlayMethod:
OverlayHandler specOverlayHandler = getCurrentOverlayHandler(call);
mOverlayManagerHandler.setCurrentOverlayHandler(specOverlayHandler);
overlayHandler = mOverlayManagerHandler;
break;
case PolylineProtocol.sMapUpdatePolylineMemberMethod:
overlayHandler = getCurrentOverlayHandler(call);
break;
default:
break;
}
if (null == overlayHandler) {
return false;
}
Map<String, Overlay> overlayMap = overlayHandler.handlerMethodCall(call, result);
if (null == overlayMap) {
return false;
}
mOverlayManagerHandler.addOverlay(overlayMap);
return true;
}
private Overlay getCurrentOverlay(MethodCall call) {
Map<String, Object> argument = call.arguments();
if (null == argument) {
if (Env.DEBUG) {
Log.d(TAG, "argument is null");
}
return null;
}
if (!argument.containsKey("id")) {
return null;
}
String id = (String) argument.get("id");
return mOverlayManagerHandler.getOverlay(id);
}
private int getHandlerType(Overlay overlay) {
int handlerType = -1;
if (overlay instanceof Polyline) {
handlerType = OverlayHandlerType.POLYLINE_HANDLER;
} else if (overlay instanceof Polygon) {
handlerType = OverlayHandlerType.POLYGON_HANDLER;
} else if (overlay instanceof Arc) {
handlerType = OverlayHandlerType.ARCLINE_HANDLER;
} else if (overlay instanceof Circle) {
handlerType = OverlayHandlerType.CIRCLE_HANDLER;
} else if (overlay instanceof Dot) {
handlerType = OverlayHandlerType.DOT_HANDLER;
} else if (overlay instanceof GroundOverlay) {
handlerType = OverlayHandlerType.GROUND_HANDLER;
} else if (overlay instanceof Text) {
handlerType = OverlayHandlerType.TEXT_HANDLER;
}
return handlerType;
}
public void clean(){
if(null == overlayHandlerHashMap || overlayHandlerHashMap.size() == 0) {
return;
}
OverlayHandler overlayHandler= null;
Iterator iterator = overlayHandlerHashMap.values().iterator();
while (iterator.hasNext()){
overlayHandler = (OverlayHandler) iterator.next();
if(null == overlayHandler){
continue;
}
overlayHandler.clean();
}
}
private OverlayHandler getCurrentOverlayHandler(MethodCall call) {
if (null == call) {
return null;
}
Overlay overlay = getCurrentOverlay(call);
if (null == overlay) {
return null;
}
int handlerType = getHandlerType(overlay);
OverlayHandler overlayHandler = overlayHandlerHashMap.get(handlerType);
if( null != overlayHandler) {
overlayHandler.setCurrentOverlay(overlay);
}
return overlayHandler;
}
}
@@ -0,0 +1,103 @@
package com.baidu.flutter_bmfmap.map.overlayHandler;
import java.util.HashMap;
import java.util.Map;
import com.baidu.flutter_bmfmap.utils.Constants.MethodProtocol.OverlayProtocol;
import com.baidu.flutter_bmfmap.utils.Env;
import com.baidu.flutter_bmfmap.utils.converter.TypeConverter;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.Overlay;
import android.text.TextUtils;
import android.util.Log;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
public class OverlayManagerHandler extends OverlayHandler {
private static final String TAG = "OverlayManagerHandler";
private HashMap<String, Overlay> mOverlayMap = new HashMap<>();
private OverlayHandler mCurrentOverlayHandler;
public OverlayManagerHandler(BaiduMap baiduMap) {
super(baiduMap);
}
@Override
public Map<String, Overlay> handlerMethodCall(MethodCall call, MethodChannel.Result result) {
if (Env.DEBUG) {
Log.d(TAG, "handlerMethodCall enter");
//result.success(false);
}
Map<String, Object> argument = call.arguments();
if (null == argument) {
if (Env.DEBUG) {
Log.d(TAG, "argument is null");
}
result.success(false);
return null;
}
boolean ret = false;
String methodId = call.method;
switch (methodId) {
case OverlayProtocol.sMapRemoveOverlayMethod:
ret = removeOverlay(argument);
break;
default:
break;
}
result.success(ret);
return null;
}
public void addOverlay(Map<String, Overlay> overlayMap) {
mOverlayMap.putAll(overlayMap);
}
public Overlay getOverlay(String id) {
return mOverlayMap.get(id);
}
public void setCurrentOverlayHandler(OverlayHandler overlayHandler) {
mCurrentOverlayHandler = overlayHandler;
}
/**
* 移除overlay
*
* @param argument
* @return
*/
private boolean removeOverlay(Map<String, Object> argument) {
String id = new TypeConverter<String>().getValue(argument, "id");
if (TextUtils.isEmpty(id)) {
return false;
}
Overlay overlay = mOverlayMap.get(id);
if (null == overlay) {
if (Env.DEBUG) {
Log.d(TAG, "not found overlay with id:" + id);
}
return false;
}
overlay.remove();
mOverlayMap.remove(id);
if(null != mCurrentOverlayHandler) {
mCurrentOverlayHandler.clean(id);
mCurrentOverlayHandler = null;
}
if (Env.DEBUG) {
Log.d(TAG, "remove Overlay success");
}
return true;
}
}
@@ -0,0 +1,117 @@
package com.baidu.flutter_bmfmap.map.overlayHandler;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.baidu.flutter_bmfmap.utils.Env;
import com.baidu.flutter_bmfmap.utils.converter.FlutterDataConveter;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.Overlay;
import com.baidu.mapapi.map.PolygonOptions;
import com.baidu.mapapi.map.Stroke;
import com.baidu.mapapi.model.LatLng;
import android.text.TextUtils;
import android.util.Log;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
public class PolygonHandler extends OverlayHandler {
private static final String TAG = "PolygonHandler";
public PolygonHandler(BaiduMap baiduMap) {
super(baiduMap);
}
@Override
public Map<String, Overlay> handlerMethodCall(MethodCall call, MethodChannel.Result result) {
if (Env.DEBUG) {
Log.d(TAG, "handlerMethodCall enter0");
}
Map<String, Object> argument = call.arguments();
if (null == argument) {
if (Env.DEBUG) {
Log.d(TAG, "argument is null");
}
return null;
}
if (!argument.containsKey("id")
|| !argument.containsKey("coordinates")) {
if (Env.DEBUG) {
Log.d(TAG, "argument does not contain");
}
return null;
}
final String id = (String) argument.get("id");
if (TextUtils.isEmpty(id)) {
return null;
}
List<Map<String, Double>> coordinates =
(List<Map<String, Double>>) argument.get("coordinates");
if (coordinates.size() < 1) {
if (Env.DEBUG) {
Log.d(TAG, "coordinates.size() < 1");
}
return null;
}
PolygonOptions polygonOptions = new PolygonOptions();
List<LatLng> coordinatesList = FlutterDataConveter.mapToLatlngs(coordinates);
if (null == coordinatesList) {
if (Env.DEBUG) {
Log.d(TAG, "coordinatesList is null");
}
return null;
}
polygonOptions.points(coordinatesList);
if (argument.containsKey("width") && argument
.containsKey("strokeColor")) {
int width = (Integer) argument.get("width");
String strokeColorStr = (String) argument.get("strokeColor");
if (Env.DEBUG) {
Log.d(TAG, "strokeColorStr:" + strokeColorStr);
}
if (!TextUtils.isEmpty(strokeColorStr)) {
int strokeColor = FlutterDataConveter.strColorToInteger(strokeColorStr);
Stroke stroke = new Stroke(width, strokeColor);
polygonOptions.stroke(stroke);
}
}
if (argument.containsKey("fillColor")) {
String fillColorStr = (String) argument.get("fillColor");
if (Env.DEBUG) {
Log.d(TAG, "fillColorStr:" + fillColorStr);
}
if (!TextUtils.isEmpty(fillColorStr)) {
int fillColor = FlutterDataConveter.strColorToInteger(fillColorStr);
polygonOptions.fillColor(fillColor);
}
}
if (argument.containsKey("zIndex")) {
int zIndex = (Integer) argument.get("zIndex");
polygonOptions.zIndex(zIndex);
}
if (argument.containsKey("visible")) {
boolean visible = (Boolean) argument.get("visible");
polygonOptions.visible(visible);
}
final Overlay overlay = mBaiduMap.addOverlay(polygonOptions);
return new HashMap<String, Overlay>() {
{
put(id, overlay);
}
};
}
}
@@ -0,0 +1,620 @@
package com.baidu.flutter_bmfmap.map.overlayHandler;
import java.text.BreakIterator;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import com.baidu.flutter_bmfmap.utils.Constants;
import com.baidu.flutter_bmfmap.utils.Env;
import com.baidu.flutter_bmfmap.utils.converter.FlutterDataConveter;
import com.baidu.flutter_bmfmap.utils.converter.TypeConverter;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BaiduMap.OnPolylineClickListener;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.Overlay;
import com.baidu.mapapi.map.Polyline;
import com.baidu.mapapi.map.PolylineDottedLineType;
import com.baidu.mapapi.map.PolylineOptions;
import com.baidu.mapapi.model.LatLng;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
public class PolylineHandler extends OverlayHandler {
private static final String TAG = "PolylineHandler";
private HashMap<String, List<BitmapDescriptor>> mBitmapMap = new HashMap<>();
private OnPolylineClickListener mOnPolylineClickListener = new OnPolylineClickListener() {
@Override
public boolean onPolylineClick(Polyline polyline) {
return false;
}
};
public PolylineHandler(BaiduMap baiduMap) {
super(baiduMap);
}
@Override
public Map<String, Overlay> handlerMethodCall(MethodCall call, MethodChannel.Result result) {
if (Env.DEBUG) {
Log.d(TAG, "handlerMethodCall enter");
}
Map<String, Object> argument = call.arguments();
if (null == argument) {
if (Env.DEBUG) {
Log.d(TAG, "argument is null");
}
return null;
}
String methodId = call.method;
Map<String, Overlay> overlayMap = null;
switch (methodId) {
case Constants.MethodProtocol.PolylineProtocol.sMapAddPolylineMethod:
overlayMap = addPolyLine(argument);
break;
case Constants.MethodProtocol.PolylineProtocol.sMapUpdatePolylineMemberMethod:
overlayMap = updateMember(argument);
break;
default:
break;
}
return overlayMap;
}
private Map<String, Overlay> addPolyLine(Map<String, Object> argument) {
if (!argument.containsKey("id")
|| !argument.containsKey("coordinates")
|| !argument.containsKey("indexs")) {
if (Env.DEBUG) {
Log.d(TAG, "argument does not contain");
}
return null;
}
final String id = new TypeConverter<String>().getValue(argument, "id");
if (TextUtils.isEmpty(id)) {
if (Env.DEBUG) {
Log.d(TAG, "id is null");
}
return null;
}
List<Map<String, Double>> coordinates =
new TypeConverter<List<Map<String, Double>>>().getValue(argument, "coordinates");
List<LatLng> latLngList = FlutterDataConveter.mapToLatlngs(coordinates);
if (null == latLngList) {
if (Env.DEBUG) {
Log.d(TAG, "latLngList is null");
}
return null;
}
PolylineOptions polylineOptions = new PolylineOptions().points(latLngList);
int pointNum = coordinates.size();
List<Integer> indexs = new TypeConverter<List<Integer>>().getValue(argument, "indexs");
setOptions(id, argument, polylineOptions, indexs, pointNum);
if (Env.DEBUG) {
Log.d(TAG, "addOverlay success");
}
final Polyline polyline = (Polyline) mBaiduMap.addOverlay(polylineOptions);
Bundle bundle = new Bundle();
bundle.putCharArray("id", id.toCharArray());
polyline.setExtraInfo(bundle);
if (null != polyline) {
mBaiduMap.setOnPolylineClickListener(mOnPolylineClickListener);
return new HashMap<String, Overlay>() {
{
put(id, polyline);
}
};
}
return null;
}
private void setOptions(String id, Map<String, Object> polylineOptionsMap,
PolylineOptions polylineOptions,
List<Integer> indexs,
int pointNumn) {
if (null == polylineOptionsMap || null == polylineOptions || null == indexs) {
return;
}
Integer width = new TypeConverter<Integer>().getValue(polylineOptionsMap, "width");
if (null != width) {
polylineOptions.width(width);
}
Boolean clickable = new TypeConverter<Boolean>().getValue(polylineOptionsMap, "clickable");
if (null != clickable) {
polylineOptions.clickable(clickable);
}
Boolean isKeepScale =
new TypeConverter<Boolean>().getValue(polylineOptionsMap, "isKeepScale");
if (null != isKeepScale) {
polylineOptions.keepScale(isKeepScale);
}
Boolean isFocus = new TypeConverter<Boolean>().getValue(polylineOptionsMap, "isFocus");
if (null != isFocus) {
polylineOptions.focus(isFocus);
}
Integer zIndex = new TypeConverter<Integer>().getValue(polylineOptionsMap, "zIndex");
if (null != zIndex) {
polylineOptions.zIndex(zIndex);
}
Boolean visible = new TypeConverter<Boolean>().getValue(polylineOptionsMap, "visible");
if (null != visible) {
polylineOptions.visible(visible);
}
Boolean isThined = new TypeConverter<Boolean>().getValue(polylineOptionsMap, "isThined");
if(null != isThined){
polylineOptions.isThined(isThined);
}
Boolean dottedLine = new TypeConverter<Boolean>().getValue(polylineOptionsMap, "dottedLine");
if (null != dottedLine) {
polylineOptions.dottedLine(dottedLine);
}
List<String> colors =
new TypeConverter<List<String>>().getValue(polylineOptionsMap, "colors");
if (null != colors && colors.size() > 0) {
List<Integer> intColors = FlutterDataConveter.getColors(colors);
if (null != intColors) {
if (intColors.size() == 1) {
polylineOptions.color(intColors.get(0));
} else {
List<Integer> correctColors = correctColors(indexs, intColors, pointNumn);
polylineOptions.colorsValues(correctColors);
}
}
}
/*
*colors和icons不能共存
*/
if (null == colors || colors.size() <= 0) {
List<String> icons =
new TypeConverter<List<String>>().getValue(polylineOptionsMap, "textures");
if (null != icons && icons.size() > 0) {
List<BitmapDescriptor> bitmapDescriptors = FlutterDataConveter.getIcons(icons);
if (null != bitmapDescriptors) {
if (bitmapDescriptors.size() == 1) {
polylineOptions.customTexture(bitmapDescriptors.get(0));
} else {
polylineOptions.textureIndex(indexs);
polylineOptions.customTextureList(bitmapDescriptors);
}
clearTextureBitMap(id);
mBitmapMap.put(id, bitmapDescriptors);
}
}
}
setLineDashType(polylineOptionsMap, polylineOptions);
}
/**
* android polyline多颜色只需要设置colors
* 但flutter传过来的colors只是一个颜色数组,没有索引的概念,需要根据indexs对其进行修正
* 正常情况indexs的数目应该等于pointNum -1,如果indexs小于次值,则余下段的索引按照索引数组最后一个补齐,反之则按照poinNum - 1处理
*/
private List<Integer> correctColors(List<Integer> indexs,
List<Integer> colors,
int pointNum) {
// 通过colors的size对索引数组进行修正
List<Integer> tmpIndexs = new ArrayList<>();
for (Integer i : indexs) {
if (i < colors.size()) {
tmpIndexs.add(i);
} else {
tmpIndexs.add(colors.size() - 1);
}
}
int tmpIndexSize = tmpIndexs.size();
int lastIndexValue = tmpIndexs.get(tmpIndexSize - 1);
// 通过pointNum对索引数组进行修正
if (tmpIndexSize < pointNum - 1) {
for (int i = tmpIndexSize; i < pointNum - 1; i++) {
tmpIndexs.add(lastIndexValue);
}
}
List<Integer> tmpColors = new ArrayList<>();
for (int i = 0; i < pointNum - 1; i++) {
tmpColors.add(colors.get(tmpIndexs.get(i)));
}
return tmpColors;
}
private void setLineDashType(Map<String, Object> polylineOptionsMap,
PolylineOptions polylineOptions) {
if (null == polylineOptionsMap || null == polylineOptions) {
return;
}
Integer lineDashType =
new TypeConverter<Integer>().getValue(polylineOptionsMap, "lineDashType");
if (null == lineDashType) {
return;
}
switch (lineDashType) {
case OverlayCommon.LineDashType.sLineDashTypeSquare:
polylineOptions.dottedLineType(PolylineDottedLineType.DOTTED_LINE_SQUARE);
break;
case OverlayCommon.LineDashType.sLineDashTypeDot:
polylineOptions.dottedLineType(PolylineDottedLineType.DOTTED_LINE_CIRCLE);
break;
default:
break;
}
}
/**
* 更新polyline属性
*
* @param argument
* @return
*/
private Map<String, Overlay> updateMember(Map<String, Object> argument) {
if (null == mCurrentOverlay || !(mCurrentOverlay instanceof Polyline)) {
return null;
}
final Polyline polyline = (Polyline) mCurrentOverlay;
final String id = new TypeConverter<String>().getValue(argument, "id");
if (TextUtils.isEmpty(id)) {
return null;
}
String member = new TypeConverter<String>().getValue(argument, "member");
if (TextUtils.isEmpty(member)) {
return null;
}
switch (member) {
case "coordinates":
if (!updateCoordinates(argument, polyline)) {
return null;
}
break;
case "width":
Integer width = new TypeConverter<Integer>().getValue(argument, "value");
if (null == width) {
return null;
}
polyline.setWidth(width);
break;
case "indexs":
if (!updateIndexs(argument, polyline)) {
return null;
}
break;
case "colors":
if (!updateColors(argument, polyline)) {
return null;
}
break;
case "textures":
if (!updateTextures(argument, polyline)) {
return null;
}
break;
case "lineDashType":
if (!updateLinashType(argument, polyline)) {
return null;
}
break;
case "lineCapType":
case "lineJoinType":
return null;
case "clickable":
Boolean clickable = new TypeConverter<Boolean>().getValue(argument, "value");
if (null == clickable) {
return null;
}
polyline.setClickable(clickable);
break;
case "isKeepScale":
Boolean isKeepScale = new TypeConverter<Boolean>().getValue(argument, "value");
if (null == isKeepScale) {
return null;
}
polyline.setIsKeepScale(isKeepScale);
break;
case "isFocus":
Boolean isFocus = new TypeConverter<Boolean>().getValue(argument, "value");
if (null == isFocus) {
return null;
}
polyline.setFocus(isFocus);
break;
case "visible":
Boolean visible = new TypeConverter<Boolean>().getValue(argument, "value");
if (null == visible) {
return null;
}
polyline.setVisible(visible);
break;
case "zIndex":
Integer zIndex = new TypeConverter<Integer>().getValue(argument, "value");
if (null == zIndex) {
return null;
}
polyline.setZIndex(zIndex);
break;
case "isThined":
Boolean isThined = new TypeConverter<Boolean>().getValue(argument, "value");
if(null != isThined){
polyline.setThined(isThined);
}
break;
case "dottedLine":
Boolean dottedLine = new TypeConverter<Boolean>().getValue(argument, "value");
if (null != dottedLine) {
polyline.setDottedLine(dottedLine);
}
break;
default:
break;
}
return new HashMap<String, Overlay>() {
{
put(id, polyline);
}
};
}
private boolean updateCoordinates(Map<String, Object> argument, Polyline polyline) {
List<Map<String, Double>> coordinates =
new TypeConverter<List<Map<String, Double>>>().getValue(argument,
"value");
if (null == coordinates) {
return false;
}
List<LatLng> latLngList = FlutterDataConveter.mapToLatlngs(coordinates);
if (null == latLngList) {
return false;
}
polyline.setPoints(latLngList);
List<Integer> indexs = new TypeConverter<List<Integer>>().getValue(argument, "indexs");
if (null != indexs) {
int[] nIndexs = new int[indexs.size()];
for (int i = 0; i < indexs.size(); i++) {
nIndexs[i] = indexs.get(i);
}
polyline.setIndexs(nIndexs);
}
return true;
}
private boolean updateIndexs(Map<String, Object> argument, Polyline polyline) {
List<Integer> indexs = new TypeConverter<List<Integer>>().getValue(argument, "value");
if (null == indexs) {
return false;
}
int[] nIndexs = new int[indexs.size()];
for (int i = 0; i < indexs.size(); i++) {
nIndexs[i] = indexs.get(i);
}
polyline.setIndexs(nIndexs);
List<LatLng> points = polyline.getPoints();
if (null != points) {
polyline.setPoints(points);
}
return true;
}
private boolean updateColors(Map<String, Object> argument, Polyline polyline) {
boolean ret = false;
List<String> colors =
new TypeConverter<List<String>>().getValue(argument, "value");
List<Integer> indexs =
new TypeConverter<List<Integer>>().getValue(argument, "indexs");
List<LatLng> points = polyline.getPoints();
if (null != colors &&
colors.size() > 0 &&
null != indexs &&
indexs.size() > 0 &&
null != points &&
points.size() > 0) {
List<Integer> intColors = FlutterDataConveter.getColors(colors);
List<Integer> correctColors = correctColors(indexs, intColors, points.size());
if (null != correctColors) {
if (correctColors.size() == 1) {
polyline.setColor(correctColors.get(0));
ret = true;
} else {
int[] nColors = new int[correctColors.size()];
for (int i = 0; i < correctColors.size(); i++) {
nColors[i] = correctColors.get(i);
}
polyline.setColorList(nColors);
ret = true;
}
polyline.setPoints(points);
}
}
return ret;
}
private boolean updateTextures(Map<String, Object> argument, Polyline polyline) {
List<String> icons =
new TypeConverter<List<String>>().getValue(argument, "value");
if (null == icons) {
return false;
}
boolean ret = false;
if (null != icons && icons.size() > 0) {
List<BitmapDescriptor> bitmapDescriptors = FlutterDataConveter.getIcons(icons);
if (null != bitmapDescriptors) {
if (bitmapDescriptors.size() == 1) {
polyline.setTexture(bitmapDescriptors.get(0));
ret = true;
} else {
polyline.setTextureList(bitmapDescriptors);
ret = true;
}
List<LatLng> points = polyline.getPoints();
if (null != points) {
polyline.setPoints(points);
}
Bundle bundle = polyline.getExtraInfo();
String id = bundle.getString("id");
clearTextureBitMap(id);
mBitmapMap.put(id, bitmapDescriptors);
}
}
return ret;
}
private boolean updateLinashType(Map<String, Object> argument, Polyline polyline) {
Integer lineDashType = new TypeConverter<Integer>().getValue(argument, "value");
if (null == lineDashType) {
return false;
}
switch (lineDashType) {
case OverlayCommon.LineDashType.sLineDashTypeNone:
break;
case OverlayCommon.LineDashType.sLineDashTypeSquare:
polyline.setDottedLineType(PolylineDottedLineType.DOTTED_LINE_SQUARE);
break;
case OverlayCommon.LineDashType.sLineDashTypeDot:
polyline.setDottedLineType(PolylineDottedLineType.DOTTED_LINE_CIRCLE);
break;
default:
break;
}
return true;
}
private void clearTextureBitMap(String id) {
if (TextUtils.isEmpty(id)) {
return;
}
List<BitmapDescriptor> bitmapDescriptors = mBitmapMap.get(id);
if (null == bitmapDescriptors) {
return;
}
Iterator itr = bitmapDescriptors.iterator();
BitmapDescriptor bitmapDescriptor;
while (itr.hasNext()) {
bitmapDescriptor = (BitmapDescriptor) itr.next();
if (null == bitmapDescriptor) {
continue;
}
bitmapDescriptor.recycle();
}
mBitmapMap.remove(id);
}
public void clean(){
Iterator itr = mBitmapMap.values().iterator();
List<BitmapDescriptor> bitmapDescriptors;
BitmapDescriptor bitmapDescriptor;
while (itr.hasNext()) {
bitmapDescriptors = (List<BitmapDescriptor>) itr.next();
if (null == bitmapDescriptors) {
continue;
}
Iterator listItr = bitmapDescriptors.iterator();
while (listItr.hasNext()) {
bitmapDescriptor = (BitmapDescriptor)listItr.next();
if (null == bitmapDescriptor) {
continue;
}
bitmapDescriptor.recycle();
}
}
mBitmapMap.clear();
}
public void clean(String id) {
if (TextUtils.isEmpty(id)) {
return;
}
List<BitmapDescriptor> bitmapDescriptors = mBitmapMap.get(id);
if (null == bitmapDescriptors) {
return;
}
Iterator itr = bitmapDescriptors.iterator();
BitmapDescriptor bitmapDescriptor;
while (itr.hasNext()) {
bitmapDescriptor = (BitmapDescriptor)itr.next();
if (null == bitmapDescriptor) {
continue;
}
bitmapDescriptor.recycle();
}
mBitmapMap.remove(id);
}
}
@@ -0,0 +1,137 @@
package com.baidu.flutter_bmfmap.map.overlayHandler;
import java.util.HashMap;
import java.util.Map;
import com.baidu.flutter_bmfmap.utils.Env;
import com.baidu.flutter_bmfmap.utils.converter.FlutterDataConveter;
import com.baidu.flutter_bmfmap.utils.converter.TypeConverter;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.Overlay;
import com.baidu.mapapi.map.TextOptions;
import com.baidu.mapapi.model.LatLng;
import android.graphics.Typeface;
import android.text.TextUtils;
import android.util.Log;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
public class TextHandler extends OverlayHandler {
private static final String TAG = "TextHandler";
public TextHandler(BaiduMap baiduMap) {
super(baiduMap);
}
@Override
public Map<String, Overlay> handlerMethodCall(MethodCall call, MethodChannel.Result result) {
Map<String, Object> argument = call.arguments();
if (null == argument) {
if (Env.DEBUG) {
Log.d(TAG, "argument is null");
}
return null;
}
if (!argument.containsKey("id")
|| !argument.containsKey("text")
|| !argument.containsKey("position")) {
if (Env.DEBUG) {
Log.d(TAG, "argument does not contain" + argument.toString());
}
return null;
}
final String id = new TypeConverter<String>().getValue(argument, "id");
if (TextUtils.isEmpty(id)) {
return null;
}
TextOptions textOptions = new TextOptions();
Object posObj = (argument.get("position"));
if (null != posObj) {
Map<String, Object> posMap = (Map<String, Object>) posObj;
LatLng pos = FlutterDataConveter.mapToLatlng(posMap);
if (null != pos) {
if (Env.DEBUG) {
Log.d(TAG, "pos");
}
textOptions.position(pos);
}
}
String text = new TypeConverter<String>().getValue(argument, "text");
if (TextUtils.isEmpty(text)) {
return null;
}
textOptions.text(text);
setTextOptions(argument, textOptions);
final Overlay overlay = mBaiduMap.addOverlay(textOptions);
return new HashMap<String, Overlay>() {
{
put(id, overlay);
}
};
}
private void setTextOptions(Map<String, Object> textOptionsMap, TextOptions textOptions) {
if (null == textOptionsMap || null == textOptions) {
return;
}
String bgColorStr = new TypeConverter<String>().getValue(textOptionsMap, "bgColor");
if (!TextUtils.isEmpty(bgColorStr)) {
int bgColor = FlutterDataConveter.strColorToInteger(bgColorStr);
textOptions.bgColor(bgColor);
}
String fongColorStr = new TypeConverter<String>().getValue(textOptionsMap, "fontColor");
if (!TextUtils.isEmpty(fongColorStr)) {
int fontColor = FlutterDataConveter.strColorToInteger(fongColorStr);
textOptions.fontColor(fontColor);
}
Integer fontSize = new TypeConverter<Integer>().getValue(textOptionsMap, "fontSize");
if (null != fontSize) {
textOptions.fontSize(fontSize);
}
Integer alignx = new TypeConverter<Integer>().getValue(textOptionsMap, "alignX");
Integer aligny = new TypeConverter<Integer>().getValue(textOptionsMap, "alignY");
if (null != alignx && null != aligny) {
textOptions.align(alignx, aligny);
}
Double roate = new TypeConverter<Double>().getValue(textOptionsMap, "rotate");
if (null != roate) {
textOptions.rotate(roate.floatValue());
}
Integer zIndex = new TypeConverter<Integer>().getValue(textOptionsMap, "zIndex");
if (null != zIndex) {
textOptions.zIndex(zIndex);
}
Boolean visible = new TypeConverter<Boolean>().getValue(textOptionsMap, "visible");
if (null != visible) {
textOptions.visible(visible);
}
Map<String, Object> typeFaceMap =
new TypeConverter<Map<String, Object>>().getValue(textOptionsMap, "typeFace");
if (null != typeFaceMap) {
String familyName = new TypeConverter<String>().getValue(typeFaceMap, "familyName");
Integer textStype = new TypeConverter<Integer>().getValue(typeFaceMap, "textStype");
if (!TextUtils.isEmpty(familyName) && textStype >= 0 && textStype <= 4) {
Typeface typeface = Typeface.create(familyName, textStype);
textOptions.typeface(typeface);
}
}
}
}
@@ -0,0 +1,623 @@
package com.baidu.flutter_bmfmap.utils;
public class Constants {
public static final String VIEW_METHOD_CHANNEL_PREFIX = "flutter_bmfmap/map_";
public static final String VIEW_EVENT_CHANNEL_PREFIX = "flutter_bmfmap/event_";
public static final String sConfigChangedAction = "com.baidu.flutter_bmfmap.configChanged";
/**
* flutter widget update 或者 热重载导致的 FlutterMapView 和FlutterTextureMapView 被调用的次数
*/
public static final int MAX_GET_VIEW_CNT_BY_FLUTTER_RESIZE = 3;
/**
* view类型
*/
public static class ViewType{
public static final String sMapView = "flutter_bmfmap/map/BMKMapView";
public static final String sTextureMapView = "flutter_bmfmap/map/BMKTextureMapView";
}
/**
* overlayHandler类型
*/
public static class OverlayHandlerType{
public static final int CIRCLE_HANDLER = 0;
public static final int DOT_HANDLER = 1;
public static final int POLYGON_HANDLER = 2;
public static final int POLYLINE_HANDLER = 3;
public static final int TEXT_HANDLER = 4;
public static final int ARCLINE_HANDLER = 5;
public static final int GROUND_HANDLER = 6;
}
/**
* MapHandler类型
*/
public static class BMapHandlerType{
public static final int CUSTOM_MAP = 0;
public static final int MAP_STATE = 1;
public static final int INDOOR_MAP = 2;
public static final int MAP_SNAPSHOT = 3;
public static final int CUSTOM_COMPASS = 4;
public static final int CUSTOM_TRAFFIC_COLOR = 5;
public static final int MAP_UPDATE = 6;
public static final int HEAT_MAP = 7;
public static final int TILE_MAP = 8;
public static final int INFOWINDOW_HANDLER = 9;
public static final int MARKER_HANDLER = 10;
public static final int LOCATION_LAYER = 11;
public static final int PROJECTION = 12;
}
/**
* 与flutter method协议约定
*/
public static class MethodProtocol {
/**
* 室内图状态协议
*/
public static class IndoorMapProtocol {
/**
* map展示室内地图
*/
public static final String sShowBaseIndoorMapMethod = "flutter_bmfmap/map/showBaseIndoorMap";
/**
* map室内图标注是否显示
*/
public static final String sShowBaseIndoorMapPoiMethod = "flutter_bmfmap/map/showBaseIndoorMapPoi";
/**
* map设置室内图楼层
*/
public static final String sSwitchBaseIndoorMapFloorMethod = "flutter_bmfmap/map/switchBaseIndoorMapFloor";
/**
* map获取当前聚焦的室内图信息
*/
public static final String sGetFocusedBaseIndoorMapInfoMethod= "flutter_bmfmap/map/getFocusedBaseIndoorMapInfo";
}
/**
* 个性化地图
*/
public static class CustomMapProtocol {
/**
* 开启个性化地图
*/
public static final String sMapSetCustomMapStyleEnableMethod = "flutter_bmfmap/map/setCustomMapStyleEnable";
/**
* 设置个性化地图样式路径
*/
public static final String sMapSetCustomMapStylePathMethod = "flutter_bmfmap/map/setCustomMapStylePath";
/**
* 在线个性化样式加载状态回调接口
*/
public static final String sMapSetCustomMapStyleWithOptionMethod = "flutter_bmfmap/map/setCustomMapStyleWithOption";
}
/**
* overlay协议
*/
public static class OverlayProtocol{
/**
* 删除overlay
*/
public static final String sMapRemoveOverlayMethod = "flutter_bmfmap/overlay/removeOverlay";
}
/**
* marker协议
*/
public static class MarkerProtocol {
/**
* 添加marker
*/
public static final String sMapAddMarkerMethod = "flutter_bmfmap/marker/addMarker";
/**
* 添加markers
*/
public static final String sMapAddMarkersMethod = "flutter_bmfmap/marker/addMarkers";
/**
* 删除marker
*/
public static final String sMapRemoveMarkerMethod = "flutter_bmfmap/marker/removeMarker";
/**
* 删除markers
*/
public static final String sMapRemoveMarkersMethod = "flutter_bmfmap/marker/removeMarkers";
/**
* 清除所有的markers
*/
public static final String sMapCleanAllMarkersMethod = "flutter_bmfmap/marker/cleanAllMarkers";
/**
* marker点击回调
*/
public static final String sMapClickedmarkedMethod = "flutter_bmfmap/marker/clickedMarker";
/**
* marker 选中回调
*/
public static final String sMapDidSelectMarkerMethod = "flutter_bmfmap/marker/didSelectedMarker";
/**
* marker取消选中回调
*/
public static final String sMapDidDeselectMarkerMethod = "flutter_bmfmap/marker/didDeselectMarker";
/**
* marker拖拽
*/
public static final String sMapDragMarkerMethod = "flutter_bmfmap/marker/dragMarker";
/**
* marker拖拽
*/
public static final String sMapUpdateMarkerMemberMethod = "flutter_bmfmap/marker/updateMarkerMember";
/**
* marker拖拽状态
*/
public static class MarkerDragState{
/**
* 开始拖拽
*/
public static final String sDragStart = "dragStart";
/**
* 正在拖拽
*/
public static final String sDragging = "dragging";
/**
* 拖拽完成
*/
public static final String sDragEnd = "dragEnd";
}
}
/**
* infowindow协议
*/
public static class InfoWindowProtocol {
/**
* marker的infoWindow(iOS paopaoView)点击回调
*/
public static final String sMapDidClickedInfoWindowMethod = "flutter_bmfmap/map/didClickedInfoWindow";
// 添加infoWindow
public static final String sAddInfoWindowMapMethod = "flutter_bmfmap/map/addInfoWindow";
// 添加infoWindow
public static final String sAddInfoWindowsMapMethod = "flutter_bmfmap/map/addInfoWindows";
// 移除infoWindow
public static final String sRemoveInfoWindowMapMethod = "flutter_bmfmap/map/removeInfoWindow";
}
/**
* polyline协议
*/
public static class PolylineProtocol {
/**
* 添加polyline
*/
public static final String sMapAddPolylineMethod = "flutter_bmfmap/overlay/addPolyline";
/**
* polyline点击事件
*/
public static final String sMapOnClickedOverlayCallback = "flutter_bmfmap/overlay/onClickedOverlay";
/**
* 更新polyline属
*/
public static final String sMapUpdatePolylineMemberMethod = "flutter_bmfmap/overlay/updatePolylineMember";
}
/**
* polygon协议
*/
public static class PolygonProtocol {
/**
* 添加polyline
*/
public static final String sMapAddPolygonMethod = "flutter_bmfmap/overlay/addPolygon";
}
/**
* arline协议
*/
public static class ArclineProtocol {
/**
* 添加arcline
*/
public static final String sMapAddArclinelineMethod = "flutter_bmfmap/overlay/addArcline";
}
/**
* circleline协议
*/
public static class CirclelineProtocol {
/**
* 添加circlr
*/
public static final String sMapAddCirclelineMethod = "flutter_bmfmap/overlay/addCircle";
}
/**
* dot协议
*/
public static class DotProtocol {
/**
* 添加Dot
*/
public static final String sMapAddDotMethod = "flutter_bmfmap/overlay/addDot";
}
/**
* text协议
*/
public static class TextProtocol {
/**
* 添加text
*/
// 添加dot
public static final String sMapAddTextMethod = "flutter_bmfmap/overlay/addText";
}
/**
* dot协议
*/
public static class GroundProtocol {
/**
* 添加Ground
*/
public static final String sMapAddGroundMethod = "flutter_bmfmap/overlay/addGround";
}
public static class HeatMapProtocol {
/**
* 添加HeapMap
*/
public static final String sMapAddHeatMapMethod = "flutter_bmfmap/heatMap/addHeatMap";
/**
* 开关
*/
public static final String sMapRemoveHeatMapMethod = "flutter_bmfmap/heatMap/removeHeatMap";
/**
* 是否展示热力图
*/
public static final String sShowHeatMapMethod = "flutter_bmfmap/heatMap/showHeatMap";
}
/**
* mapState协议
*/
public static class MapStateProtocol {
// 更新地图参数
public static final String sMapUpdateMethod = "flutter_bmfmap/map/updateMapOptions";
// map放大一级比例尺
public static final String sMapZoomInMethod = "flutter_bmfmap/map/zoomIn";
// map缩小一级比例尺
public static final String sMapZoomOutMethod = "flutter_bmfmap/map/zoomOut";
// 设置路况颜色
public static final String sMapSetCustomTrafficColorMethod =
"flutter_bmfmap/map/setCustomTrafficColor";
// 更新地图状态
public static final String sMapSetMapStatusMethod = "flutter_bmfmap/map/setMapStatus";
// 获取地图状态
public static final String sMapGetMapStatusMethod = "flutter_bmfmap/map/getMapStatus";
// 按像素移动地图中心点
public static final String sMapSetScrollByMethod = "flutter_bmfmap/map/setScrollBy";
// 根据给定增量缩放地图级别
public static final String sMapSetZoomByMethod = "flutter_bmfmap/map/setZoomBy";
// 根据给定增量以及给定的屏幕坐标缩放地图级别
public static final String sMapSetZoomPointByMethod = "flutter_bmfmap/map/setZoomPointBy";
// 设置地图缩放级别
public static final String sMapSetZoomToMethod = "flutter_bmfmap/map/setZoomTo";
// 设定地图中心点坐标
public static final String sMapSetCenterCoordinateMethod =
"flutter_bmfmap/map/setCenterCoordinate";
// 设置地图中心点以及缩放级别
public static final String sMapSetCenterZoomMethod = "flutter_bmfmap/map/setMapCenterZoom";
// 获得地图当前可视区域截图
public static final String sMapTakeSnapshotMethod = "flutter_bmfmap/map/takeSnapshot";
// 获得地图指定区域截图
public static final String sMapTakeSnapshotWithRectMethod =
"flutter_bmfmap/map/takeSnapshotWithRect";
// 设置罗盘的图片
public static final String sMapSetCompassImageMethod = "flutter_bmfmap/map/setCompassImage";
// 设置显示在屏幕中的地图地理范围
public static final String sMapSetVisibleMapBoundsMethod = "flutter_bmfmap/map/setVisibleMapBounds";
// 设定地图的显示范围,并使mapRect四周保留insets指定的边界区域
public static final String sMapSetVisibleMapBoundsWithPaddingMethod =
"flutter_bmfmap/map/setVisibleMapBoundsWithPadding";
// map加载完成
public static final String sMapDidLoadCallback = "flutter_bmfmap/map/mapViewDidFinishLoad";
// map渲染完成
public static final String sMapDidFinishRenderCallback =
"flutter_bmfmap/map/mapViewDidFinishRender";
// 地图渲染每一帧画面过程中,以及每次需要重绘地图时(例如添加覆盖物)都会调用此接口
public static final String sMapOnDrawMapFrameCallback =
"flutter_bmfmap/map/mapViewOnDrawMapFrame";
// 地图绘制出有效数据的监听
public static final String sMapRenderValidDataCallback = "flutter_bmfmap/map/mapRenderValidDataCallback";
// 地图View进入/移出室内图
public static final String sMapInOrOutBaseIndoorMapCallback =
"flutter_bmfmap/map/mapViewInOrOutBaseIndoorMap";
// 地图区域即将改变时会调用此接口
public static final String sMapRegionWillChangeCallback =
"flutter_bmfmap/map/mapViewRegionWillChange";
// 地图区域即将改变时会调用此接口reason
public static final String sMapRegionWillChangeWithReasonCallback =
"flutter_bmfmap/map/mapViewRegionWillChangeWithReason";
// 地图区域改变完成后会调用此接口
public static final String sMapRegionDidChangeCallback =
"flutter_bmfmap/map/mapViewRegionDidChange";
// 地图区域改变完成后会调用此接口reason
public static final String sMapRegionDidChangeWithReasonCallback =
"flutter_bmfmap/map/mapViewRegionDidChangeWithReason";
// 点中底图空白处会回调此接口
public static final String sMapOnClickedMapBlankCallback =
"flutter_bmfmap/map/mapViewOnClickedMapBlank";
// 点中底图标注后会回调此接口
public static final String sMapOnClickedMapPoiCallback =
"flutter_bmfmap/map/mapViewonClickedMapPoi";
// 双击地图时会回调此接口
public static final String sMapOnDoubleClickCallback =
"flutter_bmfmap/map/mapViewOnDoubleClick";
// 长按地图时会回调此接口
public static final String sMapOnLongClickCallback =
"flutter_bmfmap/map/mapViewOnLongClick";
// 地图状态改变完成后会调用此接口
public static final String sMapStatusDidChangedCallback =
"flutter_bmfmap/map/mapViewStatusDidChanged";
// widget 状态更新
public static final String sMapDidUpdateWidget = "flutter_bmfmap/map/didUpdateWidget";
// widget 热重载
public static final String sMapReassemble = "flutter_bmfmap/map/reassemble";
}
/**
* 地图获取属性方法id集合
*/
public static class BMFMapGetPropertyMethodId {
// 获取map的展示类型
public static final String sMapGetMapTypeMethod = "flutter_bmfmap/map/getMapType";
// 获取map的比例尺级别
public static final String sMapGetZoomLevelMethod = "flutter_bmfmap/map/getZoomLevel";
// 获取map的自定义最小比例尺级别
public static final String sMapGetMinZoomLevelMethod = "flutter_bmfmap/map/getMinZoomLevel";
// 获取map的自定义最大比例尺级别
public static final String sMapGetMaxZoomLevelMethod = "flutter_bmfmap/map/getMaxZoomLevel";
// 获取map的旋转角度
public static final String sMapGetRotationMethod = "flutter_bmfmap/map/getRotation";
// 获取map的地图俯视角度
public static final String sMapGetOverlookingMethod = "flutter_bmfmap/map/getOverlooking";
// 获取map的是否现显示3D楼块效果
public static final String sMapGetBuildingsEnabledMethod = "flutter_bmfmap/map/getBuildingsEnabled";
// 获取map的是否打开路况图层
public static final String sMapGetTrafficEnabledMethod = "flutter_bmfmap/map/getTrafficEnabled";
// 获取map的是否打开百度城市热力图图层
public static final String sMapGetBaiduHeatMapEnabledMethod = "flutter_bmfmap/map/getBaiduHeatMapEnabled";
// 获取map的是否支持所有手势操作
public static final String sMapGetGesturesEnabledMethod = "flutter_bmfmap/map/getGesturesEnabled";
// 获取map是否支持缩放
public static final String sMapGetZoomEnabledMethod = "flutter_bmfmap/map/getZoomEnabled";
// 获取map是否支持拖拽手势
public static final String sMapGetScrollEnabledMethod = "flutter_bmfmap/map/getScrollEnabled";
// 获取map是否支持俯仰角
public static final String sMapGetOverlookEnabledMethod = "flutter_bmfmap/map/getOverlookEnabled";
// 获取map是否支持旋转
public static final String sMapGetRotateEnabledMethod = "flutter_bmfmap/map/getRotateEnabled";
// 获取map的比例尺的位置
public static final String sMapGetMapScaleBarPositionMethod = "flutter_bmfmap/map/getMapScaleBarPosition";
// 获取map的logo位置
public static final String sMapGetLogoPositionMethod = "flutter_bmfmap/map/getLogoPosition";
// 获取map的可视范围
public static final String sMapGetVisibleMapBoundsMethod = "flutter_bmfmap/map/getVisibleMapBounds";
// 获取map的显示室内图
public static final String sMapGetBaseIndoorMapEnabledMethod = "flutter_bmfmap/map/getBaseIndoorMapEnabled";
// 获取map的室内图标注是否显示
public static final String sMapGetShowIndoorMapPoiMethod = "flutter_bmfmap/map"
+ "/getShowIndoorMapPoi";
}
public static class BMFOfflineMethodId {
// 初使化
public static final String sMapInitOfflineMethod = "flutter_bmfmap/offlineMap/initOfflineMap";
// 状态回调
public static final String sMapOfflineCallBackMethod = "flutter_bmfmap/offlineMap/offlineCallBack";
// 启动下载指定城市ID的离线地图,或在暂停更新某城市后继续更新下载某城市离线地图
public static final String sMapStartOfflineMethod = "flutter_bmfmap/offlineMap/startOfflineMap";
// 启动更新指定城市ID的离线地图
public static final String sMapUpdateOfflineMethod = "flutter_bmfmap/offlineMap/updateOfflineMap";
// 暂停下载或更新指定城市ID的离线地图
public static final String sMapPauseOfflineMethod = "flutter_bmfmap/offlineMap/pauseOfflineMap";
// 删除指定城市ID的离线地图
public static final String sMapRemoveOfflineMethod = "flutter_bmfmap/offlineMap/removeOfflineMap";
// 销毁离线地图管理模块,不用时调用
public static final String sMapDestroyOfflineMethod = "flutter_bmfmap/offlineMap/destroyOfflineMap";
// 返回热门城市列表
public static final String sMapGetHotCityListMethod = "flutter_bmfmap/offlineMap/getHotCityList";
// 返回支持离线地图城市列表
public static final String sMapGetOfflineCityListMethod = "flutter_bmfmap/offlineMap/getOfflineCityList";
// 根据城市名搜索该城市离线地图记录
public static final String sMapSearchCityMethod = "flutter_bmfmap/offlineMap/searchCityList";
// 返回各城市离线地图更新信息
public static final String sMapGetAllUpdateInfoMethod = "flutter_bmfmap/offlineMap/getAllUpdateInfo";
// 返回指定城市ID离线地图更新信息
public static final String sMapGetUpdateInfoMethod = "flutter_bmfmap/offlineMap/getUpdateInfo";
}
public static class ProjectionMethodId {
//屏幕坐标转地理坐标ID
public static final String sFromScreenLocation = "flutter_bmfmap/projection/screenPointfromCoordinate";
//将地理坐标转换成屏幕坐标
public static final String sToScreenLocation = "flutter_bmfmap/projection/coordinateFromScreenPoint";
//米为计量单位的距离(沿赤道)在当前缩放水平下转换到一个以像素(水平)为计量单位的距离
public static final String sMetersToEquatorPixels = "flutter_bmfmap/map/metersToEquatorPixels";
}
public static class TileMapProtocol {
// 添加室内地图
public static final String sAddTileMapMethod = "flutter_bmfmap/overlay/addTile";
// 展示室内地图
public static final String sRemoveTileMapMethod = "flutter_bmfmap/overlay/removeTile";
}
}
public static class ErrorCode{
/**
* 没有错误
*/
public static final int sErrorNon = 0;
/**
* flutter传递参数为空
*/
public static final int sErrorNullFlutterParam = 1;
/**
* flutter参数缺少指定内容
*/
public static final int sErrorFlutterParamMissingContent = 2;
/**
* flutter参数类型不对
*/
public static final int sErrorFlutterParamType= 3;
/**
* flutter参数转换出错
*/
public static final int sErrorParamConvertFailed= 4;
/**
* flutter参数转换出错
*/
public static final int sErrorEngineError= 5;
}
// 定位图层
public static class LocationLayerMethodId {
// 设定是否显示定位图层
public static final String sMapShowUserLocationMethod =
"flutter_bmfmap/userLocation/showUserLocation";
// 设定定位模式,取值为:BMFUserTrackingMode
public static final String sMapUserTrackingModeMethod =
"flutter_bmfmap/userLocation/userTrackingMode";
// 动态定制我的位置样式
public static final String sMapUpdateLocationDisplayParamMethod =
"flutter_bmfmap/userLocation/updateLocationDisplayParam";
// 动态更新我的位置数据
public static final String sMapUpdateLocationDataMethod =
"flutter_bmfmap/userLocation/updateLocationData";
}
/**
* 枚举:室内图切换楼层结果
*/
public class SwitchIndoorFloorError {
/** 切换楼层成功 */
public static final int SUCCESS = 0;
/** 切换楼层失败 */
public static final int FAILED = 1;
/** 地图还未聚焦到传入的室内图 */
public static final int NOT_FOCUSED = 2;
/** 当前室内图不存在该楼层 */
public static final int NOT_EXIST = 3;
/** 切换楼层, 室内ID信息错误 [android] 独有 */
public static final int SWICH_FLOOR_INFO_ERROR = 4;
}
}
@@ -0,0 +1,76 @@
package com.baidu.flutter_bmfmap.utils;
public class Env {
public static Boolean DEBUG = false;
/**
* 空白地图
*/
public static final int MAP_TYPE_NONE = 0;
/**
* 普通地图
*/
public static final int MAP_TYPE_NORMAL = 1;
/**
* 卫星地图
*/
public static final int MAP_TYPE_SATELLITE = 2;
/**
* 地图左下方
*/
public static final int LOGO_POSITION_LEFT_BOTTOM = 0;
/**
* 地图左下方
*/
public static final int LOGO_POSITION_LEFT_TOP = 1;
/**
* 地图中下方
*/
public static final int LOGO_POSITION_CENTER_BOTTOM = 2;
/**
* 地图中上方
*/
public static final int LOGO_POSITION_CENTER_TOP = 3;
/**
* 地图右下方
*/
public static final int LOGO_POSITION_RIGTH_BOTTOM = 4;
/**
* 地图右上方
*/
public static final int LOGO_POSITION_RIGTH_TOP = 5;
/**
* 定位图层显示方式
*/
public static class LocationMode {
/**
* 普通态: 更新定位数据时不对地图做任何操作
*/
public static final int NORMAL = 0;
/**
* 定位方向模式
*/
public static final int MODEHEADING = 1;
/**
* 跟随态,保持定位图标在地图中心
*/
public static final int FOLLOWING = 2;
/**
* 罗盘态,显示定位方向圈,保持定位图标在地图中心
*/
public static final int COMPASS = 3;
}
}
@@ -0,0 +1,38 @@
package com.baidu.flutter_bmfmap.utils;
import android.os.Build;
import androidx.annotation.RequiresApi;
import java.io.Closeable;
import java.io.IOException;
/**
* 用于安全的关闭closeable对象
*/
public class IOStreamUtils{
public static void closeSilently(Closeable o){
if(null == o){
return;
}
try {
o.close();
} catch (IOException e) {
e.printStackTrace();
}
}
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public static void closeSilently(AutoCloseable o){
if(null == o){
return;
}
try {
o.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
@@ -0,0 +1,74 @@
package com.baidu.flutter_bmfmap.utils;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
public class ThreadPoolUtil {
private ThreadFactory mThreadFactory = new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(r,"mapThread"+mAtomicInteger.getAndIncrement());
return t;
}
};
private AtomicInteger mAtomicInteger = new AtomicInteger(0);
private final int CORE_POOL_SIZE = Runtime.getRuntime().availableProcessors();
private final int MAX_POLL_SIZE = CORE_POOL_SIZE*2;
private final int KEEP_ALIVE = 3; //空线程alive时间
private ExecutorService mExecutorService;
private ScheduledExecutorService mScheduleExecutorService;
private static volatile ThreadPoolUtil sInstance;
public static ThreadPoolUtil getInstance(){
if(null == sInstance){
synchronized (ThreadPoolUtil.class){
if(null == sInstance){
sInstance = new ThreadPoolUtil();
}
}
}
return sInstance;
}
public ThreadPoolUtil(){
mExecutorService = new ThreadPoolExecutor(CORE_POOL_SIZE, MAX_POLL_SIZE, KEEP_ALIVE,
TimeUnit.SECONDS,
new ArrayBlockingQueue<Runnable>(1000),
mThreadFactory, new ThreadPoolExecutor.DiscardOldestPolicy());
mScheduleExecutorService = new ScheduledThreadPoolExecutor(CORE_POOL_SIZE, mThreadFactory);
}
public void execute(Runnable runnable){
if(null == mExecutorService){
return;
}
mExecutorService.execute(runnable);
}
public ScheduledFuture execute(Runnable runnable, int delayTime){
if(null == mScheduleExecutorService){
return null;
}
return mScheduleExecutorService.schedule(runnable, delayTime, TimeUnit.MILLISECONDS);
}
}
@@ -0,0 +1,435 @@
package com.baidu.flutter_bmfmap.utils.converter;
import android.graphics.Color;
import android.graphics.Point;
import android.text.TextUtils;
import android.util.Size;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.Projection;
import com.baidu.mapapi.map.WeightedLatLng;
import com.baidu.mapapi.map.WinRound;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.model.LatLngBounds;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public class FlutterDataConveter {
/**
* 将map形式的经纬度信息转换为结构化的经纬度数据
* @param latlngMap
* @return
*/
public static LatLng mapToLatlng(Map<String, Object> latlngMap){
if(null == latlngMap){
return null;
}
if(!latlngMap.containsKey("latitude")
|| !latlngMap.containsKey("longitude")){
return null;
}
Object latitudeObj = latlngMap.get("latitude");
Object longitudeObj = latlngMap.get("longitude");
if(null == latitudeObj || null == longitudeObj){
return null;
}
LatLng latLng = new LatLng((double)latitudeObj, (double)longitudeObj);
return latLng;
}
/**
* 将多个map形式的经纬度信息转换为结构化的经纬度数据
* @param latlngList
* @return
*/
public static List<LatLng> mapToLatlngs(List<Map<String, Double> > latlngList) {
if (null == latlngList) {
return null;
}
Iterator itr = latlngList.iterator();
ArrayList<LatLng> latLngs = new ArrayList<>();
while (itr.hasNext()){
Map<String, Object> latlngMap = (Map<String, Object>)itr.next();
LatLng latLng = mapToLatlng(latlngMap);
if(null == latLng){
break;
}
latLngs.add(latLng);
}
if(latLngs.size() != latlngList.size()){
return null;
}
return latLngs;
}
/**
* 将整形转换为16进制字符串
* @param number
* @return
*/
private static String intToHexValue(int number) {
String result = Integer.toHexString(number & 0xff);
while (result.length() < 2) {
result = "0" + result;
}
return result.toUpperCase();
}
/**
* 将16进制颜色转换为整形颜色值
* @param str 16进制颜色值
* @return
*/
public static int strColorToInteger(String str) {
if(TextUtils.isEmpty(str) || str.length() < 8){
return 0;
}
String str1 = str.substring(0, 2);
String str2 = str.substring(2, 4);
String str3 = str.substring(4, 6);
String str4 = str.substring(6, 8);
int alpha = Integer.parseInt(str1, 16);
int red = Integer.parseInt(str2, 16);
int green = Integer.parseInt(str3, 16);
int blue = Integer.parseInt(str4, 16);
return Color.argb(alpha, red, green, blue);
}
/**
* 批量根据icon名称获取BitmapDescriptor
* @param icons
* @return
*/
public static List<BitmapDescriptor> getIcons(List<String> icons){
if(null == icons){
return null;
}
List<BitmapDescriptor> bitmapIcons = new ArrayList<>();
Iterator itr = icons.iterator();
while (itr.hasNext()){
String icon = (String) itr.next();
BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromAsset("flutter_assets/" + icon);
bitmapIcons.add(bitmapDescriptor);
}
return bitmapIcons;
}
/**
* 批量将16进制字符串颜色值转换为整形颜色值
* @param colors
* @return
*/
public static List<Integer> getColors(List<String> colors){
if(null == colors || colors.size() <= 0){
return null;
}
List<Integer> intColors = new ArrayList<>();
Iterator iterator = colors.iterator();
while (iterator.hasNext()){
String colorStr = (String)iterator.next();
if(TextUtils.isEmpty(colorStr)){
return null;
}
int color = FlutterDataConveter.strColorToInteger(colorStr);
intColors.add(color);
}
return intColors;
}
/**
* 将map形式的bounds转换为LatLngBounds
* @param boundsMap
* @return
*/
public static LatLngBounds mapToLatlngBounds(Map<String, Object> boundsMap){
if(null == boundsMap){
return null;
}
if(!boundsMap.containsKey("northeast") || !boundsMap.containsKey("southwest")){
return null;
}
Map<String, Object> northeastMap = (Map<String, Object>)boundsMap.get("northeast");
Map<String, Object> southwestMap = (Map<String, Object>)boundsMap.get("southwest");
if(null == northeastMap || null == southwestMap){
return null;
}
LatLng northeast = mapToLatlng(northeastMap);
LatLng southwest = mapToLatlng(southwestMap);
return new LatLngBounds.Builder().include(northeast).include(southwest).build();
}
/**
* 将LatLngBounds转换为map
* @param latLngBounds
* @return
*/
public static Map<String, Object> latlngBoundsToMap(LatLngBounds latLngBounds){
if(null == latLngBounds){
return null;
}
LatLng southwest = latLngBounds.southwest;
LatLng northeast = latLngBounds.northeast;
Map<String, Double> southwestMap = FlutterDataConveter.latLngToMap(southwest);
Map<String, Double> northeastMap = FlutterDataConveter.latLngToMap(northeast);
HashMap<String, Object> latLngBoundsMap = new HashMap<>();
latLngBoundsMap.put("southwest", southwestMap);
latLngBoundsMap.put("northeast", northeastMap);
return latLngBoundsMap;
}
/**
* 将map形式的带权值经纬度数据转换为结构化的带权值的经纬度数据
* @param dataList
* @return
*/
public static List<WeightedLatLng> mapToWeightedLatLngList(List<Map<String, Object> > dataList) {
if(null == dataList){
return null;
}
List<WeightedLatLng> weightedLatLngList = new ArrayList<WeightedLatLng>();
Iterator itr = dataList.iterator();
while (itr.hasNext()){
Map<String, Object> data = ( Map<String, Object> )itr.next();
if(null == data){
return null;
}
if(!data.containsKey("pt")
|| !data.containsKey("intensity")){
return null;
}
Object intensityObj = data.get("intensity");
if(null == intensityObj){
return null;
}
double intensity = (double)intensityObj;
Object ptObj = data.get("pt");
if(null == ptObj){
return null;
}
Map<String, Object> ptMap = (Map<String, Object>)ptObj;
if(null == ptMap){
return null;
}
LatLng latLng = FlutterDataConveter.mapToLatlng(ptMap);
WeightedLatLng weightedLatLng = new WeightedLatLng(latLng, intensity);
weightedLatLngList.add(weightedLatLng);
}
return weightedLatLngList;
}
/**
* 将map形式的屏幕点坐标转换为Point
* @param pointMap
* @return
*/
public static Point mapToPoint(Map<String, Object> pointMap){
if(null == pointMap){
return null;
}
if(!pointMap.containsKey("x") || !pointMap.containsKey("y")){
return null;
}
Object xObj = pointMap.get("x");
Object yObj = pointMap.get("y");
if(null == xObj || null == yObj){
return null;
}
double x = (double)xObj;
double y = (double)yObj;
Point point = new Point((int)x, (int)y);
return point;
}
/**
* 将LatLng转成map存储
* @param latLng
* @return
*/
public static Map<String, Double> latLngToMap(LatLng latLng){
if(null == latLng){
return null;
}
Map<String, Double> resultMap = new HashMap<String, Double>();
resultMap.put("latitude", latLng.latitude);
resultMap.put("longitude", latLng.longitude);
resultMap.put("latitudeE6", latLng.latitudeE6);
resultMap.put("longitudeE6", latLng.longitudeE6);
return resultMap;
}
/**
* 将Point转成map存储
* @param point
* @return
*/
public static Map<String, Double> pointToMap(Point point){
if(null == point){
return null;
}
Map<String, Double> resultMap = new HashMap<String, Double>();
resultMap.put("x", (double)point.x);
resultMap.put("y", (double)point.y);
return resultMap;
}
/**
* 将flutter传过来的BMFRect转换为WinRound
* BMFRect结构:
* /// 屏幕左上点对应的直角地理坐标
* final BMFPoint origin;
*
* /// 坐标范围
* final BMFSize size;
*
* WinRound结构:
* public int left = 0;
* public int right = 0;
* public int top = 0;
* public int bottom = 0;
*/
public static WinRound BMFRectToWinRound(Map<String, Object> bmfRect){
if(null == bmfRect){
return null;
}
if(!bmfRect.containsKey("origin") || !bmfRect.containsKey("size")){
return null;
}
Map<String, Object> pointMap = (Map<String, Object>)bmfRect.get("origin");
Point point = FlutterDataConveter.mapToPoint(pointMap);
if(null == point){
return null;
}
Map<String, Object> sizeMap = (Map<String, Object>)bmfRect.get("size");
if(null == sizeMap){
return null;
}
if(null == sizeMap){
return null;
}
Double width = new TypeConverter<Double>().getValue(sizeMap, "width");
Double height = new TypeConverter<Double>().getValue(sizeMap, "height");
if(null == width || null == height){
return null;
}
WinRound winRound = new WinRound();
winRound.left = point.x;
winRound.top = point.y;
winRound.right = point.x + width.intValue();
winRound.bottom = point.y + height.intValue();
return winRound;
}
public static WinRound insetsToWinRound(Map<String, Object> insets){
if(null == insets){
return null;
}
if(!insets.containsKey("top")
||!insets.containsKey("left")
|| !insets.containsKey("bottom")
|| !insets.containsKey("right")){
return null;
}
Double top = new TypeConverter<Double>().getValue(insets, "top");
Double left = new TypeConverter<Double>().getValue(insets, "left");
Double bottom = new TypeConverter<Double>().getValue(insets, "bottom");
Double right = new TypeConverter<Double>().getValue(insets, "right");
if(null == top
|| null == left
|| null == bottom
|| null == right){
return null;
}
WinRound winRound = new WinRound();
winRound.left = left.intValue();
winRound.top = top.intValue();
winRound.right = right.intValue();
winRound.bottom = bottom.intValue();
return winRound;
}
public static LatLngBounds BMFRectToLatLngBounds(BaiduMap baiduMap, Map<String, Object> bmfRect){
if(null == baiduMap || null == bmfRect){
return null;
}
WinRound winRound = FlutterDataConveter.BMFRectToWinRound(bmfRect);
if(null == bmfRect){
return null;
}
Point notrhEastPoint = new Point();
notrhEastPoint.x = winRound.left;
notrhEastPoint.y = winRound.top;
Point southWestPoint = new Point();
southWestPoint.x = winRound.right;
southWestPoint.y = winRound.bottom;
Projection projection = baiduMap.getProjection();
LatLng northEast = projection.fromScreenLocation(notrhEastPoint);
LatLng southWest = projection.fromScreenLocation(southWestPoint);
LatLngBounds latLngBounds = new LatLngBounds.Builder().include(northEast).include(southWest).build();
return latLngBounds;
}
}
@@ -0,0 +1,23 @@
package com.baidu.flutter_bmfmap.utils.converter;
import java.util.Map;
/**
* 主要用于快速的送map中获取元素value,并进行类型转换
* @param <T> 目标转换类型
*/
public class TypeConverter<T>{
public T getValue(Map<String, Object> map, String key){ //泛型方法getKey的返回值类型为T,T的类型由外部指定
if(null == map){
return null;
}
Object valueObj = map.get(key);
if(null == valueObj){
return null;
}
T value = (T)valueObj;
return value;
}
}
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFFFFF" />
<corners android:radius="3dip"/>
<stroke
android:width="1dip"
android:color="#0000FF" />
</shape>
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFFFFF" />
<corners android:radius="3dip"/>
<stroke
android:width="1dip"
android:color="#728ea3" />
</shape>
Binary file not shown.

After

Width:  |  Height:  |  Size: 4.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.6 KiB

@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Modify this file to customize your launch splash screen -->
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@android:color/white" />
<!-- You can insert your own image assets here -->
<!-- <item>
<bitmap
android:gravity="center"
android:src="@mipmap/launch_image" />
</item> -->
</layer-list>
Binary file not shown.

After

Width:  |  Height:  |  Size: 3.2 KiB