Flutter百度地图

Flutter百度地图-重构项目

一、实现效果如下图

qq交流群:群号:730772561

1、地图中任意踩点进行杆塔和设备的新建,对与点和线进行关联请添加图片描述

2、对于点和线进行各种操作。

请添加图片描述

3、自定义区域下载离线地图。

在这里插入图片描述

4…热烈线

5…自定义瓦片地图

6…热力地图等…地图动画等…多边形转换动画等

…很多慢慢来

如上图项目中有点线,
1.我们实现点和线。
2.对点和线进行连接。
3.设置点和线文字等。

到项目的我完成。

二、开始写项目

1、项目项目添加依赖。

.yaml下面添加
flutter_bmfmap: ^1.0.1
flutter_bmfutils: ^1.0.1

2、获取然后设置清单文件的AK百度官方文档等。

3、可能很多的小伙伴会遇到如下错位置并且奔溃 “android/platform_view_android_jni.cc”

//Flutter运行出现下面提示
android/platform_view_android_jni.cc     
//原生环境运行出现下面。
SDKInitializer.initialize(getApplicationContext())......
   这里我们需要初始化MyApplication并且继承BmfMapApplication看了分装源码的就会知道。
   在BmfMapApplication里面进行了SDK的初始化init操作。在集成过程中遇到的问题。
 在原生Android main下面进行继承
class MyApplication : BmfMapApplication() {

}

且在AndroidMmanifest.xml下面配置如下代码解决问题

<application
        android:name=".MyApplication"///这里去引用MyApplication进行初始化SDK
        android:label="flutterosm"
        android:icon="@mipmap/ic_launcher">

4、打开官方文档我们进行新App的开发旅程。

1.我们首先看看如何加载地图的

地图Flutter Widget构造,BMFMapWidget是地图Flutter插件封装的一个支持AndroidView和UiKitView的Widget。官方文档如下。

Container(
      height: screenSize.height,
      width: screenSize.width,
      child: BMFMapWidget(
        onBMFMapCreated: (controller) {
            
        },
        mapOptions: mapOptions,
      ),
    );
2、BMFMapOptions构造,BMFMapOptions包含了创建地图所需要的各种状态参数[显示样式,限制地图的显示范围,当前地图的中心点,地图的自定义最大最小比例尺级别…看官方文档

BMFMapOptions mapOptions = BMFMapOptions(
        center: BMFCoordinate(39.917215, 116.380341),
        zoomLevel: 12,
        mapPadding: BMFEdgeInsets(left: 30, top: 0, right: 30, bottom: 0));

我们直接上代码

import 'dart:io' show Platform;
import 'package:flutter/material.dart';
import 'package:flutter_bmfbase/BaiduMap/bmfmap_base.dart' show BMFMapSDK, BMF_COORD_TYPE;
import 'package:flutter_bmfbase/BaiduMap/bmfmap_base.dart';
import 'package:flutter_bmfmap/BaiduMap/bmfmap_map.dart';
void main() {
  WidgetsFlutterBinding.ensureInitialized();
  // 百度地图sdk初始化鉴权
  if (Platform.isIOS) {
    BMFMapSDK.setApiKeyAndCoordType(
        '请在此输入您在开放平台上申请的API_KEY', BMF_COORD_TYPE.BD09LL);
  } else if (Platform.isAndroid) {
    // Android 目前不支持接口设置Apikey,
    // 请在主工程的Manifest文件里设置,详细配置方法请参考官网(https://lbsyun.baidu.com/)demo
    BMFMapSDK.setCoordType(BMF_COORD_TYPE.BD09LL);
  }

  runApp(new MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  BMFMapOptions mapOptions;

  @override
  void initState() {
    super.initState();
    mapOptions = BMFMapOptions(
        center: BMFCoordinate(39.917215, 116.380341),
        zoomLevel: 12,
        mapPadding: BMFEdgeInsets(left: 30, top: 0, right: 30, bottom: 0));
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar:AppBar(
          title:Text('百度地图flutter插件Demo'),
        ),
        body: BMFMapWidget(
          onBMFMapCreated: (controller) {

          },
          mapOptions: mapOptions,
        ),
      ),
    );
  }
}

效果如下图

在这里插入图片描述
接下来我们进行基本的按钮和界面一样。
请添加图片描述

5、百度定位和百度地图以及其他插件直接的冲突和bug
1、可能出现 BaiduLBS_Android.jar 的冲突。

