package com.example.ditu; 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.ditu.overlay.DrivingRouteOverlay; public class MainActivity2 extends AppCompatActivity implements RouteSearch.OnRouteSearchListener { private MapView mMapView; private AMap aMap; private MyLocationStyle myLocationStyle; private String jing; private String wei; private double jingd; private double weid; 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"); jingd = Double.parseDouble(jing); weid = Double.parseDouble(wei); Toast.makeText(MainActivity2.this, jing + wei, Toast.LENGTH_SHORT).show(); //TODO LatLng latLng = new LatLng(jingd, weid); 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(MainActivity2.this); routeSearch.setRouteSearchListener(MainActivity2.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( MainActivity2.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(MainActivity2.this, R.string.no_result); } } else { ToastUtil.show(MainActivity2.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) { }}
package com.example.ditu; 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 edit_query; private EditText edit_query1; private Button but_but; private String query; private String query1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { edit_query = (EditText) findViewById(R.id.edit_query); edit_query1 = (EditText) findViewById(R.id.edit_query1); but_but = (Button) findViewById(R.id.but_but); but_but.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.but_but: submit(); Intent intent = new Intent(MainActivity.this, MainActivity2.class); intent.putExtra("jing", query); intent.putExtra("wei", query1); startActivity(intent); break; } } private void submit() { // validate query = edit_query.getText().toString().trim(); if (TextUtils.isEmpty(query)) { Toast.makeText(this, "query不能为空", Toast.LENGTH_SHORT).show(); return; } query1 = edit_query1.getText().toString().trim(); if (TextUtils.isEmpty(query1)) { Toast.makeText(this, "query1不能为空", Toast.LENGTH_SHORT).show(); return; } // TODO validate success, do something } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.ditu.MainActivity"> <EditText android:id="@+id/edit_query" android:layout_width="match_parent" android:layout_height="wrap_content" /> <EditText android:id="@+id/edit_query1" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/but_but" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="查找"/> </LinearLayout>
高德地图经纬查询
最新推荐文章于 2024-03-13 11:03:58 发布