Android 使用高德SDK实现导航笔记

Demo效果图:

Android 使用高德SDK实现导航笔记_第1张图片


Android 使用高德SDK实现导航笔记_第2张图片


Android 使用高德SDK实现导航笔记_第3张图片


Android 使用高德SDK实现导航笔记_第4张图片


实现逻辑

首先使用检索功能获取目的地的经纬度作为终点,使用定位功能获取当前位置的经纬度作为起点。然后将起始点传递给导航模块就可以了。


主要代码:

1.定位:

	/**
	 * 定位成功后回调函数
	 */
	@Override
	public void onLocationChanged(AMapLocation aLocation) {
		if (mListener != null && aLocation != null) {
			double locateLat = aLocation.getLatitude();
			double locateLng = aLocation.getLongitude();

			if (0.0 == locateLat || 0.0 == locateLng) {
				MyLog.e("[Location]Error Location, not update map");
			} else {
				isLocated = true;
				nowLatLng = new LatLng(locateLat, locateLng);
				mListener.onLocationChanged(aLocation); // 显示系统小蓝点
			}
			MyLog.v("[Location]Lat:" + locateLat + ",Lng:" + locateLng);
		}
	}


2.进行POI检索

/**
	 * 开始进行poi搜索
	 */
	protected void doSearchQuery() {
		showProgressDialog(); // 显示进度框
		currentPage = 0; // 重置页码
		query = new PoiSearch.Query(keyWord, "", editCity.getText().toString());// 第一个参数表示搜索字符串,第二个参数表示poi搜索类型,第三个参数表示poi搜索区域(空字符串代表全国)
		query.setPageSize(10); // 设置每页最多返回多少条poiitem
		query.setPageNum(currentPage);// 设置查第一页

		poiSearch = new PoiSearch(this, query);
		poiSearch.setOnPoiSearchListener(this);
		poiSearch.searchPOIAsyn();
	}


3.设置点击InfoWindow动作:

@Override
	public View getInfoWindow(final Marker marker) {
		View view = getLayoutInflater().inflate(R.layout.poikeywordsearch_uri,
				null);
		TextView title = (TextView) view.findViewById(R.id.title);
		title.setText(marker.getTitle());

		TextView snippet = (TextView) view.findViewById(R.id.snippet);
		snippet.setText(marker.getSnippet());
		ImageButton button = (ImageButton) view
				.findViewById(R.id.start_amap_app);

		button.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// 导航
				// 起点终点列表
				ArrayList startPoints = new ArrayList();
				ArrayList endPoints = new ArrayList();

				LatLng endLatLng = marker.getPosition();
				NaviLatLng endNaviLatLng = new NaviLatLng(endLatLng.latitude,
						endLatLng.longitude);
				endPoints.add(endNaviLatLng);

				NaviLatLng startNaviLatLng = new NaviLatLng(nowLatLng.latitude,
						nowLatLng.longitude);
				startPoints.add(startNaviLatLng);

				if (isLocated) {
					// DrivingSaveMoney--省钱
					// DrivingShortDistance--最短距离
					// DrivingNoExpressways--不走高速
					// DrivingFastestTime--最短时间
					// DrivingAvoidCongestion--避免拥堵
					AMapNavi.getInstance(MainActivity.this)
							.calculateDriveRoute(startPoints, endPoints, null,
									AMapNavi.DrivingDefault);
					mRouteCalculatorProgressDialog.show();
				} else {
					Toast.makeText(getApplicationContext(), "未定位",
							Toast.LENGTH_SHORT).show();
				}
			}
		});
		return view;
	}

目前起始点之间路径上的箭头是反向的,后面看一下什么原因。


4.Update:添加模拟导航


Android 使用高德SDK实现导航笔记_第5张图片

	@Override
	public void onCalculateRouteSuccess() {
		mRouteCalculatorProgressDialog.dismiss();
		Intent intent = new Intent(MainActivity.this, SimpleNaviActivity.class);
		intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
		Bundle bundle = new Bundle();
		bundle.putInt(AMapUtil.ACTIVITYINDEX, AMapUtil.SIMPLEGPSNAVI);
		bundle.putBoolean(AMapUtil.ISEMULATOR, isSimulate);
		intent.putExtras(bundle);
		startActivity(intent);
		finish();
	}





你可能感兴趣的:(Android,android,高德导航,sdk,定位,地图)