Android 百度地图API 定位 导航 代码

简介: 前几天百度举办了百度世界大会,可惜没去参加,之前参加了百度的开发者大会,在会上也了解了百度的产品,百度现在朝着平台化的方向在发展,感觉很不错,也试用了百度的产品,现在就用着百度的网盘,今天看了下百度地图的开放API,然后做了个Demo,这里分享出来。

前几天百度举办了百度世界大会,可惜没去参加,之前参加了百度的开发者大会,在会上也了解了百度的产品,百度现在朝着平台化的方向在发展,感觉很不错,也试用了百度的产品,现在就用着百度的网盘,今天看了下百度地图的开放API,然后做了个Demo,这里分享出来。如果应用主要针对国内市场的话,用百度地图还是一个比较不错的选择。另外,百度还有一个PCS(Personal cloud storage)个人云存储,我比较感兴趣,之后也会继续研究,然后做个Demo分享给大家。今天就先看看这个利用百度地图定位并实现目的地导航的Demo。首先看实现效果:

         Android百度地图API实现定位与目的地导航                        Android百度地图API实现定位与目的地导航

进入后首先会得到当前位置,在地图上显示出来,在输入框中输入目的地后,就会在地图上出现最佳线路,我这里设置的是距离最小的驾车线路,另外还有公交线路、步行线路,在代码中都有详细注释。另外,在控制台还输出了线路上每一个节点的信息以及起始位置和目的地的距离,信息显示的是在当前节点的导航信息。如下图:

       Android百度地图API实现定位与目的地导航

 

接下来就看如何实现了,首先,注册百度开发者账号,并进入百度地图API查看相关资料百度地图API,然后就是为需要加入地图的应用注册APP KEY,注册完后,下载百度地图jar文件,新建工程,并导入即可,下面看实现具体代码,在代码中有详细注释:

public class NavigationDemoActivity extends MapActivity {
	private String mMapKey = "注册自己的key";
	private EditText destinationEditText = null;
	private Button startNaviButton = null;
	private MapView mapView = null;
	private BMapManager mMapManager = null;
	private MyLocationOverlay myLocationOverlay = null;
	//onResume时注册此listener,onPause时需要Remove,注意此listener不是Android自带的,是百度API中的
	private LocationListener locationListener;
	private MKSearch searchModel;
	GeoPoint pt;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);
        destinationEditText = (EditText) this.findViewById(R.id.et_destination);
        startNaviButton = (Button) this.findViewById(R.id.btn_navi);
        
        mMapManager = new BMapManager(getApplication());
        mMapManager.init(mMapKey, new MyGeneralListener());
        super.initMapActivity(mMapManager);
        
        mapView = (MapView) this.findViewById(R.id.bmapsView);
        //设置启用内置的缩放控件
        mapView.setBuiltInZoomControls(true);  
        //设置在缩放动画过程中也显示overlay,默认为不绘制
