高德地圖中實現實時定位、駕車路線規劃,點間距
/**
* 實現地圖的載入
* @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();
}
}
}
}
}