我們的應(yīng)用可能會(huì)用到地圖定位等功能添祸,但是無論高德還是百度直接接入到應(yīng)用中都會(huì)導(dǎo)致體積增大桶至,甚至導(dǎo)致65535方法數(shù)問題狡相,所以用插件的方式接入是個(gè)不錯(cuò)的選擇后专。
這里我用了Apkplug這個(gè)插件框架,下面是我的接入過程美浦。
百度地圖接入
這里介紹一下將百度地圖作為插件并接入宿主的例子弦赖,例子沒有將完備的功能接入,只是將一個(gè)定位界面接入浦辨,在宿主中點(diǎn)擊一下按鈕蹬竖,彈出定位界面。
一流酬、插件開發(fā)
其實(shí)比較麻煩的是賬號(hào)申請(qǐng)币厕,key申請(qǐng),所以先介紹一下這些相關(guān)的芽腾。
注冊(cè)的話旦装,這里不做詳細(xì)介紹,我第一次注冊(cè)摊滔,怎么都要求我上傳什么身份證正反面照片阴绢,我沒傳,第二次登陸的時(shí)候好像就不需要了惭载,你遇到相同的情況旱函,可以先退出响巢,再登陸描滔,也許可以繞過上傳證件照。
申請(qǐng)key踪古,這個(gè)跟高德地圖的操作差不多含长,但是使用時(shí)有所差別券腔,下面會(huì)說,先看一下key的申請(qǐng)拘泞。
這里是百度地圖的控制臺(tái)
點(diǎn)擊創(chuàng)建應(yīng)用后纷纫,如下圖所示:
其中,apk簽名sha1值的取得參考這里
這里需要注意的是陪腌,一個(gè)應(yīng)用想使用這個(gè)key辱魁,需要簽名、包名诗鸭、key值相對(duì)應(yīng)染簇。地圖sdk是在插件中接入的,所以key值需要配置在插件里强岸,但是百度地圖sdk讀取包名時(shí)锻弓,會(huì)讀取宿主的,所以在創(chuàng)建key時(shí)蝌箍,直接使用宿主包名青灼,但是將生成的key配置到插件。如果為了測(cè)試時(shí)插件獨(dú)立運(yùn)行妓盲,可以單獨(dú)為插件生成一套相應(yīng)key杂拨,但是加入宿主時(shí),必須換成宿主key
key的問題搞清楚后悯衬,就沒有太多坑了扳躬。按照百度地圖文檔添加一個(gè)定位界面即可。
<com.baidu.mapapi.map.MapView
android:id="@+id/bmapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:layout_below="@+id/textView" />
這是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"); // 設(shè)置坐標(biāo)類型
option.setScanSpan(1000);
mLocationClient.setLocOption(option);
mLocationClient.start();
這些都在百度地圖文檔中可以查到甚亭,不做詳細(xì)介紹贷币,包括初始化、權(quán)限亏狰、及組件的配置役纹,直接參考百度文檔。沒什么坑可說暇唾。先用插件單獨(dú)運(yùn)行一下促脉,能實(shí)現(xiàn)功能就算完成了。
然后按照套路策州,給插件設(shè)置plugin.xml
<plugin-features
Bundle-Name="AMapPlug"
Bundle-SymbolicName="com.apkplug.amapplug"
Bundle-Version="1.0.0"
date="2016.07.20"
provider-name="APKPLUG"
Bundle-Activity="com.apkplug.baidumapplug.MenuActivity"
start-level ="1"
start-up="true"
>
</plugin-features>
這樣一個(gè)插件就完成了瘸味。唯一需要注意的是上面提到的key的問題。
宿主開發(fā)
宿主開發(fā)中够挂,需要將百度地圖的權(quán)限配置過來旁仿,是android 6.0的話記得手動(dòng)獲取權(quán)限。其他沒啥坑了孽糖,按照套路來就行枯冈。
我這里直接啟動(dòng)activity
Intent intent = new Intent();
intent.setClassName(MainActivity.this, "com.apkplug.baidumapplug.MainActivity");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
這里面還使用了一個(gè)異步啟動(dòng)的例子:
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);
Log.e(this.getClass().getName(),bundle.getName());
}
@Override
public void onFail(org.osgi.framework.Bundle bundle, Throwable throwable) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
throwable.printStackTrace(pw);
System.out.println(sw.toString()); // stack trace as a string
Log.e(this.getClass().getName(),throwable.toString());
}
});
}
}
如果一個(gè)插件毅贮,在初始化的時(shí)候很費(fèi)時(shí),則可以選擇異步啟動(dòng)尘奏。
demo地址:
插件地址:https://github.com/apkplug/SDKDemo/tree/master/BaiduMapPlug
宿主地址:https://github.com/apkplug/SDKDemo/tree/master/BaiduMapPlugUser
高德地圖插件開發(fā)
下面介紹高德地圖的接入過程滩褥,我把高德地圖的一個(gè)簡(jiǎn)單定位功能放到了插件里,從宿主點(diǎn)一下按鈕就跳到插件的定位界面炫加。
1 插件開發(fā)
首先注冊(cè)高德瑰煎,注冊(cè)好后,比較關(guān)鍵的就是key的使用俗孝,高德地圖的各種接口調(diào)用都要檢測(cè)key丢间,也是因?yàn)橛腥朔从砶ey的各種問題,所以這個(gè)例子重點(diǎn)說的其實(shí)是key的事驹针。
key的管理在控制臺(tái):
上面提到獲取sha1值的方法:參考這里
搞定了key的問題烘挫,就好辦了,下面做一個(gè)定位的界面
在你的界面中加入地圖控件
<com.amap.api.maps.MapView
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent">
</com.amap.api.maps.MapView>
然后在代碼中進(jìn)行設(shè)置
mlocationClient = new AMapLocationClient(getContext());
mLocationOption = new AMapLocationClientOption();
mMapView = ((MapView)getView().findViewById(R.id.map));
mMapView.onCreate(savedInstanceState);
aMap = mMapView.getMap();
//對(duì)界面的監(jiān)聽柬甥,這個(gè)要先設(shè)置
aMap.setLocationSource(new LocationSource() {
@Override
public void activate(final OnLocationChangedListener onLocationChangedListener) {
mlocationClient.setLocationListener(new AMapLocationListener() {
@Override
public void onLocationChanged(AMapLocation aMapLocation) {
onLocationChangedListener.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);// 設(shè)置默認(rèn)定位按鈕是否顯示
aMap.setMyLocationEnabled(true);// 設(shè)置為true表示顯示定位層并可觸發(fā)定位饮六,false表示隱藏定位層并不可觸發(fā)定位,默認(rèn)是false
// 設(shè)置定位的類型為定位模式 苛蒲,可以由定位卤橄、跟隨或地圖根據(jù)面向方向旋轉(zhuǎn)幾種
aMap.setMyLocationType(AMap.LOCATION_TYPE_LOCATE);
這樣界面就好了,你單獨(dú)運(yùn)行插件應(yīng)該就能定位了臂外,如果返回錯(cuò)誤碼窟扑,提示你key錯(cuò)了,那你需要再好好看看上面設(shè)置key的說明漏健。
然后按照套路嚎货,給插件設(shè)置plugin.xml
<plugin-features
Bundle-Name="AMapPlug"
Bundle-SymbolicName="com.apkplug.amapplug"
Bundle-Version="1.0.0"
date="2016.07.20"
provider-name="APKPLUG"
Bundle-Activity="com.apkplug.amapplug.MenuActivity"
start-level ="1"
start-up="true"
>
</plugin-features>
2 宿主開發(fā)
宿主不需要特殊說明什么,按照套路來蔫浆,初始化殖属、安裝插件。按照高德地圖說明文檔配置權(quán)限瓦盛。
直接用intent啟動(dòng)插件就行
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