使用百度地图开发一个导航定位demo-android学习之旅(77)
首先介绍如何导入百度地图
步骤(其实官方文档写的很清楚了)http://developer.baidu.com/map/index.php?title=androidsdk/guide/introduction
1.注册开发者账号
2.注册你的应用,登陆控制台,然后输入数字签名和包名,得到开发Id
3. 下载android sdk进行配置,我用的是Android studio,配置步骤是,在app底下的libs下导入,没有的话新建,在src/main/目录下新建jniLibs目录,放下那些搜文件,arm64-v8a,armeabi,armeabi-v7a,x86,x86_64,一共5个文件,这个版本目前是这样,参考网址http://developer.baidu.com/map/index.php?title=androidsdk/guide/buildproject
4.在Manifest添加标签
<application>
<meta-data
android:name="com.baidu.lbsapi.API_KEY"
android:value="开发者 key" />
</application>
5.添加所需权限
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
6.在布局中加入这个控件
<com.baidu.mapapi.map.MapView
android:id="@+id/bmapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true" />
7.在类中初始化代码
public class MainActivity extends Activity {
MapView mMapView = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//在使用SDK各组件之前初始化context信息,传入ApplicationContext
//注意该方法要再setContentView方法之前实现
SDKInitializer.initialize(getApplicationContext());
setContentView(R.layout.activity_main);
//获取地图控件引用
mMapView = (MapView) findViewById(R.id.bmapView);
}
@Override
protected void onDestroy() {
super.onDestroy();
//在activity执行onDestroy时执行mMapView.onDestroy(),实现地图生命周期管理
mMapView.onDestroy();
}
@Override
protected void onResume() {
super.onResume();
//在activity执行onResume时执行mMapView. onResume (),实现地图生命周期管理
mMapView.onResume();
}
@Override
protected void onPause() {
super.onPause();
//在activity执行onPause时执行mMapView. onPause (),实现地图生命周期管理
mMapView.onPause();
}
}
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
我写了一个demo,实现了查看卫星地图,实时路况,以及定位和基于android传感器的方向导航
github地址
https://github.com/fengsehng/BaiduMapDemoTest
截图
public class FlyControlBaiduSdk extends Activity{
private MapView mapView;
private BaiduMap mBaiduMap;
private Button normal,site,traffic,myLocation;
private Context context;
//定位相关
private LocationClient locationClient;
private MyLocationListener myLocationListener;
private boolean isFirstIn = true;
private double myLatitude;
private double myLongtitude;
//自定义定位图标相关
private BitmapDescriptor bitmapDescriptor;
private float mCurrentX;
//传感器相关
private MyOrientationlistener mMyOrientationListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
SDKInitializer.initialize(getApplicationContext());
setContentView(R.layout.flycontrol_baidu);
this.context = this;
initViews();
initLocation();
normal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
}
});
site.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mBaiduMap.setMapType(BaiduMap.MAP_TYPE_SATELLITE);
}
});
traffic.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mBaiduMap.isTrafficEnabled()){
mBaiduMap.setTrafficEnabled(false);
}else {
mBaiduMap.setTrafficEnabled(true);
}
}
});
myLocation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LatLng latLng = new LatLng(myLatitude,myLongtitude);
MapStatusUpdate msu = MapStatusUpdateFactory.newLatLng(latLng);
mBaiduMap.animateMapStatus(msu);
}
});
}
private void initViews(){
mapView = (MapView) findViewById(R.id.bmapView);
MapStatusUpdate msu = MapStatusUpdateFactory.zoomTo(15.0f);
mBaiduMap = mapView.getMap();
mBaiduMap.setMapStatus(msu);
normal = (Button) findViewById(R.id.normal);
site = (Button) findViewById(R.id.site);
traffic = (Button) findViewById(R.id.traffic);
myLocation = (Button) findViewById(R.id.myLocation);
}
private void initLocation(){
locationClient = new LocationClient(this);
myLocationListener = new MyLocationListener();
locationClient.registerLocationListener(myLocationListener);
LocationClientOption locationClientOption = new LocationClientOption();
locationClientOption.setCoorType("bd09ll");
locationClientOption.setIsNeedAddress(true);
locationClientOption.setOpenGps(true);
locationClientOption.setScanSpan(1000);
locationClient.setLocOption(locationClientOption);
//初始化图标
bitmapDescriptor = BitmapDescriptorFactory.fromResource(R.drawable.navii);
mMyOrientationListener = new MyOrientationlistener(this);
mMyOrientationListener.setOnOrientationListener(new MyOrientationlistener.OnOrientationListener() {
@Override
public void onOrientationChange(float x) {
mCurrentX = x;
}
});
}
//初始化定位
@Override
protected void onStart() {
super.onStart();
//开始定位
mBaiduMap.setMyLocationEnabled(true);
if (!locationClient.isStarted()){
locationClient.start();}
//开始方向传感器
mMyOrientationListener.start();
}
@Override
protected void onResume() {
super.onResume();
mapView.onResume();
}
@Override
protected void onPause() {
super.onPause();
mapView.onPause();
}
//停止定位
@Override
protected void onStop() {
super.onStop();
//停止定位
mBaiduMap.setMyLocationEnabled(false);
locationClient.stop();
//停止方向传感器
mMyOrientationListener.stop();
}
@Override
protected void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}
//定位监听类
private class MyLocationListener implements BDLocationListener{
@Override
public void onReceiveLocation(BDLocation bdLocation) {
MyLocationData myLocationData = new MyLocationData.Builder().accuracy(bdLocation.getRadius())//
.direction(mCurrentX).latitude(bdLocation.getLatitude()).longitude(bdLocation.getLongitude()).build();
mBaiduMap.setMyLocationData(myLocationData);
//设置经纬度
myLatitude = bdLocation.getLatitude();
myLongtitude = bdLocation.getLongitude();
//设置定位图标
MyLocationConfiguration configuration = new MyLocationConfiguration(MyLocationConfiguration.LocationMode.NORMAL,true,bitmapDescriptor);
mBaiduMap.setMyLocationConfigeration(configuration);
if (isFirstIn){
LatLng latLng = new LatLng(bdLocation.getLatitude(),bdLocation.getLongitude());
MapStatusUpdate msu = MapStatusUpdateFactory.newLatLng(latLng);
mBaiduMap.animateMapStatus(msu);
isFirstIn = false;
Toast.makeText(context,bdLocation.getAddrStr(),Toast.LENGTH_LONG).show();
}
}
}
}
传感器类代码
public class MyOrientationlistener implements SensorEventListener{
private SensorManager mSensorManager;
private Sensor mSensor;
private Context mContext;
private float lastX;
public MyOrientationlistener(Context context){
this.mContext = context;
}
public void start(){
mSensorManager = (SensorManager) mContext.getSystemService(Context.SENSOR_SERVICE);
if (mSensorManager != null){
mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
}
if (mSensor != null){
mSensorManager.registerListener(this,mSensor,SensorManager.SENSOR_DELAY_UI);
}
}
public void stop(){
mSensorManager.unregisterListener(this);
}
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ORIENTATION){
float x = event.values[SensorManager.DATA_X];
if (Math.abs(x-lastX) > 1.0){
if (onOrientationListener != null){
onOrientationListener.onOrientationChange(x);
}
}
lastX = x;
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
private OnOrientationListener onOrientationListener;
public void setOnOrientationListener(OnOrientationListener onOrientationListener) {
this.onOrientationListener = onOrientationListener;
}
public interface OnOrientationListener{
void onOrientationChange(float x);
}
}
布局代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<com.baidu.mapapi.map.MapView
android:id="@+id/bmapView"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="10"
android:clickable="true" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:text="我的位置"
android:id="@+id/myLocation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/button_back"/>
<Button
android:text="普通地图"
android:id="@+id/normal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/button_back"/>
<Button
android:text="卫星地图"
android:id="@+id/site"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/button_back"/>
<Button
android:text="实时交通"
android:id="@+id/traffic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/button_back"/>
</LinearLayout>
</LinearLayout>
使用百度地图开发一个导航定位demo-android学习之旅(77)的更多相关文章
- Android学习——百度地图开发定位与显示Demo
百度地图给我们提供了很丰富的API供我们进行二次开发.百度地图的SDK与定位SDK在今年6月份进行了更新. 地图更新为3.0,定位更新为4.2.百度说:这次更新对接口有了较大部分的调整,与之前版本号不 ...
- [android] 百度地图开发 (两).所在地的城市定位和城市POI搜索
一个. 百度地图城市位置和POI搜索知识 上一篇文章"百度地图开发(一)"中讲述了怎样申请百度APIKey及解决显示空白网格的问题.该篇文章主要讲述怎样定位城市位置.定 ...
- Android studio 百度地图开发(3)地图导航
Android studio 百度地图开发(3)地图导航 email:chentravelling@163.com 开发环境:win7 64位,Android Studio,请注意是Android S ...
- Android studio 百度地图开发(2)地图定位
Android studio 百度地图开发(2)地图定位 email:chentravelling@163.com 开发环境:win7 64位,Android Studio,请注意是Android S ...
- C#的百度地图开发(四)前端显示与定位
原文:C#的百度地图开发(四)前端显示与定位 有了这些定位信息,那要如何在前端的页面上显示出来呢?这需要用到百度地图的JavaScript的API.下面是示例代码. 前端代码 <%@ Page ...
- 百度地图开发-引入地图SDK并配置 02
百度地图开发-引入地图SDK并配置 02 通过上一篇文章的介绍,基本了解百度地图的基本信息,接下来就让我们一起来实际在项目中操作,显示出地图. 01 引入地图SDK 首先需要新建一个空白的Androi ...
- Android 百度地图开发(一)--- 申请API Key和在项目中显示百度地图
标签: Android百度地图API Key 分类: Android 百度地图开发(2) 最近自己想研究下地图,本来想研究google Map,但是申请API key比较坑爹,于是从百度地 ...
- Android 百度地图开发之一(Hello BaiDu Map)
之前也接触过百度地图的开发,但那是在网上找的案例或代码,而且是比较老的版本.打算重新学习一下百度地图的开发. 本次使用的百度地图的版本是 Android SDK v3.0.0 本篇文章主要讲述百度地图 ...
- C#的百度地图开发(三)依据坐标获取位置、商圈及周边信息
原文:C#的百度地图开发(三)依据坐标获取位置.商圈及周边信息 我们得到了百度坐标,现在依据这一坐标来获取相应的信息.下面是相应的代码 public class BaiduMap { /// < ...
随机推荐
- python学习之路基础篇(第四篇)
一.课程内容回顾 1.python基础 2.基本数据类型 (str|list|dict|tuple) 3.将字符串“老男人”转换成utf-8 s = "老男人" ret = by ...
- java连接sqlserver2008
java连接sqlserver2008时应有sqljdbc4.jar驱动包.连接的示例代码如下: import java.sql.*; public class ConnectSQL { public ...
- C++笔记001:Microsoft Visual Studio 2010软件的安装与建立第一个cpp文件
原创笔记,转载请注明出处! 点击[关注],关注也是一种美德~ 我学习C++使用软件为Microsoft Visual Studio 2010. 首先,软件的安装包 链接:https://pan.bai ...
- git 学习笔记(常用命令)
1.新建一个文件,如果没有使用git add 命令将它提交到暂存区,那么这个文件就还没有被跟踪. 2.通过配置.gitignore文件可以指定要忽略的文件,被忽略的文件夹是不会被提交到暂存区的.所以这 ...
- Python 元组内置函数
Python元组包含了以下内置函数 序号 方法及描述 1 cmp(tuple1, tuple2)比较两个元组元素. 2 len(tuple)计算元组元素个数. 3 max(tuple)返回元组中元素最 ...
- mysql5.7在centos上安装的完整教程以及相关的“坑”
安装前的准备 Step1: 如果你系统已经有mysql,如一般centos自带mysql5.1系列,那么你需要删除它,先检查一下系统是否自带mysql yum list installed | gre ...
- Openstack: aborted: Block Device Mapping is Invalid
Issue: When you create an instance, you may encounter following exception: aborted: Block Device Map ...
- 潜谈IT从业人员在传统IT和互联网之间的择业问题(上)-传统乙方形公司
外包能去吗?项目型公司如何?甲方比乙方好?互联网公司就一定好吗? 相信许多从业者在经历了3-5年的工作期后都会带着这样的疑问或者疑惑. 2012年-2014年间,曾经面试过500人,亲身面试的也有15 ...
- (译)Objective-C 类属性
翻译自:Objective-C Class Properties 译者:Haley_Wong 由于Swift 3.0 出了太多令人兴奋的新特性,人们很容易忽略 Objective-C中的小改动.苹果展 ...
- 没事不要在for循环期间增减迭代序列的成员
>>> arr=[4, 4, 9, 7, 7] >>> for i,a in enumerate(arr): arr.pop(i) print(i,a) 4 0 4 ...