您的位置:首页 > 其它

安卓高德地图开发自定义线路规划(按着自己定义的经纬度规划线路)

2015-07-09 14:35 330 查看
原理在之前的博客已经说明了,这个具体的过程就不多说了,这里我直接贴上代码:1:设置的经纬度起始点和终点的经纬度;
private LatLonPoint startPoint = new LatLonPoint(34.185642, 108.881905);
private LatLonPoint endPoint = new LatLonPoint(34.214397, 108.963448);

private LatLonPoint lat_one = new LatLonPoint(34.215748, 108.891869);
private LatLonPoint lat_two = new LatLonPoint(34.233685, 108.900635);
private LatLonPoint lat_three = new LatLonPoint(34.225311, 108.930992);
private LatLonPoint lat_four = new LatLonPoint(34.224136, 108.955284);

// 旺座现代城:34.215748,108.891869
// 亚美大厦:34.233685,108.900635
// 吉祥村:34.225311,108.930992
// 陕西历史博物馆:34.224136,108.955284
2:主代码
package com.example.yngaodethreemap;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.amap.api.maps.AMap;
import com.amap.api.maps.AMap.InfoWindowAdapter;
import com.amap.api.maps.AMap.OnInfoWindowClickListener;
import com.amap.api.maps.AMap.OnMapClickListener;
import com.amap.api.maps.AMap.OnMarkerClickListener;
import com.amap.api.maps.MapView;
import com.amap.api.maps.MapsInitializer;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.overlay.DrivingRouteOverlay;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.geocoder.GeocodeAddress;
import com.amap.api.services.geocoder.GeocodeQuery;
import com.amap.api.services.geocoder.GeocodeResult;
import com.amap.api.services.geocoder.GeocodeSearch;
import com.amap.api.services.geocoder.GeocodeSearch.OnGeocodeSearchListener;
import com.amap.api.services.geocoder.RegeocodeQuery;
import com.amap.api.services.geocoder.RegeocodeResult;
import com.amap.api.services.poisearch.PoiItemDetail;
import com.amap.api.services.poisearch.PoiResult;
import com.amap.api.services.poisearch.PoiSearch.OnPoiSearchListener;
import com.amap.api.services.route.BusRouteResult;
import com.amap.api.services.route.DrivePath;
import com.amap.api.services.route.DriveRouteResult;
import com.amap.api.services.route.RouteSearch;
import com.amap.api.services.route.RouteSearch.DriveRouteQuery;
import com.amap.api.services.route.RouteSearch.OnRouteSearchListener;
import com.amap.api.services.route.WalkRouteResult;

/**
* 班车线路
*
* @author wangxuan
*
*/
public class MainActivity extends Activity implements OnGeocodeSearchListener, OnClickListener, OnMarkerClickListener,
OnMapClickListener, OnInfoWindowClickListener, InfoWindowAdapter, OnPoiSearchListener, OnRouteSearchListener {
private ProgressDialog progDialog = null;
private GeocodeSearch geocoderSearch;
private String addressName;

private AMap aMap;
private MapView mapView;

private int drivingMode = RouteSearch.DrivingDefault;// 驾车默认模式
private DriveRouteResult driveRouteResult;// 驾车模式查询结果
private int routeType = 1;// 1代表公交模式,2代表驾车模式,3代表步行模式
private LatLonPoint startPoint = new LatLonPoint(34.185642, 108.881905);
private LatLonPoint endPoint = new LatLonPoint(34.214397, 108.963448);

private LatLonPoint lat_one = new LatLonPoint(34.215748, 108.891869);
private LatLonPoint lat_two = new LatLonPoint(34.233685, 108.900635);
private LatLonPoint lat_three = new LatLonPoint(34.225311, 108.930992);
private LatLonPoint lat_four = new LatLonPoint(34.224136, 108.955284);

// 旺座现代城:34.215748,108.891869
// 亚美大厦:34.233685,108.900635
// 吉祥村:34.225311,108.930992
// 陕西历史博物馆:34.224136,108.955284

private String[] station_name = new String[] { "中兴产业园", "旺座现代城", "亚美大厦", "吉祥村", "陕西历史博物馆", "大雁塔" };

private List<LatLonPoint> list_latLatLonPoints = new ArrayList<LatLonPoint>();

private RouteSearch routeSearch;
public ArrayAdapter<String> aAdapter;

private LatLonPoint start_msg;
private LatLonPoint end_msg;
private List<ShowBanCheMessage> list_showBanCheMessages = new ArrayList<ShowBanCheMessage>();

/**
* 页面标题
*/
private TextView titleTv;

@Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.activity_main);
geocoderSearch = new GeocodeSearch(this);
geocoderSearch.setOnGeocodeSearchListener(this);
list_latLatLonPoints.add(lat_one);
list_latLatLonPoints.add(lat_two);
list_latLatLonPoints.add(lat_three);
list_latLatLonPoints.add(lat_four);

