热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Android百度地图开发——学习历程(二)

步骤:1.注册百度地图开发者平台,申请API_KEY,下载定位SDK,2.把SDK中libs中的文件全部复制到项目中的li

步骤:

1.注册百度地图开发者平台,申请API_KEY,下载定位SDK,
2.把SDK中libs中的文件全部复制到项目中的libs文件夹下。。。。。(此步骤请参考我的上一个博客)。
3.在Androidmanifest 文件中加入一下代码, 要注意接入的位置,是在第一个Application中哦

<service
android:name&#61;"com.baidu.location.f"android:enabled&#61;"true"android:process&#61;":remote">
service>

这里写图片描述

然后我贴一个完整的代码和布局文件吧&#xff0c;


MainActivity:

public class MainActivity extends Activity implements OnClickListener{private TextView mText;private LocationClient mLocationClient &#61; null;private BDLocationListener myListener &#61; new MyLocationListener();private MapView mMapView;public BaiduMap mBaiduMap;&#64;Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);SDKInitializer.initialize(getApplicationContext());setContentView(R.layout.activity_main);mLocationClient &#61; new LocationClient(getApplicationContext()); // 声明LocationClient类mLocationClient.registerLocationListener(myListener); // 注册监听函数initWidgets();setLocationOption();mLocationClient.start();// 开始定位;mMapView &#61; (MapView) findViewById(R.id.bmapView);mBaiduMap &#61; mMapView.getMap();}private void initWidgets() {mText &#61; (TextView) findViewById(R.id.tv_text);Button btn &#61; (Button) findViewById(R.id.btn_request);btn.setOnClickListener(this);}&#64;Overrideprotected void onDestroy() {super.onDestroy();mLocationClient.stop();// 停止定位}private void setLocationOption() {LocationClientOption option &#61; new LocationClientOption();option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);//可选&#xff0c;默认高精度&#xff0c;设置定位模式&#xff0c;高精度&#xff0c;低功耗&#xff0c;仅设备option.setCoorType("bd09ll");//可选&#xff0c;默认gcj02&#xff0c;设置返回的定位结果坐标系int span&#61;1000;option.setScanSpan(span);//可选&#xff0c;默认0&#xff0c;即仅定位一次&#xff0c;设置发起定位请求的间隔需要大于等于1000ms才是有效的option.setIsNeedAddress(true);//可选&#xff0c;设置是否需要地址信息&#xff0c;默认不需要option.setOpenGps(true);//可选&#xff0c;默认false,设置是否使用gpsoption.setLocationNotify(true);//可选&#xff0c;默认false&#xff0c;设置是否当GPS有效时按照1S/1次频率输出GPS结果option.setIsNeedLocationDescribe(true);//可选&#xff0c;默认false&#xff0c;设置是否需要位置语义化结果&#xff0c;可以在BDLocation.getLocationDescribe里得到&#xff0c;结果类似于“在北京天安门附近”option.setIsNeedLocationPoiList(true);//可选&#xff0c;默认false&#xff0c;设置是否需要POI结果&#xff0c;可以在BDLocation.getPoiList里得到option.setIgnoreKillProcess(false);//可选&#xff0c;默认true&#xff0c;定位SDK内部是一个SERVICE&#xff0c;并放到了独立进程&#xff0c;设置是否在stop的时候杀死这个进程&#xff0c;默认不杀死option.SetIgnoreCacheException(false);//可选&#xff0c;默认false&#xff0c;设置是否收集CRASH信息&#xff0c;默认收集option.setEnableSimulateGps(false);//可选&#xff0c;默认false&#xff0c;设置是否需要过滤GPS仿真结果&#xff0c;默认需要mLocationClient.setLocOption(option);}public class MyLocationListener implements BDLocationListener {&#64;Overridepublic void onReceiveLocation(BDLocation location) {if (location &#61;&#61; null)return;StringBuffer sb &#61; 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() &#61;&#61; BDLocation.TypeGpsLocation) {//GPSsb.append("\n速度 : ");sb.append(location.getSpeed());sb.append("\n卫星数 : ");sb.append(location.getSatelliteNumber());} else if (location.getLocType() &#61;&#61; BDLocation.TypeNetWorkLocation) {//网络定位sb.append("\n地址 : ");sb.append(location.getAddrStr());}mText.setText(sb.toString());//设置地图中心LatLng cenpt &#61; new LatLng(location.getLatitude(),location.getLongitude());//定义一个坐标点//定义地图状态MapStatus mMapStatus &#61; new MapStatus.Builder().target(cenpt).zoom(18).build();//定义MapStatusUpdate对象&#xff0c;以便描述地图状态将要发生的变化MapStatusUpdate mMapStatusUpdate &#61; MapStatusUpdateFactory.newMapStatus(mMapStatus);//改变地图状态mBaiduMap.setMapStatus(mMapStatusUpdate);//添加覆盖物LatLng point &#61; new LatLng(location.getLatitude(), location.getLongitude());//构建Marker图标BitmapDescriptor bitmap &#61; BitmapDescriptorFactory.fromResource(R.drawable.mark1);//构建MarkerOption&#xff0c;用于在地图上添加MarkerOverlayOptions option &#61; new MarkerOptions().position(point).icon(bitmap);//在地图上添加Marker&#xff0c;并显示mBaiduMap.addOverlay(option);}&#64;Overridepublic void onConnectHotSpotMessage(String s, int i) {//回调连接wifi是否是移动热点}}&#64;Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.btn_request:if (mLocationClient !&#61; null ){mLocationClient.registerLocationListener(myListener);if(mLocationClient.isStarted()&#61;&#61;false){mLocationClient.start();}mLocationClient.requestLocation();}break;}}
}

