//xml中的代碼android
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.dituss.MainActivity"> <EditText android:id="@+id/et_shu" android:layout_width="match_parent" android:layout_height="wrap_content" /> <EditText android:id="@+id/et_shu2" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/bt" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="提交" /> </LinearLayout>
//第一個頁面Java代碼git
package com.example.dituss; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.text.TextUtils; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private EditText et_shu; private EditText et_shu2; private Button bt; private String shu; private String shu2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { et_shu = (EditText) findViewById(R.id.et_shu); et_shu2 = (EditText) findViewById(R.id.et_shu2); bt = (Button) findViewById(R.id.bt); bt.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.bt: submit(); Intent intent = new Intent(MainActivity.this,Main2Activity.class); intent.putExtra("jing",shu); intent.putExtra("wei",shu2); startActivity(intent); break; } } private void submit() { // validate shu = et_shu.getText().toString().trim(); if (TextUtils.isEmpty(shu)) { Toast.makeText(this, "shu不能爲空", Toast.LENGTH_SHORT).show(); return; } shu2 = et_shu2.getText().toString().trim(); if (TextUtils.isEmpty(shu2)) { Toast.makeText(this, "shu2不能爲空", Toast.LENGTH_SHORT).show(); return; } // TODO validate success, do something } }
//第二個頁面xml代碼api
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <com.amap.api.maps.MapView android:id="@+id/map" android:layout_width="match_parent" android:layout_height="match_parent"/> </LinearLayout>
//第二個頁面Java代碼app
package com.example.dituss; import android.location.Location; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; 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.dituss.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, ""); } } else { ToastUtil.show(Main2Activity.this,""); } } else { ToastUtil.showerror(this.getApplicationContext(), errorCode); } } @Override public void onWalkRouteSearched(WalkRouteResult walkRouteResult, int i) { } @Override public void onRideRouteSearched(RideRouteResult rideRouteResult, int i) { } }