/*
* 设置离线地图存储目录,在下载离线地图或初始化地图设置; 使用过程中可自行设置, 若自行设置了离线地图存储的路径,
* 则需要在离线地图下载和使用地图页面都进行路径设置
*/
// Demo中为了其他界面可以使用下载的离线地图,使用默认位置存储,屏蔽了自定义设置
MapsInitializer.sdcardDir = getSdCacheDir(this);
mapView = (MapView) findViewById(R.id.map);
mapView.onCreate(bundle);// 此方法必须重写
init();

searchRouteResult(startPoint, endPoint);
}

/**
* 初始化AMap对象
*/
private void init() {
if (aMap == null) {
aMap = mapView.getMap();
registerListener();
}
routeSearch = new RouteSearch(this);
routeSearch.setRouteSearchListener(this);
}

/**
* 响应地理编码
*/
public void getLatlon(final String name) {
GeocodeQuery query = new GeocodeQuery(name, "029");// 第一个参数表示地址,第二个参数表示查询城市,中文或者中文全拼,citycode、adcode,
geocoderSearch.getFromLocationNameAsyn(query);// 设置同步地理编码请求
}

/**
* 响应逆地理编码
*/
public void getAddress(final LatLonPoint latLonPoint) {
RegeocodeQuery query = new RegeocodeQuery(latLonPoint, 200, GeocodeSearch.AMAP);// 第一个参数表示一个Latlng,第二参数表示范围多少米,第三个参数表示是火系坐标系还是GPS原生坐标系
geocoderSearch.getFromLocationAsyn(query);// 设置同步逆地理编码请求
}

/**
* 地理编码查询回调
*/
@Override
public void onGeocodeSearched(GeocodeResult result, int rCode) {
if (rCode == 0) {
if (result != null && result.getGeocodeAddressList() != null && result.getGeocodeAddressList().size() > 0) {
GeocodeAddress address = result.getGeocodeAddressList().get(0);
address.getLatLonPoint();

// if (flag == 0) {
// start_msg = address.getLatLonPoint();
// System.out.println("========start_msg======:"+start_msg);
// } else {
// end_msg = address.getLatLonPoint();
// System.out.println("=======end_msg=======:"+end_msg);
// }
//
// if (start_msg != null && end_msg != null) {
// searchRouteResult(start_msg, end_msg);
// }

addressName = "经纬度值:" + address.getLatLonPoint() + "\n位置描述:" + address.getFormatAddress();
ToastUtil.show(MainActivity.this, start_msg + "," + end_msg);
} else {
ToastUtil.show(MainActivity.this, R.string.no_result);
}
} else if (rCode == 27) {
ToastUtil.show(MainActivity.this, R.string.error_network);
} else if (rCode == 32) {
ToastUtil.show(MainActivity.this, R.string.error_key);
} else {
ToastUtil.show(MainActivity.this, getString(R.string.error_other) + rCode);
}
}

/**
* 逆地理编码回调
*/
@Override
public void onRegeocodeSearched(RegeocodeResult result, int rCode) {
if (rCode == 0) {
if (result != null && result.getRegeocodeAddress() != null && result.getRegeocodeAddress().getFormatAddress() != null) {
addressName = result.getRegeocodeAddress().getFormatAddress() + "附近";
ToastUtil.show(MainActivity.this, addressName);
} else {
ToastUtil.show(MainActivity.this, R.string.no_result);
}
} else if (rCode == 27) {
ToastUtil.show(MainActivity.this, R.string.error_network);
} else if (rCode == 32) {
ToastUtil.show(MainActivity.this, R.string.error_key);
} else {
ToastUtil.show(MainActivity.this, getString(R.string.error_other) + rCode);
}
}

@Override
public void onInfoWindowClick(Marker marker) {
}

