Android百度地图开发——学习历程(二)
2017-04-22 17:50
344 查看
步骤:
1.注册百度地图开发者平台,申请API_KEY,下载定位SDK,2.把SDK中libs中的文件全部复制到项目中的libs文件夹下。。。。。(此步骤请参考我的上一个博客)。
3.在Androidmanifest 文件中加入一下代码, 要注意接入的位置,是在第一个Application中哦
<service android:name="com.baidu.location.f" android:enabled="true" android:process=":remote"> </service>
然后我贴一个完整的代码和布局文件吧,
MainActivity:
public class MainActivity extends Activity implements OnClickListener{ private TextView mText; private LocationClient mLocationClient = null; private BDLocationListener myListener = new MyLocationListener(); private MapView mMapView; public BaiduMap mBaiduMap; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); SDKInitializer.initialize(getApplicationContext()); setContentView(R.layout.activity_main); mLocationClient = new LocationClient(getApplicationContext()); // 声明LocationClient类 mLocationClient.registerLocationListener(myListener); // 注册监听函数 initWidgets(); setLocationOption(); mLocationClient.start();// 开始定位; mMapView = (MapView) findViewById(R.id.bmapView); mBaiduMap = mMapView.getMap(); } private void initWidgets() { mText = (TextView) findViewById(R.id.tv_text); Button btn = (Button) findViewById(R.id.btn_request); btn.setOnClickListener(this); } @Override protected void onDestroy() { super.onDestroy(); mLocationClient.stop();// 停止定位 } private void setLocationOption() { LocationClientOption option = new LocationClientOption(); option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy); //可选,默认高精度,设置定位模式,高精度,低功耗,仅设备 option.setCoorType("bd09ll"); //可选,默认gcj02,设置返回的定位结果坐标系 int span=1000; option.setScanSpan(span); //可选,默认0,即仅定位一次,设置发起定位请求的间隔需要大于等于1000ms才是有效的 option.setIsNeedAddress(true); //可选,设置是否需要地址信息,默认不需要 option.setOpenGps(true); //可选,默认false,设置是否使用gps option.setLocationNotify(true); //可选,默认false,设置是否当GPS有效时按照1S/1次频率输出GPS结果 option.setIsNeedLocationDescribe(true); //可选,默认false,设置是否需要位置语义化结果,可以在BDLocation.getLocationDescribe里得到,结果类似于“在北京天安门附近” option.setIsNeedLocationPoiList(true); //可选,默认false,设置是否需要POI结果,可以在BDLocation.getPoiList里得到 option.setIgnoreKillProcess(false); //可选,默认true,定位SDK内部是一个SERVICE,并放到了独立进程,设置是否在stop的时候杀死这个进程,默认不杀死 option.SetIgnoreCacheException(false); //可选,默认false,设置是否收集CRASH信息,默认收集 option.setEnableSimulateGps(false); //可选,默认false,设置是否需要过滤GPS仿真结果,默认需要 mLocationClient.setLocOption(option); } public class MyLocationListener implements BDLocationListener { @Override public void onReceiveLocation(BDLocation location) { if (location == null) return; StringBuffer sb = new StringBuffer(256); sb.append("当前时间 : "); sb.append(location.getTime()); sb.append("\n错误码 : "); sb.append(location.getLocType()); sb.append("\n纬度 : "); sb.append(location.getLatitude()); sb.append("\n经度 : "); sb.append(location.getLongitude()); sb.append("\n半径 : "); sb.append(location.getRadius()); if (location.getLocType() == BDLocation.TypeGpsLocation) {//GPS sb.append("\n速度 : "); sb.append(location.getSpeed()); sb.append("\n卫星数 : "); sb.append(location.getSatelliteNumber()); } else if (location.getLocType() == BDLocation.TypeNetWorkLocation) {//网络定位 sb.append("\n地址 : "); sb.append(location.getAddrStr()); } mText.setText(sb.toString()); //设置地图中心 LatLng cenpt = new LatLng(location.getLatitude(),location.getLongitude());//定义一个坐标点 //定义地图状态 MapStatus mMapStatus = new MapStatus.Builder() .target(cenpt) .zoom(18) .build(); //定义MapStatusUpdate对象,以便描述地图状态将要发生的变化 MapStatusUpdate mMapStatusUpdate = MapStatusUpdateFactory.newMapStatus(mMapStatus); //改变地图状态 mBaiduMap.setMapStatus(mMapStatusUpdate); //添加覆盖物 LatLng point = new LatLng(location.getLatitude(), location.getLongitude()); //构建Marker图标 BitmapDescriptor bitmap = BitmapDescriptorFactory .fromResource(R.drawable.mark1); //构建MarkerOption,用于在地图上添加Marker OverlayOptions option = new MarkerOptions() .position(point) .icon(bitmap); //在地图上添加Marker,并显示 mBaiduMap.addOverlay(option); } @Override public void onConnectHotSpotMessage(String s, int i) { //回调连接wifi是否是移动热点 } } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_request: if (mLocationClient != null ){ mLocationClient.registerLocationListener(myListener); if(mLocationClient.isStarted()==false){ mLocationClient.start(); } mLocationClient.requestLocation(); } break; } } }
布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="zsw.com.locationtest1.MainActivity" > <com.baidu.mapapi.map.MapView android:layout_weight="1" android:id="@+id/bmapView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:clickable="true" /> <Button android:text="定位" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/btn_request" /> <TextView android:layout_weight="3" android:text="TextView" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/tv_text" /> </LinearLayout>
manifest文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="zsw.com.locationtest1">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<service android:name="com.baidu.location.f" android:enabled="true" android:process=":remote"> </service>
<meta-data
android:name="com.baidu.lbsapi.API_KEY"
android:value="G8CCDPLQTEEuOEMpO8Ru90HDowmNUXUP" />
//key:开发者申请的Key
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<!-- 这个权限用于进行网络定位 -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<!-- 这个权限用于访问GPS定位 -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!-- 用于访问wifi网络信息,wifi信息会用于进行网络定位 -->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<!-- 获取运营商信息,用于支持提供运营商信息相关的接口 -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- 这个权限用于获取wifi的获取权限,wifi信息会用来进行网络定位 -->
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<!-- 用于读取手机当前的状态 -->
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<!-- 写入扩展存储,向扩展卡写入数据,用于写入离线定位数据 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- 访问网络,网络定位需要上网 -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- SD卡读取权限,用户写入离线定位数据 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
</manifest>
总结:如果发现自己定位到了非洲,如果Android版本是6.0以上,而因为定位权限是敏感权限,需要动态授权,如果过你用的是模拟器的话,也有可能会定位到非洲的,所以请用真机测试。
相关文章推荐
- Android百度地图开发一学习历程(一)
- Android开发历程_7(ListView和ProgressBar控件的学习)
- 使用百度地图开发一个导航定位demo-android学习之旅(77)
- Android百度地图开发学习笔记(一)之HelloMap地图显示学习心得
- Android学习——百度地图开发定位与显示Demo
- android-百度地图开发学习1
- Android百度地图开发学习笔记(三)之添加Marker和两点测距
- 使用百度地图开发一个导航定位demo-android学习之旅(77)
- Android开发学习之路--百度地图之初体验
- 轻松学习android百度地图开发(一)
- 我的Android学习开发历程--HelloWorld项目的创建
- 使用百度地图开发一个导航定位demo-android学习之旅(77)
- 轻松学习android百度地图开发(三)
- Android学习——百度地图开发定位与显示Demo
- Android百度地图开发学习笔记(四)之POI检索并显示在listview中
- 最快学习百度地图android开发的方法探讨--从官方例子开始之问题处理-例子分割
- 轻松学习android百度地图开发(二)
- 使用百度地图开发一个导航定位demo-android学习之旅(77)
- android学习杂记(3)--百度地图开发环境的配置
- 我将用一个月学会android并开发一个应用,这篇主要用来记录自己的学习历程