布局文件&#xff1a;

<LinearLayout xmlns:android&#61;"http://schemas.android.com/apk/res/android"xmlns:tools&#61;"http://schemas.android.com/tools"android:layout_width&#61;"match_parent"android:layout_height&#61;"match_parent"android:orientation&#61;"vertical"tools:context&#61;"zsw.com.locationtest1.MainActivity" ><com.baidu.mapapi.map.MapView
android:layout_weight&#61;"1"android:id&#61;"&#64;&#43;id/bmapView"android:layout_width&#61;"fill_parent"android:layout_height&#61;"fill_parent"android:clickable&#61;"true" />
<Button
android:text&#61;"定位"android:layout_width&#61;"match_parent"android:layout_height&#61;"wrap_content"android:id&#61;"&#64;&#43;id/btn_request" />
<TextView android:layout_weight&#61;"3"android:text&#61;"TextView"android:layout_width&#61;"match_parent"android:layout_height&#61;"match_parent"android:id&#61;"&#64;&#43;id/tv_text" />
LinearLayout>

manifest文件


<manifest xmlns:android&#61;"http://schemas.android.com/apk/res/android"package&#61;"zsw.com.locationtest1"><application
android:allowBackup&#61;"true"android:icon&#61;"&#64;mipmap/ic_launcher"android:label&#61;"&#64;string/app_name"android:supportsRtl&#61;"true"android:theme&#61;"&#64;style/AppTheme">
<service
android:name&#61;"com.baidu.location.f"android:enabled&#61;"true"android:process&#61;":remote">
service><meta-data
android:name&#61;"com.baidu.lbsapi.API_KEY"android:value&#61;"G8CCDPLQTEEuOEMpO8Ru90HDowmNUXUP" />
//key:开发者申请的Key<activity android:name&#61;".MainActivity"><intent-filter><action android:name&#61;"android.intent.action.MAIN" /><category android:name&#61;"android.intent.category.LAUNCHER" />intent-filter>activity>application><uses-permission android:name&#61;"android.permission.ACCESS_COARSE_LOCATION" /><uses-permission android:name&#61;"android.permission.ACCESS_FINE_LOCATION" /><uses-permission android:name&#61;"android.permission.ACCESS_WIFI_STATE" /><uses-permission android:name&#61;"android.permission.ACCESS_NETWORK_STATE" /><uses-permission android:name&#61;"android.permission.CHANGE_WIFI_STATE" /><uses-permission android:name&#61;"android.permission.READ_PHONE_STATE" /><uses-permission android:name&#61;"android.permission.WRITE_EXTERNAL_STORAGE" /><uses-permission android:name&#61;"android.permission.INTERNET" /><uses-permission android:name&#61;"android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
manifest>

总结&#xff1a;如果发现自己定位到了非洲&#xff0c;如果Android版本是6.0以上&#xff0c;而因为定位权限是敏感权限&#xff0c;需要动态授权&#xff0c;如果过你用的是模拟器的话&#xff0c;也有可能会定位到非洲的&#xff0c;所以请用真机测试。


推荐阅读
author-avatar
此恨缠绵_793
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有