1.下载百度地图的demo,下载地址:http://lbsyun.baidu.com/sdk/download?selected=mapsdk_basicmap,mapsdk_searchfunction,mapsdk_lbscloudsearch,mapsdk_calculationtool,mapsdk_radar
2.把demo里面的BaiduMapsApiDemo解压,把BaiduMapsApiDemo文件夹里面的libs里面的所有文件都复制到自己的项目的libs里面
3.配置AndroidManifest.xml,把BaiduMapsApiDemo文件夹里面的AndroidManifest.xml有关百度地图要使用的配置信息复制到自己的AndroidManifest.xml里面
这是我配置好的例子的AndroidManifest.xml:
1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="com.zzw.baidumappoint" 4 android:versionCode="1" 5 android:versionName="1.0" > 6 7 <uses-sdk 8 android:minSdkVersion="14" 9 android:targetSdkVersion="21" /> 10 11 <application 12 android:allowBackup="true" 13 android:icon="@drawable/ic_launcher" 14 android:label="@string/app_name" 15 android:theme="@style/AppTheme" > 16 <activity 17 android:name=".MainActivity" 18 android:label="@string/app_name" > 19 <intent-filter> 20 <action android:name="android.intent.action.MAIN" /> 21 22 <category android:name="android.intent.category.LAUNCHER" /> 23 </intent-filter> 24 </activity> 25 26 <meta-data 27 android:name="com.baidu.lbsapi.API_KEY" 28 android:value="tpBieaHroLK1LeSkOQWLzq6A" /> 29 30 <service 31 android:name="com.baidu.location.f" 32 android:enabled="true" 33 android:process=":remote" > 34 </service> 35 </application> 36 37 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" > 38 </uses-permission> 39 <uses-permission android:name="android.permission.INTERNET" > 40 </uses-permission> 41 <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" /> 42 <uses-permission android:name="android.permission.WAKE_LOCK" > 43 </uses-permission> 44 <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> 45 <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 46 <!-- SDK1.5需要android.permission.GET_TASKS权限判断本程序是否为当前运行的应用? --> 47 <uses-permission android:name="android.permission.GET_TASKS" /> 48 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" > 49 </uses-permission> 50 <uses-permission android:name="android.permission.WRITE_SETTINGS" /> 51 <!-- 这个权限用于进行网络定位 --> 52 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" > 53 </uses-permission> 54 <!-- 这个权限用于访问GPS定位 --> 55 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" > 56 </uses-permission> 57 <!-- 用于访问wifi网络信息,wifi信息会用于进行网络定位 --> 58 <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" > 59 </uses-permission> 60 <!-- 获取运营商信息,用于支持提供运营商信息相关的接口 --> 61 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" > 62 </uses-permission> 63 <!-- 这个权限用于获取wifi的获取权限,wifi信息会用来进行网络定位 --> 64 <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" > 65 </uses-permission> 66 <!-- 用于读取手机当前的状态 --> 67 <uses-permission android:name="android.permission.READ_PHONE_STATE" > 68 </uses-permission> 69 <!-- 写入扩展存储,向扩展卡写入数据,用于写入离线定位数据 --> 70 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" > 71 </uses-permission> 72 <!-- 访问网络,网络定位需要上网 --> 73 <uses-permission android:name="android.permission.INTERNET" /> 74 <!-- SD卡读取权限,用户写入离线定位数据 --> 75 <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" > 76 </uses-permission> 77 <!-- 允许应用读取低级别的系统日志文件 --> 78 <uses-permission android:name="android.permission.READ_LOGS" > 79 </uses-permission> 80 81 </manifest>
4.代码:
activity_main.xml:
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 tools:context="com.zzw.baidumappoint.MainActivity" > 6 7 <com.baidu.mapapi.map.MapView 8 android:id="@+id/mapView" 9 android:layout_width="match_parent" 10 android:layout_height="match_parent" /> 11 12 </RelativeLayout>
MainActivity.java:
1 package com.zzw.baidumappoint; 2 3 import com.baidu.location.BDLocation; 4 import com.baidu.location.BDLocationListener; 5 import com.baidu.location.LocationClient; 6 import com.baidu.location.LocationClientOption; 7 import com.baidu.mapapi.SDKInitializer; 8 import com.baidu.mapapi.map.BaiduMap; 9 import com.baidu.mapapi.map.MapStatusUpdate; 10 import com.baidu.mapapi.map.MapStatusUpdateFactory; 11 import com.baidu.mapapi.map.MapView; 12 import com.baidu.mapapi.map.MyLocationData; 13 import com.baidu.mapapi.model.LatLng; 14 15 import android.app.Activity; 16 import android.os.Bundle; 17 18 public class MainActivity extends Activity { 19 20 private LocationClient mLocationClient; 21 private BaiduMap mBaiduMap; 22 23 @Override 24 protected void onCreate(Bundle savedInstanceState) { 25 super.onCreate(savedInstanceState); 26 SDKInitializer.initialize(getApplicationContext());//是程序不崩溃的作用 27 setContentView(R.layout.activity_main); 28 29 // 获取LocationClient 30 mLocationClient = new LocationClient(this); 31 32 LocationClientOption option = new LocationClientOption(); 33 option.setCoorType("bd09ll"); 34 mLocationClient.setLocOption(option); 35 36 // 获取BaiduMap 37 MapView mapView = (MapView) findViewById(R.id.mapView); 38 mBaiduMap = mapView.getMap(); 39 40 // 显示出当前位置的小图标 41 mBaiduMap.setMyLocationEnabled(true); 42 43 MyLocationListener mListener = new MyLocationListener(); 44 mLocationClient.registerLocationListener(mListener); 45 mLocationClient.start(); 46 } 47 48 @Override 49 protected void onDestroy() { 50 mLocationClient.stop(); 51 super.onDestroy(); 52 } 53 54 private class MyLocationListener implements BDLocationListener { 55 56 @Override 57 public void onReceiveLocation(BDLocation location) { 58 59 // 只是完成了定位 60 MyLocationData locData = new MyLocationData.Builder().latitude(location.getLatitude()) 61 .longitude(location.getLongitude()).build(); 62 63 //设置图标在地图上的位置 64 mBaiduMap.setMyLocationData(locData); 65 66 // 开始移动百度地图的定位地点到中心位置 67 LatLng ll = new LatLng(location.getLatitude(), location.getLongitude()); 68 MapStatusUpdate u = MapStatusUpdateFactory.newLatLngZoom(ll, 16.0f); 69 mBaiduMap.animateMapStatus(u); 70 } 71 72 } 73 74 }
来源:https://www.cnblogs.com/zzw1994/p/5008134.html