该Demo的意思就是通过该程序去调起手机上已安装了的百度地图android客户端来进行导航,其实,个人觉得该功能实际意义不大,因为如果手机上已安装了百度地图客户端,我们可以直接就使用它进行导航,该程序只是负责去调起百度地图客户端的导航功能,而本程序并不任何导航功能,不过了解一下怎么调用也好,代码如下:
Activity:
package com.home;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import com.baidu.mapapi.navi.BaiduMapAppNotSupportNaviException;
import com.baidu.mapapi.navi.BaiduMapNavigation;
import com.baidu.mapapi.navi.NaviPara;
import com.baidu.platform.comapi.basestruct.GeoPoint;
public class NaviActivity extends Activity {
// 天安门坐标
double mLat1 = 39.915291;
double mLon1 = 116.403857;
// 百度大厦坐标
double mLat2 = 40.056858;
double mLon2 = 116.308194;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navi_demo);
TextView text = (TextView) findViewById(R.id.navi_info);
text.setText(String.format("起点:(%f,%f)\n终点:(%f,%f)", mLat1, mLon1,
mLat2, mLon2));
}
/**
* 开始导航
*
* @param view
*/
public void startNavi(View view) {
int lat = (int) (mLat1 * 1E6);
int lon = (int) (mLon1 * 1E6);
GeoPoint pt1 = new GeoPoint(lat, lon);
lat = (int) (mLat2 * 1E6);
lon = (int) (mLon2 * 1E6);
GeoPoint pt2 = new GeoPoint(lat, lon);
// 构建 导航参数
NaviPara para = new NaviPara();
para.startPoint = pt1;
para.startName = "从这里开始";
para.endPoint = pt2;
para.endName = "到这里结束";
try {
BaiduMapNavigation.openBaiduMapNavi(para, this);
} catch (BaiduMapAppNotSupportNaviException e) {
e.printStackTrace();
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("您尚未安装百度地图app或app版本过低,点击确认安装?");
builder.setTitle("提示");
builder.setPositiveButton("确认", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
BaiduMapNavigation.GetLatestBaiduMapApp(NaviActivity.this);
}
});
builder.setNegativeButton("取消", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.create().show();
}
}
}
布局XML:
<RelativeLayout 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:background="@android:color/white"
tools:context=".NaviDemo" >
<TextView
android:id="@+id/navi_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:textColor="@android:color/black"
android:textSize="16sp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/navi_info"
android:layout_centerHorizontal="true"
android:layout_marginTop="76dp"
android:onClick="startNavi"
android:text="开始导航" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/navi_info"
android:layout_alignParentLeft="true"
android:layout_marginBottom="57dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:text="@string/navi_tip"
android:textColor="@android:color/black"
android:textSize="16sp" />
</RelativeLayout>
String:navi_tip
<string name="navi_tip">本功能为调起百度地图Android客户端(V5.0及以上版本)的导航功能,用户必须已安装百度地图客户端,如果没有安装则提示用户下载安装百度地图客户端。</string>
附上图片效果:
本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。