1首先layout文件加入一个mapview
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_run_map" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.sportrecord.RunMap"> <com.amap.api.maps.MapView android:id="@+id/map" android:layout_width="match_parent" android:layout_height="match_parent"/> </RelativeLayout>
对应的java文件如下
package com.example.sportrecord; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import com.amap.api.location.AMapLocationClient; import com.amap.api.maps.AMap; import com.amap.api.maps.MapView; import com.amap.api.services.core.AMapException; import com.amap.api.services.core.LatLonPoint; import com.amap.api.services.route.BusRouteResult; import com.amap.api.services.route.DriveRouteResult; import com.amap.api.services.route.RideRouteResult; import com.amap.api.services.route.RouteSearch; import com.amap.api.services.route.WalkPath; import com.amap.api.services.route.WalkRouteResult; import com.example.sportrecord.overlay.WalkRouteOverlay; public class RunMap extends AppCompatActivity implements RouteSearch.OnRouteSearchListener{ private WalkRouteResult walkRouteResult; private MapView mMapView; RouteSearch routeSearch; AMap aMap; private WalkRouteOverlay walkRouteOverlay; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_run_map); //获取地图控件引用 AMapLocationClient.setApiKey("key值"); mMapView = (MapView) findViewById(R.id.map); //在activity执行onCreate时执行mMapView.onCreate(savedInstanceState),创建地图 mMapView.onCreate(savedInstanceState); if (aMap == null) { aMap = mMapView.getMap(); } // MyLocationStyle myLocationStyle; // myLocationStyle = new MyLocationStyle();//初始化定位蓝点样式类myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE);//连续定位、且将视角移动到地图中心点,定位点依照设备方向旋转,并且会跟随设备移动。(1秒1次定位)如果不设置myLocationType,默认也会执行此种模式。 // myLocationStyle.interval(2000); //设置连续定位模式下的定位间隔,只在连续定位模式下生效,单次定位模式下不会生效。单位为毫秒。 // myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_MAP_ROTATE);//连续定位、且将视角移动到地图中心点,地图依照设备方向旋转,定位点会跟随设备移动。(1秒1次定位) // myLocationStyle.showMyLocation(true);//设置是否显示定位小蓝点,用于满足只想使用定位,不想使用定位小蓝点的场景,设置false以后图面上不再有定位蓝点的概念,但是会持续回调位置信息。 // aMap.setMyLocationStyle(myLocationStyle);//设置定位蓝点的Style ////aMap.getUiSettings().setMyLocationButtonEnabled(true);设置默认定位按钮是否显示,非必需设置。 // aMap.setMyLocationEnabled(true);// 设置为true表示启动显示定位蓝点,false表示隐藏定位蓝点并不进行定位,默认是false。 // // LatLng latLng = new LatLng(34.341568,108.940174); // final Marker marker = aMap.addMarker(new MarkerOptions().position(latLng).title("北京").snippet("DefaultMarker")); // MarkerOptions markerOption = new MarkerOptions(); // //markerOption.position(Constants.XIAN); // markerOption.title("西安市").snippet("西安市:34.341568, 108.940174"); // // markerOption.draggable(true);//设置Marker可拖动 // markerOption.icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory // .decodeResource(getResources(),R.drawable.amap_car))); // // 将Marker设置为贴地显示,可以双指下拉地图查看效果 // markerOption.setFlat(true);//设置marker平贴地图效果 routeSearch = new RouteSearch(this); routeSearch.setRouteSearchListener(this); LatLonPoint mStartPoint = new LatLonPoint(39.942295, 116.335891); LatLonPoint mEndPoint = new LatLonPoint(39.995576, 116.481288); final RouteSearch.FromAndTo fromAndTo = new RouteSearch.FromAndTo(mStartPoint, mEndPoint); RouteSearch.WalkRouteQuery query = new RouteSearch.WalkRouteQuery(fromAndTo, RouteSearch.WALK_DEFAULT); routeSearch.calculateWalkRouteAsyn(query);//开始算路 } @Override public void onBusRouteSearched(BusRouteResult busRouteResult, int i) { } @Override public void onDriveRouteSearched(DriveRouteResult driveRouteResult, int i) { } @Override public void onRideRouteSearched(RideRouteResult rideRouteResult, int i) { } @Override public void onWalkRouteSearched(WalkRouteResult result, int rCode) { //dissmissProgressDialog(); if (rCode == 1000) { 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(); } } } @Override protected void onDestroy() { super.onDestroy(); //在activity执行onDestroy时执行mMapView.onDestroy(),销毁地图 mMapView.onDestroy(); } @Override protected void onResume() { super.onResume(); //在activity执行onResume时执行mMapView.onResume (),重新绘制加载地图 mMapView.onResume(); } @Override protected void onPause() { super.onPause(); //在activity执行onPause时执行mMapView.onPause (),暂停地图的绘制 mMapView.onPause(); } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); //在activity执行onSaveInstanceState时执行mMapView.onSaveInstanceState (outState),保存地图当前的状态 mMapView.onSaveInstanceState(outState); } }
在以上代码中使用了WalkRouteOverlay,这个类在高德3d地图的包里面是没有的,以为高德把他开源了,首先看一下文件的路径
附上这些代码的链接,下载后需要做一点修改(把包名改成自己的就可以了,很简单)
链接: https://pan.baidu.com/s/1McPDfWisfyhEpO3oNFxUiA 提取码: 54wk
运行结果如下