//        mapView.setDrawOverlayWhenZooming(true);
        //获取当前位置层
        myLocationOverlay = new MyLocationOverlay(this, mapView);
        //将当前位置的层添加到地图底层中
        mapView.getOverlays().add(myLocationOverlay);
        
        // 注册定位事件
        locationListener = new LocationListener(){

			@Override
			public void onLocationChanged(Location location) {
				if (location != null){
					//生成GEO类型坐标并在地图上定位到该坐标标示的地点
					 pt = new GeoPoint((int)(location.getLatitude()*1e6),
							(int)(location.getLongitude()*1e6));
//					System.out.println("---"+location.getLatitude() +":"+location.getLongitude());
					mapView.getController().animateTo(pt);
				}
			}
        };
        
        //初始化搜索模块
        searchModel = new MKSearch();
        //设置路线策略为最短距离
        searchModel.setDrivingPolicy(MKSearch.ECAR_DIS_FIRST);
        searchModel.init(mMapManager, new MKSearchListener() {
			//获取驾车路线回调方法
			@Override
			public void onGetDrivingRouteResult(MKDrivingRouteResult res, int error) {
				// 错误号可参考MKEvent中的定义
				if (error != 0 || res == null) {
					Toast.makeText(NavigationDemoActivity.this, "抱歉,未找到结果", Toast.LENGTH_SHORT).show();
					return;
				}
				RouteOverlay routeOverlay = new RouteOverlay(NavigationDemoActivity.this, mapView);
				
			    // 此处仅展示一个方案作为示例
				MKRoute route = res.getPlan(0).getRoute(0);
				int distanceM = route.getDistance();
				String distanceKm = String.valueOf(distanceM / 1000) +"."+String.valueOf(distanceM % 1000);
				System.out.println("距离:"+distanceKm+"公里---节点数量:"+route.getNumSteps());
				for (int i = 0; i < route.getNumSteps(); i++) {
					MKStep step = route.getStep(i);
					System.out.println("节点信息:"+step.getContent());
				}
			    routeOverlay.setData(route);
			    mapView.getOverlays().clear();
			    mapView.getOverlays().add(routeOverlay);
			    mapView.invalidate();
			    mapView.getController().animateTo(res.getStart().pt);
			}
			
			//以下两种方式和上面的驾车方案实现方法一样
			@Override
			public void onGetWalkingRouteResult(MKWalkingRouteResult res, int error) {
				//获取步行路线
			}
			
			@Override
			public void onGetTransitRouteResult(MKTransitRouteResult arg0, int arg1) {
				//获取公交线路
			}
			
			@Override
			public void onGetBusDetailResult(MKBusLineResult arg0, int arg1) {
			}
			@Override
			public void onGetAddrResult(MKAddrInfo arg0, int arg1) {
			}
			@Override
			public void onGetSuggestionResult(MKSuggestionResult arg0, int arg1) {
			}
			@Override
			public void onGetPoiResult(MKPoiResult arg0, int arg1, int arg2) {
			}
		});
        
        startNaviButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String destination = destinationEditText.getText().toString();
				
				//设置起始地(当前位置)
				MKPlanNode startNode = new MKPlanNode();
				startNode.pt = pt;
				//设置目的地
				MKPlanNode endNode = new MKPlanNode(); 
				endNode.name = destination;
				
				//展开搜索的城市
				String city = getResources().getString(R.string.beijing);
//				System.out.println("----"+city+"---"+destination+"---"+pt);
				searchModel.drivingSearch(city, startNode, city, endNode);
				//步行路线
//				searchModel.walkingSearch(city, startNode, city, endNode);
				//公交路线
//				searchModel.transitSearch(city, startNode, endNode);
			}
		});
        
    }
    
    @Override
	protected void onResume() {
		mMapManager.getLocationManager().requestLocationUpdates(locationListener);
        myLocationOverlay.enableMyLocation();
        myLocationOverlay.enableCompass(); // 打开指南针
		mMapManager.start();
		super.onResume();
	}
    
    @Override
	protected void onPause() {
		mMapManager.getLocationManager().removeUpdates(locationListener);
		myLocationOverlay.disableMyLocation();//显示当前位置
		myLocationOverlay.disableCompass(); // 关闭指南针
		mMapManager.stop();
		super.onPause();
	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}
	
	// 常用事件监听,用来处理通常的网络错误,授权验证错误等
	class MyGeneralListener implements MKGeneralListener {
			@Override
			public void onGetNetworkState(int iError) {
				Log.d("MyGeneralListener", "onGetNetworkState error is "+ iError);
				Toast.makeText(NavigationDemoActivity.this, "您的网络出错啦!",
						Toast.LENGTH_LONG).show();
			}

			@Override
			public void onGetPermissionState(int iError) {
				Log.d("MyGeneralListener", "onGetPermissionState error is "+ iError);
				if (iError ==  MKEvent.ERROR_PERMISSION_DENIED) {
					// 授权Key错误:
					Toast.makeText(NavigationDemoActivity.this, 
							"请在BMapApiDemoApp.java文件输入正确的授权Key!",
							Toast.LENGTH_LONG).show();
				}
			}
		}
}

