完全按照百度Android地图SDK完成配置与代码编写
问题相关代码
布局文件:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.baidu.mapapi.map.MapView
android:id="@+id/bmapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true" />
</LinearLayout>
活动代码:
public class ShowMapActivity extends AppCompatActivity {
private MapView mMapView = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//在使用SDK各组件之前初始化context信息,传入ApplicationContext
SDKInitializer.initialize(getApplicationContext());
//自4.3.0起,百度地图SDK所有接口均支持百度坐标和国测局坐标,用此方法设置您使用的坐标类型.
//包括BD09LL和GCJ02两种坐标,默认是BD09LL坐标。
SDKInitializer.setCoordType(CoordType.BD09LL);
setContentView(R.layout.showmap_layout);
mMapView = (MapView) findViewById(R.id.bmapView);
}
@Override
protected void onResume() {
super.onResume();
//在activity执行onResume时执行mMapView. onResume (),实现地图生命周期管理
mMapView.onResume();
}
@Override
protected void onPause() {
super.onPause();
//在activity执行onPause时执行mMapView. onPause (),实现地图生命周期管理
mMapView.onPause();
}
@Override
protected void onDestroy() {
super.onDestroy();
//在activity执行onDestroy时执行mMapView.onDestroy(),实现地图生命周期管理
mMapView.onDestroy();
}
}
运行结果及报错内容
E/SharedPreferencesImpl: Couldn't create directory for SharedPreferences file /data/user/0/com.example.activitytest/shared_prefs/authStatus_com.example.activitytest������������������������������������������������������������������������������������������������������������������������������������������������������.xml
E/PermissionCheck: permission check result is: 601
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.activitytest, PID: 8036
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.activitytest/com.example.activitytest.Activity.ShowMapActivity}: com.baidu.mapapi.common.BaiduMapSDKException: not agree privacyMode, please invoke SDKInitializer.setAgreePrivacy(Context, boolean) function
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3774)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3936)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2341)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:233)
at android.os.Looper.loop(Looper.java:344)
at android.app.ActivityThread.main(ActivityThread.java:8171)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:584)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1034)
Caused by: com.baidu.mapapi.common.BaiduMapSDKException: not agree privacyMode, please invoke SDKInitializer.setAgreePrivacy(Context, boolean) function
at com.baidu.mapsdkplatform.comapi.a.c(Unknown Source:86)
at com.baidu.mapsdkplatform.comapi.c.a(Unknown Source:51)
at com.baidu.mapapi.SDKInitializer.initialize(Unknown Source:2)
at com.example.activitytest.Activity.ShowMapActivity.onCreate(ShowMapActivity.java:20)
at android.app.Activity.performCreate(Activity.java:8130)
at android.app.Activity.performCreate(Activity.java:8110)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1344)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3743)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3936)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2341)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:233)
at android.os.Looper.loop(Looper.java:344)
at android.app.ActivityThread.main(ActivityThread.java:8171)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:584)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1034)
I/Process: Sending signal. PID: 8036 SIG: 9