首页 > 代码库 > 百度地图3.1.0(二)基于MapFragment的基本地图
百度地图3.1.0(二)基于MapFragment的基本地图
基于Fragment的百度Map也很简单
直接看例子吧:
AndroidManifest.xml的配置:
1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="com.example.testbaidu_v_3_1" 4 android:versionCode="1" 5 android:versionName="1.0" > 6 7 <uses-sdk 8 android:minSdkVersion="8" 9 android:targetSdkVersion="21" />10 11 <uses-permission android:name="android.permission.GET_ACCOUNTS" />12 <uses-permission android:name="android.permission.USE_CREDENTIALS" />13 <uses-permission android:name="android.permission.MANAGE_ACCOUNTS" />14 <uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" />15 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />16 <uses-permission android:name="android.permission.INTERNET" />17 <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />18 <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />19 <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />20 <uses-permission android:name="android.permission.READ_PHONE_STATE" />21 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />22 <uses-permission android:name="android.permission.BROADCAST_STICKY" />23 <uses-permission android:name="android.permission.WRITE_SETTINGS" />24 <uses-permission android:name="android.permission.READ_PHONE_STATE" />25 26 <application27 android:allowBackup="true"28 android:icon="@drawable/ic_launcher"29 android:label="@string/app_name"30 android:theme="@style/AppTheme" >31 <meta-data32 android:name="com.baidu.lbsapi.API_KEY"33 android:value="your key" />34 35 <activity36 android:name=".MapFragment"37 android:label="@string/app_name" >38 <intent-filter>39 <action android:name="android.intent.action.MAIN" />40 41 <category android:name="android.intent.category.LAUNCHER" />42 </intent-filter>43 </activity>44 </application>45 46 </manifest>
MapFragment类:
1 package com.example.testbaidu_v_3_1; 2 3 import android.content.BroadcastReceiver; 4 import android.content.Context; 5 import android.content.Intent; 6 import android.content.IntentFilter; 7 import android.os.Bundle; 8 import android.support.v4.app.FragmentActivity; 9 import android.support.v4.app.FragmentManager;10 import android.widget.Toast;11 12 import com.baidu.mapapi.SDKInitializer;13 import com.baidu.mapapi.map.BaiduMapOptions;14 import com.baidu.mapapi.map.MapStatus;15 import com.baidu.mapapi.map.SupportMapFragment;16 17 public class MapFragment extends FragmentActivity{18 19 private SupportMapFragment map;20 private SDKReceiver mReceiver;21 22 @Override23 public void onCreate(Bundle savedInstanceState) {24 // 在使用SDK各组件之前初始化context信息,传入ApplicationContext25 // 注意该方法要再setContentView方法之前实现26 SDKInitializer.initialize(getApplicationContext());27 super.onCreate(savedInstanceState);28 setContentView(R.layout.activity_fragment);29 MapStatus ms = new MapStatus.Builder().overlook(-20).zoom(15).build();30 //overlook 设置地图俯仰角31 //zoom 设置地图缩放级别32 BaiduMapOptions bo = new BaiduMapOptions().mapStatus(ms)33 .compassEnabled(false).zoomControlsEnabled(false);34 //compassEnabled 设置是否允许指南针,默认允许35 //zoomControlsEnabled 设置是否显示缩放控件36 map = SupportMapFragment.newInstance(bo);37 FragmentManager manager = getSupportFragmentManager();38 manager.beginTransaction().add(R.id.map, map, "map_fragment").commit();39 40 IntentFilter iFilter = new IntentFilter();41 iFilter.addAction(SDKInitializer.SDK_BROADTCAST_ACTION_STRING_PERMISSION_CHECK_ERROR);42 iFilter.addAction(SDKInitializer.SDK_BROADCAST_ACTION_STRING_NETWORK_ERROR);43 mReceiver = new SDKReceiver();44 registerReceiver(mReceiver, iFilter);45 }46 47 protected void onDestroy() {48 super.onDestroy();49 // 在activity执行onDestroy时执行mMapView.onDestroy(),实现地图生命周期管理50 unregisterReceiver(mReceiver);51 }52 53 public class SDKReceiver extends BroadcastReceiver {54 public void onReceive(Context context, Intent intent) {55 String s = intent.getAction();56 if (s.equals(SDKInitializer.SDK_BROADTCAST_ACTION_STRING_PERMISSION_CHECK_ERROR)) {57 Toast.makeText(MapFragment.this,58 "key 验证出错! 请在 AndroidManifest.xml 文件中检查 key 设置",59 Toast.LENGTH_SHORT).show();60 ;61 } else if (s62 .equals(SDKInitializer.SDK_BROADCAST_ACTION_STRING_NETWORK_ERROR)) {63 Toast.makeText(MapFragment.this, "网络出错", Toast.LENGTH_SHORT)64 .show();65 }66 }67 }68 }
activity_fragment.xml文件:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="fill_parent" 4 android:layout_height="fill_parent" 5 android:orientation="vertical" > 6 7 <FrameLayout 8 android:id="@+id/map" 9 android:layout_width="match_parent"10 android:layout_height="match_parent" />11 12 </LinearLayout>
运行结果截图:
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。