参照API和自己添加的写法以及顺序
1.下载Android地图api以及demo
2.申请Key
3.studio配置:
一、在工程app/libs目录下放入baidumapapi_vX_X_X.jar包(特别注意版本必须一样)
二、在src/main/目录下新建jniLibs目录(看着百度的讲解容易乱、并且容易出错);建好jniLibs要把libs中的.so和jar包引用到里面,在build.gradle(app),如下:android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
sourceSets {
main {
jniLibs.srcDir 'libs'
}
}
defaultConfig {
applicationId "loc.hanwei.com"
minSdkVersion 16
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
4.配置好环境进入开发,需要在AndroidManifest.xml中进行配置Key、service等;如下:
package="loc.hanwei.com">
android:anyDensity="true"
android:largeScreens="true"
android:normalScreens="false"
android:resizeable="true"
android:smallScreens="true" />
android:name=".LocDemoApplication"//在使用SDK各组件之前初始化context信息,所有我们再次调用SDKInitializer.initialize(getApplicationContext());
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
android:name="com.baidu.lbsapi.API_KEY"
android:value="1G5hhFOAehNtYKdgGmQu9apes2nUDKWq" />
android:name="com.baidu.location.f"
android:enabled="true"
android:process=":remote" >
5.调用SDKInitializer.initialize(getApplicationContext())/**
*
* Created by 陈苗辉 on 2017/2/13.
*/
public class LocDemoApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
// 在使用 SDK 各组间之前初始化 context 信息,传入 ApplicationContext
SDKInitializer.initialize(getApplicationContext());
}
}
6.可用MapView展示地图了
android:id="@+id/bmapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true" />
以上就完成了!
说说我遇到的Bug吧:
android.view.InflateException: Binary XML file line #899: Error inflating class com.baidu.mapapi.map.MapView
这句话的意思是:xml中899行的百度MapView出错;查了网上很多资料,都说没有初始化SDK,但是我的确初始化了,找了半天没发现原因,最后自己又写一个demo发现.so和jar中的不同,但是版本一样,瞬间雷化了;以后再也不用百度了,太坑、太乱!