“find libs BaiduLBS_Android.jar and jetb_BaiduLBS_Android.jar” 去每一个依赖了的builde.gradle里面只能有且一个api其他的都需要compileOnly

 compileOnly files('libs/BaiduLBS_Android.jar')
2、出现 …path ‘lib/arm64-v8a/libindoor.so’…
  • What went wrong:
    Execution failed for task ‘:app:transformNativeLibsWithMergeJniLibsForDebug’. More than one file was found with OS independent path ‘lib/arm64-v8a/libindoor.so’
* What went wrong:
Execution failed for task ':app:transformNativeLibsWithMergeJniLibsForDebug'.
> More than one file was found with OS independent path 'lib/arm64-v8a/libindoor.so'

在下面的Flutter plugins下面寻找库里面libs下面重复的so库然后在下面的build下面进行移除或者唯一

    packagingOptions{
        pickFirst 'lib/arm64-v8a/libindoor.so'
        pickFirst 'lib/x86/libindoor.so'
        pickFirst 'lib/x86_64/libindoor.so'
        pickFirst 'lib/armeabi-v7a/libindoor.so'
        pickFirst 'lib/x86_64/liblocSDK8a.so'
        pickFirst 'lib/arm64-v8a/liblocSDK8a.so'
        pickFirst 'lib/x86/liblocSDK8a.so'
        pickFirst 'lib/armeabi-v7a/liblocSDK8a.so'
    }

3、出现BMFMapWidget create失败…register
运行之后出FlutterBmfmapPlugin…没有register
我们去Flutter plugin下面找到flutter_bmfmap_1.0.1下面的src.main.FlutterBmmapPlugin

/**
 * FlutterBmfmapPlugin
 */
package com.baidu.flutter_bmfmap;

import android.os.Build;
import android.util.Log;

import androidx.annotation.NonNull;

import com.baidu.flutter_bmfmap.map.OfflineHandler;
import com.baidu.flutter_bmfmap.utils.Constants;
import com.baidu.flutter_bmfmap.utils.Env;

import io.flutter.embedding.engine.FlutterEngine;
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.embedding.engine.plugins.shim.ShimPluginRegistry;
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;
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;
    //魔改百度地图插件 全局变量01
    private FlutterPluginBinding flutterPluginBinding;
    //魔改百度地图插件 全局变量02
    private PlatformViewRegistry mRegistrar;
    //魔改百度地图插件 方法01
    @Override
    public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
        mOfflineHandler = new OfflineHandler();
        mOfflineHandler.init(flutterPluginBinding.getBinaryMessenger());
        this.flutterPluginBinding = flutterPluginBinding;
        //BMKMapView
        this.mRegistrar = flutterPluginBinding.getPlatformViewRegistry();
        Log.e(TAG, "onAttachedToEngine: "+"注册了" );

     //注释百度他们自己的东西
    /*ShimPluginRegistry shimPluginRegistry = new ShimPluginRegistry(flutterEngine);
    String key = FlutterBmfmapPlugin.class.getSimpleName();
    if(shimPluginRegistry.hasPlugin(key)){
      if(Env.DEBUG){
        Log.d(TAG, "hasPlugin");
      }
      return;
    }

    mRegistrar = shimPluginRegistry.registrarFor(key);*/
    }

    // 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());
    }
    //魔改百度地图插件 方法02
    @Override
    public void onAttachedToActivity(ActivityPluginBinding binding) {
      Log.e(TAG, "onAttachedToActivity: "+"绑定了" );
        if (null == binding || null == mRegistrar) {
            return;
        }
        //删除百度下面的。然后复制这几行
        mRegistrar.registerViewFactory(
                Constants.ViewType.sMapView,
                new MapViewFactory(binding.getActivity()
                        , flutterPluginBinding.getBinaryMessenger()
                        , Constants.ViewType.sMapView));

        mRegistrar.registerViewFactory(
                Constants.ViewType.sTextureMapView,
                new TextureMapViewFactory(binding.getActivity()
                        , flutterPluginBinding.getBinaryMessenger()
                        , 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");
    }
}

要是大家觉得很麻烦:
5里的东西不用管了用下面的吧。已经上传git

https://gitee.com/xskill/flutter_bmfmap 群里的小哥直接放到git了。我当时打算推到dar库没成功就算了。

运行结果:完成定位和地图同时展示

慢慢来待续
视频教学同步走

©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页