概述
/**
* 实现地图的加载
* @author 木兰缀露
*对于标记点过多的显示问题,将定位点的经纬度传至服务器的方法内增加减少经纬度,然后好数据库标记点进行经纬度比对,如果有则加载到前台,如果没则扩大加载范围
*/
public class MapActivity extends Activity implements
LocationSource,OnRouteSearchListener,
AMapLocationListener,OnCheckedChangeListener {
private MapView mapView;
private AMap aMap;
private Marker marker2;// 有跳动效果的marker对象
private MarkerOptions markerOption;
private UiSettings mUiSettings;//高德logo,指南针,比例尺等功能的控制
private OnLocationChangedListener mListener;
private AMapLocationClient mlocationClient;
private AMapLocationClientOption mLocationOption;
private DriveRouteResult driveRouteResult;// 驾车模式查询结果
private LatLonPoint startPoint = null;
private LatLonPoint endPoint = null;
private RouteSearch routeSearch;
private int drivingMode = RouteSearch.DrivingDefault;// 驾车默认模式
private Handler handler;
private DrivingRouteOverlay drivingRouteOverlay;//用于清除其他路线
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
mapView=(MapView) findViewById(R.id.map);
mapView.onCreate(savedInstanceState);//这个方法必须重写
init();
Thread th=new Thread(new MyThread());
th.start();
handler=new Handler(){
@Override
public void handleMessage(Message msg) {
if(msg.what==1){
addMarkersToMap();
}else{
toast("failure");
}
}
};
}
public void toast(String st){
Toast.makeText(this, st, Toast.LENGTH_LONG);
}
//实例化map对象
public void init(){
routeSearch = new RouteSearch(this);
routeSearch.setRouteSearchListener(this);
aMap=mapView.getMap();
mUiSettings=aMap.getUiSettings();
aMap.setLocationSource(this);// 设置定位监听
aMap.getUiSettings().setMyLocationButtonEnabled(true);// 设置默认定位按钮是否显示
aMap.setMyLocationEnabled(true);// 设置为true表示显示定位层并可触发定位,false表示隐藏定位层并不可触发定位,默认是false
// 设置定位的类型为定位模式 ,可以由定位、跟随或地图根据面向方向旋转几种
aMap.setMyLocationType(AMap.LOCATION_TYPE_LOCATE);
MyLocationStyle myLocationStyle = new MyLocationStyle();
// myLocationStyle.myLocationIcon(BitmapDescriptorFactory.
// fromResource(R.drawable.icon_loaction_start));// 自定义定位蓝点图标
myLocationStyle.strokeColor(Color.alpha(0));// 自定义精度范围的圆形边框颜色
myLocationStyle.radiusFillColor(Color.alpha(0));//圆圈的颜色,设为透明的时候就可以去掉园区区域了
// 将自定义的 myLocationStyle 对象添加到地图上
aMap.setMyLocationStyle(myLocationStyle);
mUiSettings
.setLogoPosition(AMapOptions.LOGO_POSITION_BOTTOM_RIGHT);//logo显示在底部中间
mUiSettings.setScaleControlsEnabled(true);//显示地图的比例尺
mUiSettings.setZoomControlsEnabled(true);//缩放按钮是否可见
mUiSettings.setCompassEnabled(true);//指南针是否显示
mUiSettings.setScrollGesturesEnabled(true);//是否可以手动滑动
mUiSettings.setZoomGesturesEnabled(true);//是否可手动放缩
mUiSettings.setTiltGesturesEnabled(true);//图形是否可以倾斜
mUiSettings.setRotateGesturesEnabled(true);//是否可以旋转
//监听点击地图面的事件
aMap.setOnMapClickListener(new OnMapClickListener() {
@Override
public void onMapClick(LatLng arg0) {
//获取缩放比例尺
String mZoom = String.valueOf(aMap.getCameraPosition().zoom);
//Toast.makeText(MapActivity.this, mZoom, Toast.LENGTH_LONG).show();
}
});
//addMarkersToMap();//添加marker标记
//监听点击标记点的事件
aMap.setOnMarkerClickListener(new OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
// 点击标记点时显示信息内容
//marker.showInfoWindow();
//计算两个点之间的距离
//LatLng startLatlng = new LatLng(38.89141, 115.381306);
//LatLng endLatlng = new LatLng(38.93141, 115.431306);
// 计算量坐标点距离?????????利用标记的距离实现显示比例尺的长度
//float kk=AMapUtils.calculateLineDistance(startLatlng, endLatlng);
//String st=String.valueOf(kk);
//Toast.makeText(MapActivity.this,"相距是"+ st+"米", Toast.LENGTH_LONG).show();
//float i=kk/1000;
//if(i>6){
//aMap.moveCamera(CameraUpdateFactory.zoomBy(4));//赋值时视野变大
//}else if(i>3&&i<=6){
//aMap.moveCamera(CameraUpdateFactory.zoomBy(3));//赋值时视野变大
//}else{aMap.moveCamera(CameraUpdateFactory.zoomBy(4));//赋值时视野变大
//}
marker.setTitle("最快十分钟");
//路线规
double y=marker.getPosition().latitude;
double x=marker.getPosition().longitude;
endPoint = new LatLonPoint(y,
x);
searchRouteResult(startPoint,endPoint);//路径方法调用
return false;
}
});
}
/**
* 开始搜索路径规划方案 这里是默认驾车模式
*/
public void searchRouteResult(LatLonPoint startPoint, LatLonPoint endPoint) {
final RouteSearch.FromAndTo fromAndTo = new RouteSearch.FromAndTo(
startPoint, endPoint);
//if (routeType == 1) {// 公交路径规划
// BusRouteQuery query = new BusRouteQuery(fromAndTo, busMode, "北京", 0);// 第一个参数表示路径规划的起点和终点,第二个参数表示公交查询模式,第三个参数表示公交查询城市区号,第四个参数表示是否计算夜班车,0表示不计算
// routeSearch.calculateBusRouteAsyn(query);// 异步路径规划公交模式查询
//} else if (routeType == 2) {
// 驾车路径规划,并且实现了对已经存在的路线进行清除和生成新的路线的规划
if(drivingRouteOverlay==null){
DriveRouteQuery query = new DriveRouteQuery(fromAndTo, drivingMode,
null, null, "");// 第一个参数表示路径规划的起点和终点,第二个参数表示驾车模式,第三个参数表示途经点,第四个参数表示避让区域,第五个参数表示避让道路
routeSearch.calculateDriveRouteAsyn(query);// 异步路径规划驾车模式查询
}else{drivingRouteOverlay.removeFromMap();
DriveRouteQuery query = new DriveRouteQuery(fromAndTo, drivingMode,
null, null, "");// 第一个参数表示路径规划的起点和终点,第二个参数表示驾车模式,第三个参数表示途经点,第四个参数表示避让区域,第五个参数表示避让道路
routeSearch.calculateDriveRouteAsyn(query);// 异步路径规划驾车模式查询
}
}
/**
* 方法必须重写
*/
@Override
protected void onResume() {
super.onResume();
mapView.onResume();
}
/**
* 方法必须重写
*/
@Override
protected void onPause() {
super.onPause();
mapView.onPause();
}
/**
* 方法必须重写
*/
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}
/**
* 方法必须重写
*/
@Override
protected void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}
/**
* 激活地位的重写方法
*/
@Override
public void activate(OnLocationChangedListener listener) {
// TODO Auto-generated method stub
mListener = listener;
if (mlocationClient == null) {
mlocationClient = new AMapLocationClient(this);
mLocationOption = new AMapLocationClientOption();
//设置定位监听
mlocationClient.setLocationListener(this);
//设置为高精度定位模式
mLocationOption.setLocationMode(AMapLocationMode.Hight_Accuracy);
//设置定位参数
mLocationOption.setInterval(2000);//时间间隔默认是2000ms可以自定义
mlocationClient.setLocationOption(mLocationOption);
// 此方法为每隔固定时间会发起一次定位请求,为了减少电量消耗或网络流量消耗,
// 注意设置合适的定位时间的间隔(最小间隔支持为2000ms),并且在合适时间调用stopLocation()方法来取消定位请求
// 在定位结束后,在合适的生命周期调用onDestroy()方法
// 在单次定位情况下,定位无论成功与否,都无需调用stopLocation()方法移除请求,定位sdk内部会移除
mlocationClient.startLocation();
}
}
/**
* 停止定位
*/
@Override
public void deactivate() {
// TODO Auto-generated method stub
mListener = null;
if (mlocationClient != null) {
mlocationClient.stopLocation();
mlocationClient.onDestroy();
}
mlocationClient = null;
}
@Override
public void onCheckedChanged(RadioGroup arg0, int arg1) {
// TODO Auto-generated method stub
//aMap.setMyLocationType(AMap.LOCATION_TYPE_LOCATE);
}
/**
* 定位成功后的回掉函数
*/
@Override
public void onLocationChanged(AMapLocation amapLocation) {
// 这是理想无错的情况下,应该对出错进行处置
mListener.onLocationChanged(amapLocation);// 显示系统小蓝点
double k=amapLocation.getLatitude();
double f=amapLocation.getLongitude();
startPoint = new LatLonPoint(k,
f);
//检测移动点的情况,定时进行 启动刷新标记点位置
}
/**
* 在地图上添加marker
*/
private void addMarkersToMap() {
//文字显示标注,可以设置显示内容,位置,字体大小颜色,背景色旋转角度
/*TextOptions textOptions = new TextOptions().position(new LatLng(39.90403, 116.407525))
.text("Text").fontColor(Color.BLACK)
.backgroundColor(Color.BLUE).fontSize(30).rotate(20).align(Text.ALIGN_CENTER_HORIZONTAL, Text.ALIGN_CENTER_VERTICAL)
.zIndex(1.f).typeface(Typeface.DEFAULT_BOLD)
;
aMap.addText(textOptions);
*/
MarkerOptions markerOption1 = new MarkerOptions()
.position(new LatLng(38.91141, 115.411306)).title("测试地1")
.snippet("保定:38.91141, 115.411306").icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory
.decodeResource(getResources(),
R.drawable.icon_car))
);
MarkerOptions markerOption2 = new MarkerOptions().anchor(0.5f, 0.5f)
.position(new LatLng(38.93141, 115.431306)).title("测试地")
.snippet("保定:38.93141, 115.431306").icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory
.decodeResource(getResources(),
R.drawable.power)));
MarkerOptions markerOption3 = new MarkerOptions().anchor(0.5f, 0.5f)
.position(new LatLng(38.93421, 115.451556)).title("测试地3")
.snippet("测试3:38.93421, 115.451556").icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory
.decodeResource(getResources(),
R.drawable.power)));
MarkerOptions markerOption4 = new MarkerOptions().anchor(0.5f, 0.5f)
.position(new LatLng(38.91441, 115.422106)).title("测试地4")
.snippet("测试地4:38.91441, 115.422106").icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory
.decodeResource(getResources(),
R.drawable.power)));
//保定
MarkerOptions markerOption5 = new MarkerOptions().anchor(0.5f, 0.5f)
.position(new LatLng(38.96121, 115.461316)).title("测试地5")
.snippet("测试地5:38.96121, 115.461316").icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory
.decodeResource(getResources(),
R.drawable.icon_car)));
MarkerOptions markerOption6 = new MarkerOptions().anchor(0.5f, 0.5f)
.position(new LatLng(38.87141, 115.671506)).title("测试地6")
.snippet("测试地6:38.87141, 115.671506").icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory
.decodeResource(getResources(),
R.drawable.power)));
MarkerOptions markerOption7 = new MarkerOptions().anchor(0.5f, 0.5f)
.position(new LatLng(38.89141, 115.381306)).title("测试地7")
.snippet("测试地7:38.89141, 115.381306").icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory
.decodeResource(getResources(),
R.drawable.power)));
MarkerOptions markerOption8 = new MarkerOptions().anchor(0.5f, 0.5f)
.position(new LatLng(38.88141, 115.491306)).title("测试地8")
.snippet("测试地8:38.89141, 115.381306").icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory
.decodeResource(getResources(),
R.drawable.power)));
//保定3
MarkerOptions markerOption9 = new MarkerOptions().anchor(0.5f, 0.5f)
.position(new LatLng(38.86121, 115.401316)).title("测试地9")
.snippet("测试地9:38.86121, 115.401316").icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory
.decodeResource(getResources(),
R.drawable.icon_car)));
MarkerOptions markerOption10 = new MarkerOptions().anchor(0.5f, 0.5f)
.position(new LatLng(38.77141, 115.571506)).title("测试地10")
.snippet("测试地10:38.77141, 115.571506").icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory
.decodeResource(getResources(),
R.drawable.power)));
MarkerOptions markerOption11 = new MarkerOptions().anchor(0.5f, 0.5f)
.position(new LatLng(38.79141, 115.481306)).title("测试地11")
.snippet("测试地11:38.79141, 115.481306").icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory
.decodeResource(getResources(),
R.drawable.power)));
MarkerOptions markerOption12 = new MarkerOptions().anchor(0.5f, 0.5f)
.position(new LatLng(38.388141, 115.291306)).title("测试地12")
.snippet("测试地12:38.88141, 115.391306").icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory
.decodeResource(getResources(),
R.drawable.power)));
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//可以计算这些点和定位位置的经纬度来距离来显示加载标记不一定要全部加载出来,可以
//在想后太查询的时候就进行限制
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
ArrayList<MarkerOptions> markerOptionlst = new ArrayList<MarkerOptions>();
markerOptionlst.add(markerOption1);
markerOptionlst.add(markerOption2);
markerOptionlst.add(markerOption3);
markerOptionlst.add(markerOption4);
markerOptionlst.add(markerOption5);
markerOptionlst.add(markerOption6);
markerOptionlst.add(markerOption7);
markerOptionlst.add(markerOption8);
markerOptionlst.add(markerOption9);
markerOptionlst.add(markerOption10);
markerOptionlst.add(markerOption11);
markerOptionlst.add(markerOption12);
List<Marker> markerlst = aMap.addMarkers(markerOptionlst, false);
}
/**
* 驾车路径的规划
*/
@Override
public void onBusRouteSearched(BusRouteResult arg0, int arg1) {
// 公交车回调
Toast.makeText(MapActivity.this, "只是公交路线回调方法", Toast.LENGTH_LONG);
}
@Override
public void onDriveRouteSearched(DriveRouteResult result, int rCode) {
// 驾车模式回调
if (rCode == 0) {
if (result != null && result.getPaths() != null
&& result.getPaths().size() > 0) {
driveRouteResult = result;
DrivePath drivePath = driveRouteResult.getPaths().get(0);
//aMap.clear();// 清理地图上的所有覆盖物
drivingRouteOverlay = new DrivingRouteOverlay(
this, aMap, drivePath, driveRouteResult.getStartPos(),
driveRouteResult.getTargetPos());//路线覆盖
drivingRouteOverlay.removeFromMap();
drivingRouteOverlay.addToMap();
drivingRouteOverlay.zoomToSpan();
} else {
Toast.makeText(MapActivity.this, "没有查出对应的路径", Toast.LENGTH_LONG);
}
} else if (rCode == 27) {
Toast.makeText(MapActivity.this, "网络错误", Toast.LENGTH_LONG);
} else if (rCode == 32) {
Toast.makeText(MapActivity.this, "地图key值错误", Toast.LENGTH_LONG);
} else {
Toast.makeText(MapActivity.this, "其他未知错误", Toast.LENGTH_LONG);
}
}
@Override
public void onWalkRouteSearched(WalkRouteResult arg0, int arg1) {
// 步行回调
Toast.makeText(MapActivity.this, "这是步行回调方法", Toast.LENGTH_LONG);
}
/**
* 子线程模拟标记点的移动的情况
*/
public class MyThread implements Runnable {
@Override
public void run() {
// TODO Auto-generated method stub
while (true) {
try {
Thread.sleep(10000);// 线程暂停10秒,单位毫秒
Message message = new Message();
message.what = 1;
handler.sendMessage(message);// 发送消息
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
最后
以上就是陶醉大雁为你收集整理的高德地图中实现实时定位、驾车路线规划,点间距的全部内容,希望文章能够帮你解决高德地图中实现实时定位、驾车路线规划,点间距所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复