@Override
public boolean onMarkerClick(Marker marker) {
if (marker.isInfoWindowShown()) {
marker.hideInfoWindow();
} else {
marker.showInfoWindow();
}
return false;
}

@Override
public void onMapClick(LatLng latng) {
}

@Override
public View getInfoContents(Marker marker) {
return null;
}

@Override
public View getInfoWindow(Marker marker) {
return null;
}

/**
* 注册监听
*/
private void registerListener() {
aMap.setOnMapClickListener(MainActivity.this);
aMap.setOnMarkerClickListener(MainActivity.this);
aMap.setOnInfoWindowClickListener(MainActivity.this);
aMap.setInfoWindowAdapter(MainActivity.this);
}

/**
* 显示进度框
*/
private void showProgressDialog() {
if (progDialog == null)
progDialog = new ProgressDialog(this);
progDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progDialog.setIndeterminate(false);
progDialog.setCancelable(true);
progDialog.setMessage("正在搜索");
progDialog.show();
}

/**
* 隐藏进度框
*/
private void dissmissProgressDialog() {
if (progDialog != null) {
progDialog.dismiss();
}
}

/**
* 开始搜索路径规划方案
*/
public void searchRouteResult(LatLonPoint startPoint, LatLonPoint endPoint) {
showProgressDialog();

final RouteSearch.FromAndTo fromAndTo = new RouteSearch.FromAndTo(startPoint, endPoint);
// 驾车路径规划
// 第一个参数表示路径规划的起点和终点,第二个参数表示驾车模式,第三个参数表示途经点,第四个参数表示避让区域,第五个参数表示避让道路

DriveRouteQuery query = new DriveRouteQuery(fromAndTo, drivingMode, list_latLatLonPoints, null, "");
routeSearch.calculateDriveRouteAsyn(query);// 异步路径规划驾车模式查询
}

@Override
public void onPoiItemDetailSearched(PoiItemDetail arg0, int arg1) {

}

/**
* POI搜索结果回调
*/
@Override
public void onPoiSearched(PoiResult result, int rCode) {
}

/**
* 驾车结果回调
*/
@Override
public void onDriveRouteSearched(DriveRouteResult result, int rCode) {
dissmissProgressDialog();
if (rCode == 0) {
if (result != null && result.getPaths() != null && result.getPaths().size() > 0) {
driveRouteResult = result;
DrivePath drivePath = driveRouteResult.getPaths().get(0);
aMap.clear();// 清理地图上的所有覆盖物
DrivingRouteOverlay drivingRouteOverlay = new DrivingRouteOverlay(this, aMap, drivePath, driveRouteResult.getStartPos(),
driveRouteResult.getTargetPos());
drivingRouteOverlay.removeFromMap();
drivingRouteOverlay.addToMap();
drivingRouteOverlay.zoomToSpan();
} else {
ToastUtil.show(MainActivity.this, R.string.no_result);
}
} else if (rCode == 27) {
ToastUtil.show(MainActivity.this, R.string.error_network);
} else if (rCode == 32) {
ToastUtil.show(MainActivity.this, R.string.error_key);
} else {
ToastUtil.show(MainActivity.this, getString(R.string.error_other) + rCode);
}
}

/**
* 公交路线查询回调
*/
@Override
public void onBusRouteSearched(BusRouteResult result, int rCode) {
}

/**
* 步行路线结果回调
*/
@Override
public void onWalkRouteSearched(WalkRouteResult result, int rCode) {
}

public String getSdCacheDir(Context context) {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
java.io.File fExternalStorageDirectory = Environment.getExternalStorageDirectory();
java.io.File autonaviDir = new java.io.File(fExternalStorageDirectory, "amapsdk");
boolean result = false;
if (!autonaviDir.exists()) {
result = autonaviDir.mkdir();
}
java.io.File minimapDir = new java.io.File(autonaviDir, "offlineMap");
if (!minimapDir.exists()) {
result = minimapDir.mkdir();
}
return minimapDir.toString() + "/";
} else {
return "";
}

}

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub

}

}
</pre><pre name="code" class="html">3:源码给大家学习,不懂的加群

261742462一起交流

</pre><pre name="code" class="html">高德地图自定义线路规划源码:
<span style="color:#ff0000;"><strong>http://download.csdn.net/detail/u014388322/8883913</strong></span>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: