第一步
在a_xml中
<LinearLayout android:weightSum="10" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="50dp"> <EditText android:id="@+id/ed_JD" android:layout_width="0dp" android:layout_weight="2" android:layout_height="match_parent" /> <EditText android:id="@+id/ed_WD" android:layout_width="0dp" android:layout_weight="6" android:layout_height="match_parent" /> <Button android:text="搜索" android:id="@+id/btn_query" android:layout_width="0dp" android:layout_weight="2" android:layout_height="match_parent" /> </LinearLayout>
第二部
在Maintity中
@Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_query: submit(); String s = ed_JD.getText().toString(); String s1 = ed_WD.getText().toString(); Intent intent = new Intent(MainActivity.this, Main2Activity.class); intent.putExtra("jing",s); intent.putExtra("wei",s1); startActivity(intent); break;
第三部
在xml_2中
<com.amap.api.maps.MapView android:id="@+id/map" android:layout_width="match_parent" android:layout_height="match_parent"/>
第四部
在Main2tity中
package com.example.map; import android.location.Location; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.Toast; import com.amap.api.maps.AMap; import com.amap.api.maps.MapView; import com.amap.api.maps.model.LatLng; import com.amap.api.maps.model.Marker; import com.amap.api.maps.model.MarkerOptions; import com.amap.api.maps.model.MyLocationStyle; 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.DrivePath; 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.WalkRouteResult; import com.example.map.overlay.DrivingRouteOverlay; public class Main2Activity extends AppCompatActivity implements RouteSearch.OnRouteSearchListener { private MapView mMapView ; private AMap aMap; private MyLocationStyle myLocationStyle; private String jing; private String wei; private double jingdu; private double weidu; private RouteSearch.FromAndTo fromAndTo; private RouteSearch routeSearch; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); initView(); guihuanluxian(); //在activity执行onCreate时执行mMapView.onCreate(savedInstanceState),创建地图 mMapView.onCreate(savedInstanceState); //初始化地图控制器对象 if (aMap == null) { aMap = mMapView.getMap(); } } private void guihuanluxian() { jing = getIntent().getStringExtra("jing"); wei = getIntent().getStringExtra("wei"); jingdu = Double.parseDouble(jing); weidu = Double.parseDouble(wei); Toast.makeText(Main2Activity.this, jing + wei, Toast.LENGTH_SHORT).show(); //TODO LatLng latLng = new LatLng(jingdu, weidu); Marker marker = aMap.addMarker(new MarkerOptions().position(latLng).title("北京").snippet("DefaultMarker")); AMap.OnMarkerClickListener onMarkerClickListener = new AMap.OnMarkerClickListener() { @Override public boolean onMarkerClick(Marker marker) { routeSearch = new RouteSearch(Main2Activity.this); routeSearch.setRouteSearchListener(Main2Activity.this); Location location = aMap.getMyLocation(); double latitude = location.getLatitude(); double longitude = location.getLongitude(); LatLng position = marker.getPosition(); double latitude1 = position.latitude; double longitude1 = position.longitude; LatLonPoint point = new LatLonPoint(latitude, longitude); LatLonPoint point1 = new LatLonPoint(latitude1, longitude1); fromAndTo = new RouteSearch.FromAndTo(point, point1); RouteSearch.DriveRouteQuery query = new RouteSearch.DriveRouteQuery(fromAndTo, RouteSearch.DRIVING_MULTI_STRATEGY_FASTEST_SHORTEST_AVOID_CONGESTION, null, null, ""); routeSearch.calculateDriveRouteAsyn(query); return false; } }; aMap.setOnMarkerClickListener(onMarkerClickListener); } @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); } private void initView() { mMapView = (MapView) findViewById(R.id.map); //初始化地图控制器对象 if (aMap == null) { aMap = mMapView.getMap(); } myLocationStyle = new MyLocationStyle();//初始化定位蓝点样式类myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE);//连续定位、且将视角移动到地图中心点,定位点依照设备方向旋转,并且会跟随设备移动。(1秒1次定位)如果不设置myLocationType,默认也会执行此种模式。 myLocationStyle.interval(200000); //设置连续定位模式下的定位间隔,只在连续定位模式下生效,单次定位模式下不会生效。单位为毫秒。 myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE);//连续定位、且将视角移动到地图中心点,定位点依照设备方向旋转,并且会跟随设备移动。(1秒1次定位)默认执行此种模式。 myLocationStyle.showMyLocation(true); aMap.setMyLocationStyle(myLocationStyle);//设置定位蓝点的Style aMap.setMyLocationEnabled(true); } @Override public void onBusRouteSearched(BusRouteResult busRouteResult, int i) { } @Override public void onDriveRouteSearched(DriveRouteResult result, int errorCode) { aMap.clear();// 清理地图上的所有覆盖物 if (errorCode == AMapException.CODE_AMAP_SUCCESS) { if (result != null && result.getPaths() != null) { if (result.getPaths().size() > 0) { result = result; final DrivePath drivePath = result.getPaths() .get(0); DrivingRouteOverlay drivingRouteOverlay = new DrivingRouteOverlay( Main2Activity.this, aMap, 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(Main2Activity.this, R.string.no_result); } } else { ToastUtil.show(Main2Activity.this, R.string.no_result); } } else { ToastUtil.showerror(this.getApplicationContext(), errorCode); } } @Override public void onWalkRouteSearched(WalkRouteResult walkRouteResult, int i) { } @Override public void onRideRouteSearched(RideRouteResult rideRouteResult, int i) { } }