看看这个利用百度地图定位并实现目的地导航的Demo。

首先看实现效果:

                        

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

接下来就看怎样实现了,首先。注冊百度开发人员账号,并进入百度地图API查看相关资料百度地图API,然后就是为须要增加地图的应用注冊APP
KEY,注冊完后,下载百度地图jar文件。新建project。并导入就可以。以下看实现具体代码,在代码中有具体凝视:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
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(); 
                
            
        
}

然后是布局文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?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:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?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>

Android 百度地图API 定位 导航的更多相关文章

  1. 百度地图api定位和导航简写

    function locate() { // 百度地图API功能 var map = new BMap.Map("allmap"); // 创建Map实例 var point = ...

  2. Android 百度地图API(01)_开发环境 HelloBaiduMap

    转载于:http://blog.csdn.net/lmj623565791/article/details/37729091 转载于:http://blog.csdn.net/crazy1235/ar ...

  3. H5微信通过百度地图API实现导航方式一

    根据业务需求修改百度API,实现微信中的导航功能.因为源码中SearchInfoWindow_min.js有点小问题(部分小城市公交线路少,查不到路线时没有提示),所以这里在源码的基础上改了一点点.可 ...

  4. 百度地图API定位+显示位置

    1. 先在需要嵌入地图的页面引入map.js <script src="http://api.map.baidu.com/api?v=2.0&ak=你的秘钥"> ...

  5. Android应用中使用百度地图API定位自己的位置(二)

    官方文档:http://developer.baidu.com/map/sdkandev-6.htm#.E7.AE.80.E4.BB.8B3 百度地图SDK为开发人员们提供了例如以下类型的地图覆盖物: ...

  6. 通过百度地图API定位--第三方开源--百度地图(一)

    1.把百度地图定位API(下载地址:http://lbsyun.baidu.com/sdk/download?selected=location)里面的libs复制到自己的项目libs里面 2.进行相 ...

  7. 团队项目:安卓端用百度地图api定位显示跑道

    因为安卓调用api对我来说是一个完全陌生的领域,我在经过很长时间终于弄出来了,这段时间还是很有成效的,我得到了历练. 第一步:注册成为百度开发者 在百度地图开放平台创建应用.地址http://lbsy ...

  8. 百度地图API 定位一直4.9E-324

    使用百度地图Android SDK 7.0定位坐标一直为4.9E-324,网上搜索了很多,但是均未解决我的问题,在此坐下解决记录,在设置权限中将应用添加信任即可!android 6.0 其他版本未测! ...

  9. Android 百度地图SDK 定位

    引用locSDK_6.1.3.jar,切记添加相应的so文件. 1.定位初始化,需要使用getApplicationContext() mLocClient = new LocationClient( ...

随机推荐

  1. C#.Net 如何动态加载与卸载程序集(.dll或者.exe)0-------通过应用程序域AppDomain加载和卸载程序集

    本博客中以“C#.Net 如何动态加载与卸载程序集(.dll或者.exe)”开头的都是引用莫问奴归处 微软装配车的大门似乎只为货物装载敞开大门,却将卸载工人拒之门外.车门的钥匙只有一把,若要获得还需要 ...

  2. hibernate的一对多、多对一详解

    :双向一对多关系,一是关系维护端(owner side),多是关系被维护端(inverse side).在关系被维护端需要通过@JoinColumn建立外键列指向关系维护端的主键列.     publ ...

  3. [原]Unity3D深入浅出 - 常用类的成员变量和成员函数(Tranform、Time、Random、Mathf、Input)

    Transform的成员变量 Transform的成员函数 Time类,获取和时间相关的信息,可用来计算帧速率,调整时间流逝的速度等. Random类,可用来生成随机数,随机点和旋转. Mathf类提 ...

  4. [swustoj 1097] 2014

    2014(1097) 问题描述 今年是2014年,所以小明喜欢2014的每一位数字(即:2,0,1,4),小明想知道在区间[l,r](包括l和r)中有多少个数中含有这4个数字(数字无前缀零). 输入 ...

  5. Maven配置文件Pom.xml详解

    <project xmlns="http://maven.apache.org/POM/4.0.0 "      xmlns:xsi="http://www.w3. ...

  6. 精简版、GHOST版win7,arduino驱动安装失败的解决方法分享

    arduino组件安装驱动不成功,总是提示系统找不到指定文件. 原因是因为精简版缺少了两个关键的系统文件,导致无法安装.mdmcpq.inf  和 usbser.sys 解决方案详见帖子http:// ...

  7. 【转】忙里偷闲写的小例子---读取android根目录下的文件或文件夹

    原文网址:http://www.cnblogs.com/wenjiang/p/3140055.html 最近几天真的是各种意义上的忙,忙着考试,还要忙着课程设计,手上又有外包的项目,另一边学校的项目还 ...

  8. 【转】 COCOS2D-X之使用CURL下载图片的一个简单Demo

    #include"curl/curl.h" #pragma  comment(lib,"libcurl_imp.lib") bool HelloWorld::i ...

  9. linux防火墙启动、停止、查看

    停止防火墙 service iptables stop 启动防火墙 service iptables start 查看防火墙配置 iptables -L -n 修改的内容只是暂时保存在内存中,如果重启 ...

  10. 《深入Java虚拟机学习笔记》- 第2章 平台无关

    Java虚拟机学习笔记(二)平台无关