暂时只会这种导航,实时显示自己的位置,,求其他更好的方法,或api
16lz
2021-01-26
有没有更好的方法解决实时显示自己的位置的导航
<?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>
1 package com.xmb.navigationdemoactivity; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import com.baidu.mapapi.BMapManager; 7 import com.baidu.mapapi.GeoPoint; 8 import com.baidu.mapapi.LocationListener; 9 import com.baidu.mapapi.MKAddrInfo; 10 11 import com.baidu.mapapi.ItemizedOverlay; 12 import com.baidu.mapapi.MKDrivingRouteResult; 13 import com.baidu.mapapi.MKEvent; 14 import com.baidu.mapapi.MKGeneralListener; 15 import com.baidu.mapapi.MKPlanNode; 16 import com.baidu.mapapi.MKPoiResult; 17 import com.baidu.mapapi.MKRoute; 18 import com.baidu.mapapi.MKSearch; 19 import com.baidu.mapapi.MKSearchListener; 20 import com.baidu.mapapi.MKStep; 21 import com.baidu.mapapi.OverlayItem; 22 23 import com.baidu.mapapi.MKTransitRouteResult; 24 import com.baidu.mapapi.MKWalkingRouteResult; 25 import com.baidu.mapapi.MapActivity; 26 import com.baidu.mapapi.MapView; 27 import com.baidu.mapapi.MyLocationOverlay; 28 import com.baidu.mapapi.RouteOverlay; 29 30 import android.graphics.drawable.Drawable; 31 import android.location.Location; 32 import android.os.Bundle; 33 import android.annotation.SuppressLint; 34 import android.app.Activity; 35 import android.util.Log; 36 import android.view.Menu; 37 import android.view.MenuItem; 38 import android.view.View; 39 import android.view.View.OnClickListener; 40 import android.view.Window; 41 import android.widget.*; 42 43 @SuppressLint("ParserError") 44 public class NavigationDemoActivity extends MapActivity { 45 private String mMapKey = "C9DF0F24E9FFCCEB4AF1DA296E8A694B311A3073"; 46 private EditText destinationEditText = null; 47 private Button startNaviButton = null; 48 private MapView mapView = null; 49 private BMapManager mMapManager = null; 50 private MyLocationOverlay myLocationOverlay = null; 51 //onResume时注册此listener,onPause时需要Remove,注意此listener不是Android自带的,是百度API中的 52 private LocationListener locationListener; 53 private MKSearch searchModel; 54 GeoPoint pt; 55 56 @Override 57 public void onCreate(Bundle savedInstanceState) { 58 super.onCreate(savedInstanceState); 59 requestWindowFeature(Window.FEATURE_NO_TITLE); 60 setContentView(R.layout.daohang); 61 destinationEditText = (EditText) this.findViewById(R.id.et_destination); 62 startNaviButton = (Button) this.findViewById(R.id.btn_navi); 63 64 mMapManager = new BMapManager(getApplication()); 65 mMapManager.init(mMapKey, new MyGeneralListener()); 66 mMapManager.getLocationManager().setNoitifyInternal(20, 5); 67 super.initMapActivity(mMapManager); 68 69 mapView = (MapView) this.findViewById(R.id.bmapsView); 70 //设置启用内置的缩放控件 71 mapView.setBuiltInZoomControls(true); 72 //设置在缩放动画过程中也显示overlay,默认为不绘制 73 // mapView.setDrawOverlayWhenZooming(true); 74 //获取当前位置层 75 myLocationOverlay = new MyLocationOverlay(this, mapView); 76 //将当前位置的层添加到地图底层中 77 mapView.getOverlays().add(myLocationOverlay); 78 locationListener= new LocationListener(){ 79 @Override 80 public void onLocationChanged(Location location) { 81 // TODO Auto-generated method stub 82 if (location != null){ 83 //生成GEO类型坐标并在地图上定位到该坐标标示的地点 84 85 Log.i("out","onLocationChanged"); 86 pt = new GeoPoint((int)(location.getLatitude()*1e6), 87 (int)(location.getLongitude()*1e6)); 88 Drawable drawable = getResources().getDrawable(R.drawable.car_green); 89 90 mapView.getOverlays().add(new MyOverLayItem(drawable)); 91 // System.out.println("---"+location.getLatitude() +":"+location.getLongitude()); 92 mapView.getController().animateTo(pt); 93 } 94 } 95 }; 96 97 98 //初始化搜索模块 99 searchModel = new MKSearch();100 //设置路线策略为最短距离101 searchModel.setDrivingPolicy(MKSearch.ECAR_DIS_FIRST);102 searchModel.init(mMapManager, new MKSearchListener() {103 //获取驾车路线回调方法104 @Override105 public void onGetDrivingRouteResult(MKDrivingRouteResult res, int error) {106 // 错误号可参考MKEvent中的定义107 if (error != 0 || res == null) {108 Toast.makeText(NavigationDemoActivity.this, "抱歉,未找到结果", Toast.LENGTH_SHORT).show();109 return;110 }111 RouteOverlay routeOverlay = new RouteOverlay(NavigationDemoActivity.this, mapView);112 // 此处仅展示一个方案作为示例113 MKRoute route = res.getPlan(0).getRoute(0);114 int distanceM = route.getDistance();115 String distanceKm = String.valueOf(distanceM / 1000) +"."+String.valueOf(distanceM % 1000);116 System.out.println("距离:"+distanceKm+"公里---节点数量:"+route.getNumSteps());117 for (int i = 0; i < route.getNumSteps(); i++) {118 MKStep step = route.getStep(i);119 System.out.println("节点信息:"+step.getContent());120 }121 routeOverlay.setData(route);122 mapView.getOverlays().clear();123 mapView.getOverlays().add(routeOverlay);124 mapView.invalidate();125 mapView.getController().animateTo(res.getStart().pt);126 }127 128 //以下两种方式和上面的驾车方案实现方法一样129 @Override130 public void onGetWalkingRouteResult(MKWalkingRouteResult res, int error) {131 //获取步行路线132 }133 134 @Override135 public void onGetTransitRouteResult(MKTransitRouteResult arg0, int arg1) {136 //获取公交线路137 }138 139 140 @Override141 public void onGetAddrResult(MKAddrInfo arg0, int arg1) {142 }143 144 @Override145 public void onGetPoiResult(MKPoiResult arg0, int arg1, int arg2) {146 }147 });148 149 startNaviButton.setOnClickListener(new OnClickListener() {150 151 @Override152 public void onClick(View v) {153 String destination = destinationEditText.getText().toString();154 155 //设置起始地(当前位置)156 MKPlanNode startNode = new MKPlanNode();157 startNode.pt = pt;158 //设置目的地159 MKPlanNode endNode = new MKPlanNode(); 160 endNode.name = destination;161 162 //展开搜索的城市163 String city = getResources().getString(R.string.beijing);164 // System.out.println("----"+city+"---"+destination+"---"+pt);165 searchModel.drivingSearch(city, startNode, city, endNode);166 /* mMapManager.getLocationManager().requestLocationUpdates(167 // 注册定位事件168 new LocationListener(){169 @Override170 public void onLocationChanged(Location location) {171 // TODO Auto-generated method stub172 if (location != null){173 Log.i("out","mMapManager_onLocationChanged");174 //生成GEO类型坐标并在地图上定位到该坐标标示的地点175 pt = new GeoPoint((int)(location.getLatitude()*1e6),176 (int)(location.getLongitude()*1e6));177 Drawable drawable = getResources().getDrawable(R.drawable.ic_launcher);178 179 mapView.getOverlays().add(new MyOverLayItem(drawable));180 // System.out.println("---"+location.getLatitude() +":"+location.getLongitude());181 mapView.getController().animateTo(pt);182 }183 }184 })*/185 //步行路线186 // searchModel.walkingSearch(city, startNode, city, endNode);187 //公交路线188 // searchModel.transitSearch(city, startNode, endNode);189 }190 });191 192 }193 public class MyOverLayItem extends ItemizedOverlay<OverlayItem> {194 195 private List<OverlayItem> list = new ArrayList<OverlayItem>();196 // 定义一组坐标,都是一个double类型定义197 198 199 200 // 用于在地图上标识坐标,用一个图片标注201 public MyOverLayItem(Drawable drawable) {202 super(drawable);203 // TODO Auto-generated constructor stub204 // 第一组数据的在地图上的标注点205 206 list.add(new OverlayItem(pt, "Point1", "Point1"));207 208 populate();//刷新地图的功能209 }210 211 // 返回指定的list集合中每一个坐标212 @Override213 protected OverlayItem createItem(int arg0) {214 // TODO Auto-generated method stub215 return list.get(arg0);216 }217 218 @Override219 public int size() {220 // TODO Auto-generated method stub221 return list.size();222 }223 224 @Override225 public boolean onTap(int i) {226 // TODO Auto-generated method stub227 Toast.makeText(NavigationDemoActivity.this, list.get(i).getSnippet(), 1).show();228 229 return true;230 }231 232 }233 /* 234 class MyLocationListener implements LocationListener {235 236 @Override237 public void onLocationChanged(Location location) {238 // TODO Auto-generated method stub239 if (location != null){240 //生成GEO类型坐标并在地图上定位到该坐标标示的地点241 pt = new GeoPoint((int)(location.getLatitude()*1e6),242 (int)(location.getLongitude()*1e6));243 // System.out.println("---"+location.getLatitude() +":"+location.getLongitude());244 mapView.getController().animateTo(pt);245 }246 }247 // tv1.setText("经度:" + jindu + ",纬度:" + weidu);248 //AllMession =new String[4];249 // AllMession[0]=jingdu.toString();250 // AllMession[1]=weidu.toString();251 // Bundle mydata=new Bundle();252 // Message msg=new Message();253 // mydata.putString("Longltude",String.valueOf(jingdu));254 // mydata.putString("Latitude",String.valueOf(weidu));255 // mydata.putString("accuracy",Accuray); 256 257 // msg.setData(mydata);258 // msg.what=1;259 //Toast.makeText(context, jzdwaddress, Toast.LENGTH_LONG).show();260 // handler.sendMessage(msg);261 262 263 // t1=(TextView)(((Activity)context).findViewById(R.id.label_wz));264 // t1.setText("经度:" + jingdu + "\n纬度:" + weidu);265 // System.out.println("jindu:"+jingdu+" "+"weidu:"+weidu);266 // MKSearch search = new MKSearch();267 // search.init(mapManager, new MyMKSearchListener());268 // search.reverseGeocode(new GeoPoint(jingdu, weidu));269 // 270 // t2=(TextView)(((Activity)context).findViewById(R.id.label_sj));271 // Calendar cal=Calendar.getInstance();272 // t2.setText(cal.getTime().toString());273 // 274 // t3=(TextView)(((Activity)context).findViewById(R.id.label_jd));275 // t3.setText("1118");276 }277 */ 278 @Override279 protected void onResume() {280 mMapManager.getLocationManager().requestLocationUpdates(locationListener);281 myLocationOverlay.enableMyLocation();282 myLocationOverlay.enableCompass(); // 打开指南针283 mMapManager.start();284 super.onResume();285 }286 287 @Override288 protected void onPause() {289 mMapManager.getLocationManager().removeUpdates(locationListener);290 myLocationOverlay.disableMyLocation();//显示当前位置291 myLocationOverlay.disableCompass(); // 关闭指南针292 mMapManager.stop();293 super.onPause();294 }295 296 @Override297 protected boolean isRouteDisplayed() {298 // TODO Auto-generated method stub299 return false;300 }301 302 // 常用事件监听,用来处理通常的网络错误,授权验证错误等303 class MyGeneralListener implements MKGeneralListener {304 @Override305 public void onGetNetworkState(int iError) {306 Log.d("MyGeneralListener", "onGetNetworkState error is "+ iError);307 Toast.makeText(NavigationDemoActivity.this, "您的网络出错啦!",308 Toast.LENGTH_LONG).show();309 }310 311 @Override312 public void onGetPermissionState(int iError) {313 Log.d("MyGeneralListener", "onGetPermissionState error is "+ iError);314 if (iError == MKEvent.ERROR_PERMISSION_DENIED) {315 // 授权Key错误:316 Toast.makeText(NavigationDemoActivity.this, 317 "请在BMapApiDemoApp.java文件输入正确的授权Key!",318 Toast.LENGTH_LONG).show();319 }320 }321 }322 }
更多相关文章
- [置顶] android:autoLink
- android中的LayoutInflater简单运用
- android47
- 【BottomBar】Android底部导航栏V2.0
- Android(安卓)在 TextView 中设置超链接、颜色、字体、图片
- ym——Android从零开始(26)(设置全屏+横屏、相机拍照、视频录制)
- 地图定位
- Android(安卓)高通代码预制apk可卸载,恢复出厂设置apk可恢复 Andr
- Android(安卓)ConstraintLayout 简介