Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

插件化接入百度地圖與高德地圖實戰

編輯:關於 Android 程式設計

應用一旦需要地圖與定位功能,直接把 SDK 打包進來往往會讓體積急劇膨脹,甚至可能觸及 65535 方法數的限制。此時以「插件」的方式接入是相當不錯的選擇。本文以 Apkplug 框架為例,記錄了將百度地圖與高德地圖分別作為插件接入宿主App的完整過程。

一、百度地圖接入

這裡示範把百度地圖做成插件並接入宿主:在宿主中點擊按鈕,彈出插件的定位介面。

1. 插件開發

最麻煩的其實是賬號與 key 的申請。key 的申請流程與高德類似,但使用時有明顯差異。進入百度地圖控制台:

點擊「創建應用」後:

其中 apk 簽名的 sha1 值取得方式請參考官方文件。

關鍵注意事項:一個應用要使用某個 key,簽名、包名、key 三者必須對應。由於地圖 SDK 是在插件中接入的,key 需要配置在插件裡;但百度地圖 SDK 讀取包名時讀的是宿主的包名,因此建立 key 時應直接使用宿主的包名,再把生成的 key 配置到插件。若想讓插件獨立測試,可另外為插件生成一套 key,但一旦併入宿主,就必須換回宿主對應的 key。

把 key 的問題理清後,剩下的就照百度地圖文件操作即可。在布局中加入地圖控件:

在 Activity 中初始化並啟動定位:

mMapView = (MapView) findViewById(R.id.bmapView);
baiduMap = mMapView.getMap();
baiduMap.setMyLocationEnabled(true);       // 開啟定位圖層
mLocationClient = new LocationClient(this);
mLocationClient.registerLocationListener(myListener);
LocationClientOption option = new LocationClientOption();
option.setOpenGps(true);                    // 打開 GPS
option.setCoorType("bd09ll");               // 設定座標類型
option.setScanSpan(1000);
mLocationClient.setLocOption(option);
mLocationClient.start();

先用插件獨立運行,能正常定位即算完成。接著為插件加上 plugin.xml:


2. 宿主開發

宿主需要把百度地圖的權限配置過來,Android 6.0 以上記得手動申請權限,其餘按套路進行。啟動插件 Activity:

Intent intent = new Intent();
intent.setClassName(MainActivity.this, "com.apkplug.baidumapplug.MainActivity");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

若插件初始化較耗時,可改用異步啟動:

org.osgi.framework.Bundle[] bundles = FrameworkFactory.getInstance()
        .getFrame().getSystemBundleContext().getBundles();
for (org.osgi.framework.Bundle bundle : bundles) {
    if (bundle.getName().equals("BaiduMapPlug")) {
        bundle.start(new StartCallback() {
            @Override public void onSuccess(org.osgi.framework.Bundle bundle) {
                Intent intent = new Intent();
                intent.setClassName(MainActivity.this, "com.apkplug.baidumapplug.MainActivity");
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }
            @Override public void onFail(org.osgi.framework.Bundle bundle, Throwable throwable) {
                Log.e(getClass().getName(), throwable.toString());
            }
        });
    }
}

Demo 位址:

  • 插件:https://github.com/apkplug/SDKDemo/tree/master/BaiduMapPlug
  • 宿主:https://github.com/apkplug/SDKDemo/tree/master/BaiduMapPlugUser

二、高德地圖接入

同樣把高德地圖的簡單定位功能放進插件,宿主點擊按鈕即跳轉到插件的定位介面。

1. 插件開發

完成高德註冊後,關鍵仍在於 key——高德的各項接口調用都會檢測 key。key 的管理在控制台:

在介面中加入地圖控件:

在程式碼中完成設定:

mlocationClient = new AMapLocationClient(getContext());
mLocationOption = new AMapLocationClientOption();
mMapView = getView().findViewById(R.id.map);
mMapView.onCreate(savedInstanceState);
aMap = mMapView.getMap();
aMap.setLocationSource(new LocationSource() {
    @Override public void activate(final OnLocationChangedListener listener) {
        mlocationClient.setLocationListener(aMapLocation -> listener.onLocationChanged(aMapLocation));
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        mlocationClient.setLocationOption(mLocationOption);
        mlocationClient.startLocation();
    }
    @Override public void deactivate() {
        if (mlocationClient != null) {
            mlocationClient.stopLocation();
            mlocationClient.onDestroy();
        }
        mlocationClient = null;
    }
});
aMap.getUiSettings().setMyLocationButtonEnabled(true);
aMap.setMyLocationEnabled(true);
aMap.setMyLocationType(AMap.LOCATION_TYPE_LOCATE);

單獨運行插件若能定位即完成;若回傳錯誤碼提示 key 有誤,請回頭檢查上面關於 key 的說明。接著加上 plugin.xml:


2. 宿主開發

宿主按套路初始化、安裝插件,並依高德文件配置權限,最後用 Intent 啟動插件:

Intent intent = new Intent();
intent.setClassName(MainActivity.this, "com.apkplug.amapplug.MenuActivity");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

Demo 程式碼:

  • 插件:https://github.com/apkplug/SDKDemo/tree/master/AMapPlug
  • 宿主:https://github.com/apkplug/SDKDemo/tree/master/AMapPlugUser
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved