public class MainActivity extends AppCompatActivity { public AMapLocationClient mLocationClient = null; @Bind(R.id.et_star) EditText etStar; @Bind(R.id.et_end) EditText etEnd; @Bind(R.id.ll) LinearLayout ll; @Bind(R.id.bt_btn_three) Button btBtnThree; @Bind(R.id.map) MapView map; @Bind(R.id.rb_one) RadioButton rbOne; @Bind(R.id.rb_two) RadioButton rbTwo; @Bind(R.id.rb_three) RadioButton rbThree; @Bind(R.id.rb_four) RadioButton rbFour; @Bind(R.id.rg_big) RadioGroup rgBig; private MapView mapView; private AMap aMap; private PoiSearch poiSearch; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); //定义了一个地图view mapView = (MapView) findViewById(R.id.map); mapView.onCreate(savedInstanceState);// 此方法须覆写,虚拟机需要在很多情况下保存地图绘制的当前状态。 //初始化地图控制器对象 if (aMap == null) { aMap = mapView.getMap(); } // 定义 Marker 点击事件监听 AMap.OnMarkerClickListener markerClickListener = new AMap.OnMarkerClickListener() { // marker 对象被点击时回调的接口 // 返回 true 则表示接口已响应事件,否则返回false @Override public boolean onMarkerClick(Marker marker) { RouteSearch routeSearch = new RouteSearch(MainActivity.this); routeSearch.setRouteSearchListener(new RouteSearch.OnRouteSearchListener() { @Override public void onBusRouteSearched(BusRouteResult result, int errorCode) { } @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) { DriveRouteResult mDriveRouteResult = result; final DrivePath drivePath = mDriveRouteResult.getPaths() .get(0); DrivingRouteOverlay drivingRouteOverlay = new DrivingRouteOverlay( MainActivity.this, aMap, drivePath, mDriveRouteResult.getStartPos(), mDriveRouteResult.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(MainActivity.this.getApplicationContext(), errorCode); } } @Override public void onWalkRouteSearched(WalkRouteResult result, int errorCode) { aMap.clear();// 清理地图上的所有覆盖物 if (errorCode == AMapException.CODE_AMAP_SUCCESS) { if (result != null && result.getPaths() != null) { if (result.getPaths().size() > 0) { WalkRouteResult mWalkRouteResult = result; final WalkPath walkPath = mWalkRouteResult.getPaths() .get(0); WalkRouteOverlay walkRouteOverlay = new WalkRouteOverlay( MainActivity.this, aMap, walkPath, mWalkRouteResult.getStartPos(), mWalkRouteResult.getTargetPos()); walkRouteOverlay.removeFromMap(); walkRouteOverlay.addToMap(); walkRouteOverlay.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(MainActivity.this.getApplicationContext(), errorCode); } } @Override public void onRideRouteSearched(RideRouteResult result, int errorCode) { aMap.clear();// 清理地图上的所有覆盖物 if (errorCode == AMapException.CODE_AMAP_SUCCESS) { if (result != null && result.getPaths() != null) { if (result.getPaths().size() > 0) { RideRouteResult mRideRouteResult = result; final RidePath ridePath = mRideRouteResult.getPaths() .get(0); RideRouteOverlay rideRouteOverlay = new RideRouteOverlay( MainActivity.this, aMap, ridePath, mRideRouteResult.getStartPos(), mRideRouteResult.getTargetPos()); rideRouteOverlay.removeFromMap(); rideRouteOverlay.addToMap(); rideRouteOverlay.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(MainActivity.this.getApplicationContext(), errorCode); } } }); 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); RouteSearch.FromAndTo 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; } }; // 绑定 Marker 被点击事件 aMap.setOnMarkerClickListener(markerClickListener); mLocationClient = new AMapLocationClient(getApplicationContext()); mLocationClient.setLocationListener(new AMapLocationListener() { @Override public void onLocationChanged(AMapLocation aMapLocation) { //获取定位结果 if (aMapLocation != null) { if (aMapLocation.getErrorCode() == 0) { //可在其中解析amapLocation获取相应内容。 int locationType = aMapLocation.getLocationType();//获取当前定位结果来源,如网络定位结果,详见定位类型表 double latitude = aMapLocation.getLatitude();//获取纬度 double longitude = aMapLocation.getLongitude();//获取经度 float accuracy = aMapLocation.getAccuracy();//获取精度信息 String address = aMapLocation.getAddress();//地址,如果option中设置isNeedAddress为false,则没有此结果,网络定位结果中会有地址信息,GPS定位不返回地址。 aMapLocation.getCountry();//国家信息 aMapLocation.getProvince();//省信息 aMapLocation.getCity();//城市信息 aMapLocation.getDistrict();//城区信息 aMapLocation.getStreet();//街道信息 aMapLocation.getStreetNum();//街道门牌号信息 aMapLocation.getCityCode();//城市编码 aMapLocation.getAdCode();//地区编码 aMapLocation.getAoiName();//获取当前定位点的AOI信息 aMapLocation.getBuildingId();//获取当前室内定位的建筑物Id aMapLocation.getFloor();//获取当前室内定位的楼层 //获取定位时间 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = new Date(aMapLocation.getTime()); df.format(date); Log.e("TAG", "纬度:" + latitude + " 经度:" + longitude + " 地址:" + address); } else { //定位失败时,可通过ErrCode(错误码)信息来确定失败的原因,errInfo是错误信息,详见错误码表。 Log.e("AmapError", "location Error, ErrCode:" + aMapLocation.getErrorCode() + ", errInfo:" + aMapLocation.getErrorInfo()); } } } }); MyLocationStyle myLocationStyle = new MyLocationStyle(); myLocationStyle.interval(2000); aMap.setMyLocationStyle(myLocationStyle); aMap.setMyLocationEnabled(true); myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE); myLocationStyle.showMyLocation(true); } @Override protected void onDestroy() { super.onDestroy(); //在activity执行onDestroy时执行mMapView.onDestroy(),销毁地图 mapView.onDestroy(); } @Override protected void onResume() { super.onResume(); //在activity执行onResume时执行mMapView.onResume (),重新绘制加载地图 mapView.onResume(); } @Override protected void onPause() { super.onPause(); //在activity执行onPause时执行mMapView.onPause (),暂停地图的绘制 mapView.onPause(); } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); //在activity执行onSaveInstanceState时执行mMapView.onSaveInstanceState (outState),保存地图当前的状态 mapView.onSaveInstanceState(outState); } @OnClick({R.id.bt_btn_three,}) public void onViewClicked(View view) { String end = etEnd.getText().toString(); String star = etStar.getText().toString(); PoiSearch.Query query = new PoiSearch.Query(star, "", end); switch (view.getId()) { case R.id.bt_btn_three: query.setPageSize(10);// 设置每页最多返回多少条poiitem query.setPageNum(1);//设置查询页码 poiSearch = new PoiSearch(MainActivity.this, query); poiSearch.setOnPoiSearchListener(new PoiSearch.OnPoiSearchListener() { @Override public void onPoiSearched(PoiResult poiResult, int i) { ArrayList<PoiItem> pois = poiResult.getPois(); for (int x = 0; x < pois.size(); x++) { LatLonPoint latLonPoint = pois.get(x).getLatLonPoint(); LatLng latLng = new LatLng(latLonPoint.getLatitude(), latLonPoint.getLongitude()); final Marker marker = aMap.addMarker(new MarkerOptions().position(latLng).title(pois.get(x).getTitle()).snippet(pois.get(x).getSnippet())); } } @Override public void onPoiItemSearched(PoiItem poiItem, int i) { } }); poiSearch.searchPOIAsyn(); break; } }}
布局
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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" tools:context="com.jiyun.asus.testmap.MainActivity"> <LinearLayout android:id="@+id/ll" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <EditText android:id="@+id/et_star" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" /> <EditText android:id="@+id/et_end" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" /> <Button android:text="搜索" android:id="@+id/bt_btn_three" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" /> </LinearLayout> <RadioGroup android:orientation="horizontal" android:layout_below="@id/ll" android:id="@+id/rg_big" android:layout_width="match_parent" android:layout_height="wrap_content"> <RadioButton android:text="自驾" android:id="@+id/rb_one" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" /> <RadioButton android:text="步行" android:id="@+id/rb_two" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" /> <RadioButton android:text="骑车" android:id="@+id/rb_three" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" /> <RadioButton android:text="公交" android:id="@+id/rb_four" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" /> </RadioGroup> <com.amap.api.maps.MapView android:layout_below="@+id/rg_big" android:id="@+id/map" android:layout_width="match_parent" android:layout_height="match_parent" tools:layout_editor_absoluteY="8dp" tools:layout_editor_absoluteX="8dp" /> </RelativeLayout>
嘻嘻嘻
最新推荐文章于 2022-11-18 15:53:58 发布