//地图SDK(包含其搜索功能)需要的基础权限
<!--允许程序打开网络套接字-->
<uses-permission android:name="android.permission.INTERNET" />
<!--允许程序设置内置sd卡的写权限-->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!--允许程序获取网络状态-->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!--允许程序访问WiFi网络信息-->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<!--允许程序读写手机状态和身份-->
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<!--允许程序访问CellID或WiFi热点来获取粗略的位置-->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<meta-data android:name="com.amap.api.v2.apikey" android:value="你的key">
</meta-data>
sourceSets{
main{
jniLibs.srcDirs=['libs']
}
}
public class MainActivity extends AppCompatActivity implements RouteSearch.OnRouteSearchListener {
private MapView mMapView;
private AMap mMap;
private RadioButton rb_fisrt;
private RadioButton rb_two;
private RadioButton rb_third;
private RadioButton rb_four;
private RadioGroup rg_rad;
private MapView map;
private RouteSearch search;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
mMapView.onCreate(savedInstanceState);
//初始化Map对象
mMap = mMapView.getMap();
mMap.setOnMarkerClickListener(new AMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
//自己的位置
Location myLocation = mMap.getMyLocation();
//终点的位置
LatLng position = marker.getPosition();
LatLonPoint start = new LatLonPoint(myLocation.getLatitude(), myLocation.getLongitude());
LatLonPoint end = new LatLonPoint(position.latitude, position.longitude);
RouteSearch.FromAndTo fat = new RouteSearch.FromAndTo(start, end);
search = new RouteSearch(MainActivity.this);
search.setRouteSearchListener(MainActivity.this);
int checkedRadioButtonId = rg_rad.getCheckedRadioButtonId();
switch (checkedRadioButtonId){
case R.id.rb_fisrt:
break;
case R.id.rb_two:
RouteSearch.DriveRouteQuery query = new RouteSearch.DriveRouteQuery(fat, RouteSearch.DRIVING_MULTI_CHOICE_AVOID_CONGESTION_NO_HIGHWAY_SAVE_MONEY, null, null, "");
search.calculateDriveRouteAsyn(query);
break;
case R.id.rb_third:
RouteSearch.WalkRouteQuery query1=new RouteSearch.WalkRouteQuery(fat);
search.calculateWalkRouteAsyn(query1);
break;
case R.id.rb_four:
RouteSearch.RideRouteQuery query2=new RouteSearch.RideRouteQuery(fat);
search.calculateRideRouteAsyn(query2);
break;
}
return true;
}
});
initBluePoint();
initMarker();
}
private void initMarker() {
LatLng lat = new LatLng(40.030762, 115.963085);
mMap.addMarker(new MarkerOptions().position(lat).snippet("这是灵溪风景区").title("123"));
}
private void initBluePoint() {
MyLocationStyle myLocationStyle;
myLocationStyle = new MyLocationStyle();//初始化定位蓝点样式类myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE);//连续定位、且将视角移动到地图中心点,定位点依照设备方向旋转,并且会跟随设备移动。(1秒1次定位)如果不设置myLocationType,默认也会执行此种模式。
myLocationStyle.interval(2000); //设置连续定位模式下的定位间隔,只在连续定位模式下生效,单次定位模式下不会生效。单位为毫秒。
mMap.setMyLocationStyle(myLocationStyle);//设置定位蓝点的Style
//aMap.getUiSettings().setMyLocationButtonEnabled(true);设置默认定位按钮是否显示,非必需设置。
mMap.setMyLocationEnabled(true);// 设置为true表示启动显示定位蓝点,false表示隐藏定位蓝点并不进行定位,默认是false。
myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_MAP_ROTATE);//连续定位、且将视角移动到地图中心点,地图依照设备方向旋转,定位点会跟随设备移动。(1秒1次定位)
myLocationStyle.showMyLocation(true);
}
private void initView() {
mMapView = (MapView) findViewById(R.id.map);
rb_fisrt = (RadioButton) findViewById(R.id.rb_fisrt);
rb_two = (RadioButton) findViewById(R.id.rb_two);
rb_third = (RadioButton) findViewById(R.id.rb_third);
rb_four = (RadioButton) findViewById(R.id.rb_four);
rg_rad = (RadioGroup) findViewById(R.id.rg_rad);
map = (MapView) findViewById(R.id.map);
}
@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);
}
@Override
public void onBusRouteSearched(BusRouteResult result, int errorCode) {
}
@Override
public void onDriveRouteSearched(DriveRouteResult result, int errorCode) {
mMap.clear();// 清理地图上的所有覆盖物
if (errorCode == AMapException.CODE_AMAP_SUCCESS) {
if (result != null && result.getPaths() != null) {
if (result.getPaths().size() > 0) {
final DrivePath drivePath = result.getPaths()
.get(0);
DrivingRouteOverlay drivingRouteOverlay = new DrivingRouteOverlay(
MainActivity.this, mMap, drivePath,
result.getStartPos(),
result.getTargetPos(), null);
drivingRouteOverlay.setNodeIconVisibility(false);//设置节点marker是否显示
drivingRouteOverlay.setIsColorfulline(true);//是否用颜色展示交通拥堵情况,默认true
drivingRouteOverlay.removeFromMap();
drivingRouteOverlay.addToMap();
drivingRouteOverlay.zoomToSpan();
} else if (result != null && result.getPaths() == null) {
ToastUtil.show(MainActivity.this, R.string.no_result);
}
} else {
ToastUtil.show(MainActivity.this, R.string.no_result);
}
} else {
ToastUtil.showerror(this.getApplicationContext(), errorCode);
}
}
@Override
public void onWalkRouteSearched(WalkRouteResult result, int errorCode) {
mMap.clear();// 清理地图上的所有覆盖物
if (errorCode == AMapException.CODE_AMAP_SUCCESS) {
if (result != null && result.getPaths() != null) {
if (result.getPaths().size() > 0) {
final WalkPath walkPath = result.getPaths()
.get(0);
WalkRouteOverlay walkRouteOverlay = new WalkRouteOverlay(
this, mMap, walkPath,
result.getStartPos(),
result.getTargetPos());
walkRouteOverlay.removeFromMap();
walkRouteOverlay.addToMap();
walkRouteOverlay.zoomToSpan();
} else if (result != null && result.getPaths() == null) {
ToastUtil.show(this, R.string.no_result);
}
} else {
ToastUtil.show(this, R.string.no_result);
}
} else {
ToastUtil.showerror(this.getApplicationContext(), errorCode);
}
}
@Override
public void onRideRouteSearched(RideRouteResult result, int errorCode) {
mMap.clear();// 清理地图上的所有覆盖物
if (errorCode == AMapException.CODE_AMAP_SUCCESS) {
if (result != null && result.getPaths() != null) {
if (result.getPaths().size() > 0) {
final RidePath ridePath = result.getPaths()
.get(0);
RideRouteOverlay rideRouteOverlay = new RideRouteOverlay(
this, mMap, ridePath,
result.getStartPos(),
result.getTargetPos());
rideRouteOverlay.removeFromMap();
rideRouteOverlay.addToMap();
rideRouteOverlay.zoomToSpan();
} else if (result != null && result.getPaths() == null) {
ToastUtil.show(this, R.string.no_result);
}
} else {
ToastUtil.show(this, R.string.no_result);
}
} else {
ToastUtil.showerror(this.getApplicationContext(), errorCode);
}
}
}
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>