然后是布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="18sp"
            android:text="Destination:" />

        <EditText
            android:id="@+id/et_destination"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>
    
    <Button 
        android:id="@+id/btn_navi"
        android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:text="Start navigate"/>

    <com.baidu.mapapi.MapView
        android:id="@+id/bmapsView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:clickable="true" />

</LinearLayout>

AndroidMainifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ericssonlabs"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
	<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
	<uses-permission android:name="android.permission.INTERNET"></uses-permission>
	<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
	<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>  
	<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission> 
	<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
	
	<supports-screens android:largeScreens="true"
	    android:normalScreens="true" android:smallScreens="true"
	    android:resizeable="true" android:anyDensity="true"/>
	<uses-sdk android:minSdkVersion="3"></uses-sdk>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".NavigationDemoActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


目录
相关文章
|
1月前
|
Ubuntu 网络协议 Java
【Android平板编程】远程Ubuntu服务器code-server编程写代码
【Android平板编程】远程Ubuntu服务器code-server编程写代码
|
1月前
|
缓存 API 定位技术
使用Python调用百度地图API实现地址查询
使用Python调用百度地图API实现地址查询
100 0
|
3月前
|
API C++
socket编程之常用api介绍与socket、select、poll、epoll高并发服务器模型代码实现(1)
前言   本文旨在学习socket网络编程这一块的内容,epoll是重中之重,后续文章写reactor模型是建立在epoll之上的。
34 0
|
3月前
|
监控 安全 Linux
socket编程之常用api介绍与socket、select、poll、epoll高并发服务器模型代码实现(3)
高并发服务器模型-poll poll介绍   poll跟select类似, 监控多路IO, 但poll不能跨平台。其实poll就是把select三个文件描述符集合变成一个集合了。
36 0
|
1月前
|
Java API PHP
获取1688商品详情API:步骤与代码示例
在电子商务领域,阿里巴巴的1688平台是一个广受商家和开发者欢迎的批发交易市场。若您是一名开发者,希望建立自己的应用程序或网站来获取并展示1688上的商品信息,您可能需要使用到1688提供的API接口。以下是获取1688商品详情API的详细步骤说明。
|
1月前
|
自然语言处理 API 语音技术
Python加百度语音API实现文字转语音功能
Python加百度语音API实现文字转语音功能
52 0
|
1月前
|
JSON 缓存 API
淘宝天猫获取sku详细信息 API 调用文档 及请求代码
淘宝天猫获取SKU详细信息的API调用通常涉及到商品信息的API接口。在淘宝开放平台或天猫开放平台上,你可以找到相关的API文档和调用示例。下面是一个简化的步骤和示例代码来展示如何调用这些API:
|
2月前
|
存储 Oracle Java
Java 包和 API 深度解析:组织代码,避免命名冲突
Java 中的包 用于将相关的类分组在一起。可以将其视为文件目录中的一个文件夹。我们使用包来避免名称冲突,并编写更易于维护的代码。 包分为两类: 内置包(来自 Java API 的包) 用户定义的包(创建自己的包)
309 2
|
2月前
|
弹性计算 前端开发 Java
通义千问API:让大模型写代码和跑代码
基于前面三章的铺垫,本章我们将展示大模型Agent的强大能力。我们不仅要实现让大模型同时使用多种查询工具,还要实现让大模型能查询天气情况,最后让大模型自己写代码来查询天气情况。
59667 448
通义千问API:让大模型写代码和跑代码
|
2月前
|
Ubuntu 网络协议 Linux
【Linux】Android平板上远程连接Ubuntu服务器code-server进行代码开发
【Linux】Android平板上远程连接Ubuntu服务器code-server进行代码开发
57 0