现在百度给的借口已经能够很容易的实现导航了,这里进步解耦合,实现0耦合导航
效果图就不说了,因为用过百度的应该都知道吧,嘿嘿!!
上一篇解决window出现的问题,传送门;
然后来看看如何实现解耦合的,
public class NaviWindow implements OnClickListener {
private Context context;
private LatLng latLng;
private static PopupWindow pop;
private Button btnWalk, btnBike, btnDrive, btnBus;
public NaviWindow(Context context) {
if (context == null)
return;
this.context = context;
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.navi_window, null);
pop = new PopupWindow(view, LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, true);
pop.setAnimationStyle(R.style.PopupAnimation);
pop.setFocusable(true);
ColorDrawable dw = new ColorDrawable(0x00000000);
pop.setBackgroundDrawable(dw);
init(view);// 加载子控件
}
private void init(View view) {
btnBike = (Button) view.findViewById(R.id.btn_navi_bike);
btnWalk = (Button) view.findViewById(R.id.btn_navi_walk);
btnDrive = (Button) view.findViewById(R.id.btn_navi_drive);
btnBus = (Button) view.findViewById(R.id.btn_navi_bus);
btnWalk.setOnClickListener(this);
btnBike.setOnClickListener(this);
btnDrive.setOnClickListener(this);
btnBus.setOnClickListener(this);
}
public synchronized static PopupWindow getInstancte(){
return pop;
}
/**
* @param ll设置目的坐标
*/
public void setLatLng(LatLng ll) {
this.latLng = ll;
}
@Override
public void onClick(View v) {
// transit、driving、walking,riding
// 分别表示公交、驾车、步行和骑行
String model = "";
switch (v.getId()) {
case R.id.btn_navi_bike:
model = "riding";
break;
case R.id.btn_navi_walk:
model = "walking";
break;
case R.id.btn_navi_drive:
model = "driving";
break;
case R.id.btn_navi_bus:
model = "transit";
break;
}
if (latLng != null) {
Navi navi = new Navi(context, latLng, model);
getInstancte().dismiss();
navi.startNavi();
} else {
Toast.makeText(context, "设置目标位置失败", Toast.LENGTH_SHORT).show();
}
}
}
实例唯一,看出来了吧getInstancte()通过这样,我们在地图上出现了window,点击window的选择操作导航方式,那么接下来神奇的事情发生了,
上代码:
public class Navi {
private String navi;
private Context context;
public Navi(Context context, LatLng ll) {
if (context != null && ll != null) {// 内部导航跳转
this.context = context;
navi = "intent://map/navi?location=" + ll.latitude + ","
+ ll.longitude + "&type=BLK"
+ "&src=thirdapp.navi.yourCompanyName.yourAppName"
+ "#Intent;scheme=bdapp;package=com.baidu.BaiduMap;end";
}
}
public Navi(Context context, LatLng ll, String model) {
if (context == null)
return;
this.context = context;
if (Consts.CurrentGPS != null && ll != null && "".equals(model)) {
navi = "intent://map/direction?"
+ "origin=latlng:" // 起点
+ Consts.CurrentGPS.getLatitude()
+ ","
+ Consts.CurrentGPS.getLongitude()
+ "|name:"
+ "我的位置"
+ "&destination=latlng:"// 终点
+ ll.latitude + "," + ll.longitude + "|name:目的地" + "&mode="
+ model + "&coord_type=bd09ll"
+ "&src=thirdapp.navi.yourCompanyName.yourAppName#Intent;"
+ "scheme=bdapp;package=com.baidu.BaiduMap;end";
} else if (Consts.CurrentGPS == null) {
navi = "intent://map/direction?"
+ "origin="
+ "我的位置"
+ "&destination=latlng:"// 终点
+ ll.latitude + "," + ll.longitude + "|name:目的地" + "&mode="
+ model + "&coord_type=bd09ll"
+ "&src=thirdapp.navi.yourCompanyName.yourAppName#Intent;"
+ "scheme=bdapp;package=com.baidu.BaiduMap;end";
}
}
@SuppressWarnings("deprecation")
public void startNavi() {
try {
Intent intent = Intent.getIntent(navi);
context.startActivity(intent);
} catch (Exception e) {
if (Consts.CurrentGPS == null)
Toast.makeText(context, "当前GPS信号太弱", Toast.LENGTH_SHORT).show();
else
Toast.makeText(context, "请前往商城下载百度地图app", Toast.LENGTH_SHORT)
.show();
}
}
}
两种构造方法?两种方式?
没错,两种:1.是直接进入导航模式,这种情况下只需要一个终点就OK了,这个借助了GPS和百度的离线导航包,才能够实现的
2.需要终点和起点,不要问我起点(Consts.CurrentGPS )怎么来的,自己调;
然后放心大胆的实现吧!!!
实现的重要代码:
在地图操作的Activity中
1.获取window
navi = new NaviWindow(this);
naviWindow = NaviWindow.getInstancte();
initMapListener();// TODO 添加地图监听;
2.写入监听,单击和长按地图的情况
private void initMapListener() {
mBaiduMap.setOnMapClickListener(new OnMapClickListener() {
@Override
public boolean onMapPoiClick(MapPoi arg0) {
return true;
}
@Override
public void onMapClick(LatLng ll) {
MapStatus.Builder builder = new MapStatus.Builder();
builder.target(ll).zoom(20.0f);
mBaiduMap.animateMapStatus(MapStatusUpdateFactory
.newMapStatus(builder.build()));
naviWindow.showAtLocation(mMapView, Gravity.CENTER, 5, 30);
navi.setLatLng(ll);
addDestInfoOverlay(ll);// 添加图标
}
});
mBaiduMap.setOnMapLongClickListener(new OnMapLongClickListener() {
@Override
public void onMapLongClick(final LatLng ll) {
// 设置图标
Toast.makeText(TBinfoActivity.this, "设置目的地成功",
Toast.LENGTH_SHORT).show();
AlertDialog.Builder builder = new Builder(TBinfoActivity.this)
.setMessage("确认直接进入导航模式")
.setTitle("提示")
.setPositiveButton("确认",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
Navi navi = new Navi(CurrentThis, ll);
navi.startNavi();
goalMaker = null;
dialog.dismiss();
}
})
.setNegativeButton("取消",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
dialog.dismiss();
}
});
builder.show();
}
});
}
3.顺便把标注的方法加载了吧
/**
* @param goal为指定坐标添加maker
*/
protected void addDestInfoOverlay(LatLng goal) {
if (goalMaker != null) {
goalMaker.remove();
goalMaker = null;
}
// Current_flash.setZIndex(0);
// goal.latitude = goal.latitude-0.001;
MarkerOptions options = new MarkerOptions().position(goal)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.icon_en))
.zIndex(5);
goalMaker = mBaiduMap.addOverlay(options);
}
铛 。铛 。。铛。。注意了了
标注对象的泄露,会在底图上出现重复的问题,so
1.添加标注的时候
if (goalMaker != null) {
goalMaker.remove();
goalMaker = null;
}
2.生命周期的时候
@Override
protected void onPause() {
mySensor.unregist();
if (mMapView != null)
mMapView.onPause();
if (goalMaker != null) {// 清除终点maker,以免对象泄露
goalMaker.remove();
goalMaker = null;
}
MainActivity.mBaiduMap_sub = null;
super.onPause();
}
最后遗留了一个问题,window的style,是一个补间动画,大家自己参考不见动画,然后在res-values-style中加入
<style name="PopupAnimation" mce_bogus="1" parent="android:Animation">
<item name="android:windowEnterAnimation">@anim/popup_enter</item>
<item name="android:windowExitAnimation">@anim/popup_exit</item>
</style>
两个文件我就不上传了,
这个分享window的内容,至于出处已经遗忘,如果有问题,可以邮箱给我,我更改和删除本文都可以!!
最后分享demo资源,有需要的可以下载:
http://download.csdn.net/detail/fengdeweilai/9823520
谢谢大家,愿和小伙伴们共同进步!!