這段時間開發的時候用到了高德地圖,對高德地圖開發有心得體會,現在分享給你們。對我開發過百度地圖的我來講,總體來講高德地圖Demo,沒有百度解說的具體html
我的更偏向於使用百度地圖,但是沒辦發,項目需要使用高德地圖,我開發的是定位,更具經緯度加入標記。標記點擊事件,以及路線規劃廢話很少說,上代代碼java
那麼首先導入高德給的jar,包,我開發的是2d地圖,android
這個包結構圖,高德題圖api也提供了步驟,就很少說了api
如下加入權限,設置key網絡
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.CHANGE_CONFIGURATION" /> <uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.WRITE_SETTINGS" />
package com.yakj.gaodedemo; import android.app.Activity; import android.graphics.Color; import android.location.Location; import android.os.Bundle; import android.view.View; import android.widget.RelativeLayout; import android.widget.Toast; import com.amap.api.location.AMapLocation; import com.amap.api.location.AMapLocationListener; import com.amap.api.location.LocationManagerProxy; import com.amap.api.location.LocationProviderProxy; import com.amap.api.maps2d.AMap; import com.amap.api.maps2d.AMap.OnMapClickListener; import com.amap.api.maps2d.AMap.OnMarkerClickListener; import com.amap.api.maps2d.CameraUpdateFactory; import com.amap.api.maps2d.LocationSource; import com.amap.api.maps2d.MapView; import com.amap.api.maps2d.model.BitmapDescriptor; import com.amap.api.maps2d.model.BitmapDescriptorFactory; import com.amap.api.maps2d.model.LatLng; import com.amap.api.maps2d.model.Marker; import com.amap.api.maps2d.model.MarkerOptions; import com.amap.api.maps2d.model.MyLocationStyle; import com.yakj.view.StationInfoPopupWindow; public class MainActivity extends Activity implements LocationSource, AMapLocationListener, OnMarkerClickListener, OnMapClickListener { /** * 基礎地圖 */ private MapView mapView; private AMap aMap; /** * 定位 */ private LocationManagerProxy mAMapLocationManager; /** * 定位監聽 */ private OnLocationChangedListener mListener; /** * 加入的覆蓋物標誌 */ private Marker currentMarker; /** * 點擊標記物彈出popWindow信息 */ private StationInfoPopupWindow popWindow; /** * 展現popWindow佈局 */ private RelativeLayout mpop; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mapView = (MapView) findViewById(R.id.map); mapView.onCreate(savedInstanceState);// 必須要寫 mpop = (RelativeLayout) findViewById(R.id.rent_map_pop); init(); } /** * 初始化AMap對象 */ private void init() { if (aMap == null) { aMap = mapView.getMap(); setUpMap(); } } /** * 設置地圖樣式 */ private void setUpMap() { // 本身定義系統定位藍點 MyLocationStyle myLocationStyle = new MyLocationStyle(); // 本身定義定位藍點圖標 myLocationStyle.myLocationIcon(BitmapDescriptorFactory.fromResource(R.drawable.location_marker)); // 本身定義精度範圍的圓形邊框顏色 myLocationStyle.strokeColor(Color.BLUE); myLocationStyle.radiusFillColor(Color.TRANSPARENT); // 本身定義精度範圍的圓形邊框寬度 myLocationStyle.strokeWidth(2); // 將本身定義的 myLocationStyle 對象加入到地圖上 aMap.setMyLocationStyle(myLocationStyle); aMap.setLocationSource(this);// 設置定位監聽 aMap.getUiSettings().setMyLocationButtonEnabled(true);// 設置默認定位button是否顯示 aMap.setMyLocationEnabled(true);// 設置爲true表示顯示定位層並可觸發定位,false表示隱藏定位層並不可觸發定位,默認是false // 設置定位的類型爲定位模式:定位(AMap.LOCATION_TYPE_LOCATE)、尾隨(AMap.LOCATION_TYPE_MAP_FOLLOW) // 地圖依據面向方向旋轉(AMap.LOCATION_TYPE_MAP_ROTATE)三種模式 // aMap.setMyLocationType(AMap.MAP_TYPE_SATELLITE); // 設置地圖可視縮放大小 aMap.moveCamera(CameraUpdateFactory.zoomTo(14)); aMap.getUiSettings().setCompassEnabled(true);// 設置指南針 aMap.getUiSettings().setScaleControlsEnabled(true);// 設置比例尺 LatLng latLng = new LatLng(31.383755, 118.438321); MarkerOptions otMarkerOptions = new MarkerOptions(); otMarkerOptions.position(latLng); otMarkerOptions.visible(true);//設置可見 otMarkerOptions.title("蕪湖市").snippet("蕪湖市:31.383755, 118.438321");//裏面的內容本身定義 otMarkerOptions.draggable(true); //如下這個是標記上面這個經緯度在地圖的位置是 // otMarkerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_mark)); //如下這個是本身定義的標記圖標用法 otMarkerOptions.icon(ImageNormal(0)); aMap.addMarker(otMarkerOptions); aMap.setOnMarkerClickListener(this); aMap.setOnMapClickListener(this); } /** * 本身定義標記物的圖片(未選中狀態) * @param i * @return */ private BitmapDescriptor ImageNormal(int i) { //這個佈局是本身定義的。這面的內容相同本身主動,在poi_view 這個xml文件中有一個有一張圖片。有一個TextView //被我刪除了,這個TextView,有須要的網友可以本身設置。這個TextView裏面可以寫數字。或者ABCD...更具需求 //各位自由發揮 View view = null; view = getLayoutInflater().inflate(R.layout.poi_view, null); RelativeLayout ly = (RelativeLayout) view.findViewById(R.id.view_mark); // TextView tv = (TextView) view.findViewById(R.id.poi_mark_img); // tv.setText(i + ""); // tv.setPadding(0, 0, 0, 25); // tv.setBackgroundResource(R.drawable.poi_mark_normal); BitmapDescriptor bitmap = BitmapDescriptorFactory.fromView(view); return bitmap; } /** * 本身定義標記物圖片(選中狀態) * @param i * @return */ private BitmapDescriptor ImagePress(int i) { //用法同上 View view = null; view = getLayoutInflater().inflate(R.layout.poi_view, null); // TextView tv = (TextView) view.findViewById(R.id.poi_mark_img); // tv.setText(i + ""); // tv.setPadding(0, 0, 0, 25); // tv.setBackgroundResource(R.drawable.poi_mark_press); BitmapDescriptor bitmap = BitmapDescriptorFactory.fromView(view); return bitmap; } /** * 方法必須重寫 */ @Override protected void onResume() { super.onResume(); mapView.onResume(); } /** * 方法必須重寫 */ @Override protected void onPause() { super.onPause(); mapView.onPause(); deactivate(); } /** * 方法必須重寫 */ @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); mapView.onSaveInstanceState(outState); } /** * 方法必須重寫 */ @Override protected void onDestroy() { super.onDestroy(); mapView.onDestroy(); if (popWindow != null) {//隱藏popwindow popWindow.dismiss(); } } /** * 激活定位 */ @Override public void activate(OnLocationChangedListener listener) { mListener = listener; if (mAMapLocationManager == null) { mAMapLocationManager = LocationManagerProxy.getInstance(this); // 此方法爲每隔固定時間會發起一次定位請求。爲了下降電量消耗或網絡流量消耗, // 注意設置合適的定位時間的間隔,並且在合適時間調用removeUpdates()方法來取消定位請求 // 在定位結束後。在合適的生命週期調用destroy()方法 // 當中假設間隔時間爲-1。則定位僅僅定一次 mAMapLocationManager.requestLocationData(LocationProviderProxy.AMapNetwork, 60 * 1000, 10, this); } } /** * 中止定位 */ @Override public void deactivate() { mListener = null; if (mAMapLocationManager != null) { mAMapLocationManager.removeUpdates(this); mAMapLocationManager.destroy(); } mAMapLocationManager = null; } @Override public void onLocationChanged(Location location) { } @Override public void onStatusChanged(String provider, int status, Bundle extras) { } @Override public void onProviderEnabled(String provider) { } @Override public void onProviderDisabled(String provider) { } /** * 定位成功後回調函數 */ @Override public void onLocationChanged(AMapLocation amapLocation) { if (mListener != null && amapLocation != null) { if (amapLocation.getAMapException().getErrorCode() == 0) { mListener.onLocationChanged(amapLocation);// 顯示系統小藍點 } } } @Override public boolean onMarkerClick(Marker marker) { currentMarker = marker; Toast.makeText(this, "你點擊了的是" + marker.getTitle(), 10000).show(); if(popWindow !=null){//先把原來的給隱藏起來 popWindow.dismiss(); } popWindow = new StationInfoPopupWindow(this); popWindow.showAsDropDown(mpop); return false; } /** * 點擊地圖其它地方時,隱藏InfoWindow,和popWindow彈出框 */ @Override public void onMapClick(LatLng latLng) { if (currentMarker != null) { currentMarker.hideInfoWindow();//隱藏InfoWindow框 popWindow.dismiss(); } } }
圖上看到的那個就是我本身定義的圖片,這個有些時候都是需要自定的,高德給的原生的不該符合咱們的需求app
當點擊那個圖上那個標記時就出現例如如下界面。異步
而後就是路勁規劃了,點擊到這裏去button就跳轉到路徑規劃的界面ide
如下吧popWidow代碼貼出來函數
package com.yakj.view; import android.content.Context; import android.content.Intent; import android.graphics.drawable.BitmapDrawable; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup.LayoutParams; import android.widget.Button; import android.widget.PopupWindow; import android.widget.PopupWindow.OnDismissListener; import com.yakj.gaodedemo.R; import com.yakj.gaodedemo.RouteActivity; public class StationInfoPopupWindow implements View.OnClickListener { private Context context; private PopupWindow popupWindow; //到這裏去button private Button goBtn; public StationInfoPopupWindow(final Context context) { this.context = context; View view = LayoutInflater.from(context).inflate(R.layout.view_map_popup_window, null); popupWindow = new PopupWindow(view, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); // 這個是爲了點擊「返回Back」也能使其消失,並且並不會影響你的背景(很是奇妙的) popupWindow.setBackgroundDrawable(new BitmapDrawable(context.getResources())); goBtn = (Button) view.findViewById(R.id.go_to_hotel_btn); goBtn.setOnClickListener(this); } // 下拉式 彈出 pop菜單 parent 右下角 public void showAsDropDown(View parent) { // 保證尺寸是依據屏幕像素密度來的 popupWindow.showAtLocation(parent, Gravity.BOTTOM, 0, 0); // 使其彙集 popupWindow.setFocusable(false); // 設置贊成在外點擊消失 popupWindow.setOutsideTouchable(false); // 設置動畫 popupWindow.setAnimationStyle(R.style.PopupWindowAnimStyle); // 刷新狀態 popupWindow.update(); } public void setDismissListener(OnDismissListener onDismissListener) { popupWindow.setOnDismissListener(onDismissListener); } // 隱藏菜單 public void dismiss() { popupWindow.dismiss(); } // 是否顯示 public boolean isShowing() { return popupWindow.isShowing(); } @Override public void onClick(View v) { if (v == goBtn) {
//這裏跳轉到路徑規劃界面 Intent intent = new Intent(context, RouteActivity.class); context.startActivity(intent); } } }
如下來講下路徑規劃佈局
package com.yakj.gaodedemo; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.ImageButton; import android.widget.Toast; import com.amap.api.maps2d.AMap; import com.amap.api.maps2d.CameraUpdateFactory; import com.amap.api.maps2d.MapView; import com.amap.api.maps2d.overlay.BusRouteOverlay; import com.amap.api.maps2d.overlay.DrivingRouteOverlay; import com.amap.api.maps2d.overlay.WalkRouteOverlay; import com.amap.api.services.core.LatLonPoint; import com.amap.api.services.route.BusPath; 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.BusRouteQuery; import com.amap.api.services.route.RouteSearch.DriveRouteQuery; import com.amap.api.services.route.RouteSearch.OnRouteSearchListener; import com.amap.api.services.route.RouteSearch.WalkRouteQuery; import com.amap.api.services.route.WalkPath; import com.amap.api.services.route.WalkRouteResult; /** * 路徑規劃 * * @author Administrator * */ public class RouteActivity extends Activity implements OnClickListener, OnRouteSearchListener { private AMap aMap; private MapView mapView; /** * 公交button,駕車button。步行button */ private ImageButton transitBtn, drivingBtn, walkBtn; private int busMode = RouteSearch.BusDefault;// 公交默認模式 private int drivingMode = RouteSearch.DrivingDefault;// 駕車默認模式 private int walkMode = RouteSearch.WalkDefault;// 步行默認模式 private RouteSearch routeSearch; private BusRouteResult busRouteResult;// 公交模式查詢結果 private DriveRouteResult driveRouteResult;// 駕車模式查詢結果 private WalkRouteResult walkRouteResult;// 步行模式查詢結果 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_route); mapView = (MapView) findViewById(R.id.route_map); mapView.onCreate(savedInstanceState);// 此方法必須重寫 transitBtn = (ImageButton) findViewById(R.id.imagebtn_roadsearch_tab_transit); drivingBtn = (ImageButton) findViewById(R.id.imagebtn_roadsearch_tab_driving); walkBtn = (ImageButton) findViewById(R.id.imagebtn_roadsearch_tab_walk); routeSearch = new RouteSearch(this); routeSearch.setRouteSearchListener(this); init(); } /** * 初始化AMap對象 */ private void init() { if (aMap == null) { aMap = mapView.getMap(); } transitBtn.setOnClickListener(this); drivingBtn.setOnClickListener(this); walkBtn.setOnClickListener(this); // 設置地圖可視縮放大小 aMap.moveCamera(CameraUpdateFactory.zoomTo(12)); } /** * 方法必須重寫 */ @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 onClick(View v) { // 這裏是寫死的兩個位置 LatLonPoint startPoint = new LatLonPoint(31.383755, 118.438321); LatLonPoint endPoint = new LatLonPoint(31.339746, 118.381727); final RouteSearch.FromAndTo fromAndTo = new RouteSearch.FromAndTo(startPoint, endPoint); if (v == transitBtn) {// 公交 BusRouteQuery query = new BusRouteQuery(fromAndTo, busMode, "蕪湖市", 1);// 第一個參數表示路徑規劃的起點和終點,第二個參數表示公交查詢模式,第三個參數表示公交查詢城市區號,第四個參數表示是否計算夜班車。0表示不計算 routeSearch.calculateBusRouteAsyn(query);// 異步路徑規劃公交模式查詢 } else if (v == drivingBtn) {// 駕車 DriveRouteQuery query = new DriveRouteQuery(fromAndTo, drivingMode, null, null, "");// 第一個參數表示路徑規劃的起點和終點,第二個參數表示駕車模式,第三個參數表示途經點,第四個參數表示避讓區域,第五個參數表示避讓道路 routeSearch.calculateDriveRouteAsyn(query);// 異步路徑規劃駕車模式查詢 } else if (v == walkBtn) {// 步行 WalkRouteQuery query = new WalkRouteQuery(fromAndTo, walkMode); routeSearch.calculateWalkRouteAsyn(query);// 異步路徑規劃步行模式查詢 } } @Override public void onBusRouteSearched(BusRouteResult result, int rCode) { if (rCode == 0) { if (result != null && result.getPaths() != null && result.getPaths().size() > 0) { busRouteResult = result; BusPath busPath = busRouteResult.getPaths().get(0); aMap.clear();// 清理地圖上的所有覆蓋物 BusRouteOverlay routeOverlay = new BusRouteOverlay(this, aMap, busPath, busRouteResult.getStartPos(), busRouteResult.getTargetPos()); routeOverlay.removeFromMap(); routeOverlay.addToMap(); routeOverlay.zoomToSpan(); } else { showToast("對不起,沒有搜索到相關數據!"); } } else if (rCode == 27) { showToast("搜索失敗,請檢查網絡鏈接!"); } else if (rCode == 32) { showToast("key驗證無效!"); } else { showToast("未知錯誤,請稍後重試!錯誤碼爲" + rCode); } } @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 drivingRouteOverlay = new DrivingRouteOverlay(this, aMap, drivePath, driveRouteResult.getStartPos(), driveRouteResult.getTargetPos()); drivingRouteOverlay.removeFromMap(); drivingRouteOverlay.addToMap(); drivingRouteOverlay.zoomToSpan(); } else { showToast("對不起,沒有搜索到相關數據。"); } } else if (rCode == 27) { showToast("搜索失敗,請檢查網絡鏈接!"); } else if (rCode == 32) { showToast("key驗證無效!"); } else { showToast("未知錯誤,請稍後重試!錯誤碼爲" + rCode); } } @Override public void onWalkRouteSearched(WalkRouteResult result, int rCode) { if (rCode == 0) { if (result != null && result.getPaths() != null && result.getPaths().size() > 0) { walkRouteResult = result; WalkPath walkPath = walkRouteResult.getPaths().get(0); aMap.clear();// 清理地圖上的所有覆蓋物 WalkRouteOverlay walkRouteOverlay = new WalkRouteOverlay(this, aMap, walkPath, walkRouteResult.getStartPos(), walkRouteResult.getTargetPos()); walkRouteOverlay.removeFromMap(); walkRouteOverlay.addToMap(); walkRouteOverlay.zoomToSpan(); } else { showToast("對不起。沒有搜索到相關數據!"); } } else if (rCode == 27) { showToast("搜索失敗,請檢查網絡鏈接。"); } else if (rCode == 32) { showToast("key驗證無效!"); } else { showToast("未知錯誤,請稍後重試!錯誤碼爲" + rCode); } } /** * toast封裝 * * @param str */ private void showToast(String str) { Toast.makeText(this, str, Toast.LENGTH_SHORT).show(); ; } }
我這裏路勁規劃的界面沒有定位,點擊進去會是北京。而後你隨便點擊上面的一個路線規劃button,就會有顯示。如上圖所看到的
如下個人把Demo路徑。點擊打開連接錯這裏啦
下載地址 :http://download.csdn.net/detail/shaozucheng/8799515