利用高德实现类似单车功能

1.首先按照高德官方文档去配置一些需要的key 什么的
2.重点:
在需要的布局中展示地图控件
  <com.amap.api.maps2d.MapView
        android:layout_below="@+id/layout_title"
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
		
接下来在activity中获取控件:(6.0需要动态的申请权限)
 //获取地图控件引用
 mMapView = (MapView) findViewById(R.id.map);
 //在activity执行onCreate时执行mMapView.onCreate(savedInstanceState),创建地图
 mMapView.onCreate(savedInstanceState);
---
初始化地图:

		 /***
     * 初始化地图
     */
    private void init() {
        //初始化地图控制器对象
        if (aMap == null) {
            aMap = mMapView.getMap();
            uiSettings = aMap.getUiSettings();
        }

        //定位(当前位置)的绘制样式类。
        locationStyle = new MyLocationStyle();
        mlocationClient = new AMapLocationClient(this);
        markerOptions = new MarkerOptions();
        //初始化定位参数
        mLocationOption = new AMapLocationClientOption();
        //设置定位监听
        mlocationClient.setLocationListener(this);
        //设置定位模式为高精度模式,Battery_Saving为低功耗模式,Device_Sensors是仅设备模式
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        //设置定位间隔,单位毫秒,默认为2000ms
        mLocationOption.setInterval(500000);
        //设置缓存
        mLocationOption.setLocationCacheEnable(false);
        //设置定位参数
        mlocationClient.setLocationOption(mLocationOption);
        aMap.setMyLocationStyle(locationStyle);
        aMap.setMyLocationEnabled(false);// 设置为true表示启动显示定位蓝点,false表示隐藏定位蓝点并不进行定位,默认是false。
        //解决每次都先显示北京地图问题
//        aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(MyApplication.latitude, MyApplication.longitude), 19));

        // 此方法为每隔固定时间会发起一次定位请求,为了减少电量消耗或网络流量消耗,
        // 注意设置合适的定位时间的间隔(最小间隔支持为2000ms),并且在合适时间调用stopLocation()方法来取消定位请求
        // 在定位结束后,在合适的生命周期调用onDestroy()方法
        // 在单次定位情况下,定位无论成功与否,都无需调用stopLocation()方法移除请求,定位sdk内部会移除
        //启动定位
        mlocationClient.startLocation();
        //绘制Marker
//        drawMarker(list);
        setMapSettings();
        mRouteSearch = new RouteSearch(this);
        mRouteSearch.setRouteSearchListener(this);
        //当可视范围改变时回调的接口 滑动地图回调
        aMap.setOnCameraChangeListener(new AMap.OnCameraChangeListener() {
            @Override
            public void onCameraChange(CameraPosition cameraPosition) {
                Log.d("Dong", "----" +cameraPosition.toString());
            }

            @Override
            public void onCameraChangeFinish(CameraPosition cameraPosition) {
                Log.d("Dong", "2~~~~~" +cameraPosition.target) ;
                //移动地图结束后获取最新的位置的车辆
                getVehicleDetails();
            }
        });
    }
	
	 /****
     * 绘制marker
     * @param list
     */
    private void drawMarker(List<MainBean.DataBean.DateListBean> list) {
        //遍历集合中经纬度
        for (int i = 0; i < list.size(); i++) {
            MarkerOptions markerOption = new MarkerOptions();
            LatLng latLng0 = new LatLng(Double.parseDouble(list.get(i).getLon()), Double.parseDouble(list.get(i).getLat()));
            markerOption.position(latLng0);
            markerOption.icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(getResources(),R.mipmap.icon_che)));
            aMap.addMarker(markerOption);
        }

//        MarkerOptions markerOption = new MarkerOptions();
//        MarkerOptions markerOption1 = new MarkerOptions();
//        MarkerOptions markerOption2 = new MarkerOptions();
//        MarkerOptions markerOption3 = new MarkerOptions();
//        MarkerOptions markerOption4 = new MarkerOptions();
//        MarkerOptions markerOption5 = new MarkerOptions();
//
//        LatLng latLng0 = new LatLng(34.341568, 108.940174);
//        LatLng latLng1 = new LatLng(31.884285 ,117.272242);
//        LatLng latLng2 = new LatLng(31.725202,117.147485);
//        LatLng latLng3 = new LatLng(31.896057,117.479211);
//        LatLng latLng4 = new LatLng(31.935657,117.1288);
//        LatLng latLng5 = new LatLng(31.73245,117.612879);
//
//        markerOption.position(latLng0);
//        markerOption1.position(latLng1);
//        markerOption2.position(latLng2);
//        markerOption3.position(latLng3);
//        markerOption4.position(latLng4);
//        markerOption5.position(latLng5);
//
//
//        markerOption1.icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory
//                .decodeResource(getResources(),R.mipmap.icon_che)));
//        markerOption2.icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory
//                .decodeResource(getResources(),R.mipmap.icon_che)));
//        markerOption3.icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory
//                .decodeResource(getResources(),R.mipmap.icon_che)));
//        markerOption4.icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory
//                .decodeResource(getResources(),R.mipmap.icon_che)));
//        markerOption5.icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory
//                .decodeResource(getResources(),R.mipmap.icon_che)));
//        markerOption.title("1231231231232132131");
//        markerOption.snippet("klkljdgjkljkljklsjklgsdf");
//        markerOption1.title("444444");
//        markerOption1.snippet("klkljdgjkljkljklsjklgsdf");
//        markerOption2.title("55555");
//        markerOption2.snippet("klkljdgjkljkljklsjklgsdf");
//        markerOption3.title("111111");
//        markerOption3.snippet("klkljdgjkljkljklsjklgsdf");
//        markerOption4.title("22222222");
//        markerOption4.snippet("klkljdgjkljkljklsjklgsdf");
//        markerOption5.title("77777777777");
//        markerOption5.snippet("klkljdgjkljkljklsjklgsdf");
////        //设置Marker可拖动
////        markerOption.draggable(true);
//        aMap.addMarker(markerOption);
//        aMap.addMarker(markerOption1);
//        aMap.addMarker(markerOption2);
//        aMap.addMarker(markerOption3);
//        aMap.addMarker(markerOption4);
//        aMap.addMarker(markerOption5);
//        marker.setIcon(BitmapDescriptorFactory.fromBitmap(BitmapFactory
//                .decodeResource(getResources(),R.mipmap.lanlon)));
//        marker.setTitle("123213213213");
        AMap.OnMarkerClickListener markerClikc = new AMap.OnMarkerClickListener() {
            @Override
            public boolean onMarkerClick(Marker marker) {
                marker.setTitle(marker.getTitle());
                LatLng latLng = marker.getPosition();
                double toLat = latLng.latitude;
                double toLon = latLng.longitude;
                RouteSearch.FromAndTo fromAndTo = new RouteSearch.FromAndTo(new LatLonPoint(currentLat,currentLon), new LatLonPoint(toLat,toLon));
                //第一个参数表示路径规划的起点和终点,第二个参数表示驾车模式,第三个参数表示途经点,第四个参数表示避让区域,第五个参数表示避让道路
                RouteSearch.DriveRouteQuery query = new RouteSearch.DriveRouteQuery(fromAndTo, RouteSearch.DrivingDefault, null, null, "");
                mRouteSearch.calculateDriveRouteAsyn(query);// 异步路径规划驾车模式查询

                ///显示当前车辆具体信息
                frameLayout.setVisibility(View.VISIBLE);
                return true;
            }
        };
        aMap.setOnMarkerClickListener(markerClikc);
    }
	
	    /****
     * 设置地图上图标显示
     */
    private void setMapSettings() {
        // 设置定位监听
        aMap.setLocationSource(this);
        //设置是否显示默认定位按钮
//        uiSettings.setMyLocationButtonEnabled(true);
        //设置手势缩放
        uiSettings.setZoomGesturesEnabled(true);
        //隐藏缩放和放大按钮
        uiSettings.setZoomControlsEnabled(false);
        // 是否可触发定位并显示定位层
        aMap.setMyLocationEnabled(true);
    }
	
	 @Override
    protected void onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy(),销毁地图
        if (mMapView != null) {
            mMapView.onDestroy();
        }
        if (mlocationClient != null) {
            mlocationClient.stopLocation();//停止定位后,本地定位服务并不会被销毁}
            mlocationClient.onDestroy();//销毁定位客户端,同时销毁本地定位服务。重新绘制加载地图
        }
    }
	
	 @Override
    protected void onResume() {
        super.onResume();
        //在activity执行onResume时执行mMapView.onResume(),
        mMapView.onResume();
        mSensorHelper = new SensorEventHelper(this);
        if (mSensorHelper != null) {
            mSensorHelper.registerSensorListener();
            if (marker != null) {
                mSensorHelper.setCurrentMarker(marker);
            }
        }
        //获取车辆信息
        getVehicleDetails();
    }
	
	
	  @Override
    protected void onPause() {
        super.onPause();
        if (mSensorHelper != null) {
            mSensorHelper.unRegisterSensorListener();
            mSensorHelper.setCurrentMarker(null);
            mSensorHelper = null;
        }
        //在activity执行onPause时执行mMapView.onPause(),暂停地图的绘制
        mMapView.onPause();
        deactivate();
        mFirstFix = false;
    }
	  @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        //在activity执行onSaveInstanceState时执行mMapView.onSaveInstanceState(outState),保存地图当前的状态
        mMapView.onSaveInstanceState(outState);
    }
	
	@Override
    public void onLocationChanged(AMapLocation amapLocation) {
        Log.d("Dong", "1213213123123" +currentLat);
        if (amapLocation != null) {
            if (amapLocation.getErrorCode() == 0) {
                Log.d("Dong", "1213213123123" +currentLat);
                //定位成功回调信息,设置相关消息
                currentLat = amapLocation.getLatitude();
                currentLon = amapLocation.getLongitude();
                //存储经纬度
                LatLng location = new LatLng(currentLat, currentLon);
                if (!mFirstFix) {
                    mFirstFix = true;
                    addCircle(location, amapLocation.getAccuracy());//添加定位精度圆
                    addMarker(location);//添加定位图标
                    mSensorHelper.setCurrentMarker(marker);//定位图标旋转
                } else {
                    mCircle.setCenter(location);
                    mCircle.setRadius(amapLocation.getAccuracy());
                    marker.setPosition(location);
                }
                //设置地图展示的当前位置和缩放级别 缩放级别是在3-19之间
                aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(location, 18));
            } else {
                //显示错误信息ErrCode是错误码,errInfo是错误信息,详见错误码表。
                Log.e("Dong","location Error, ErrCode:"
                        + amapLocation.getErrorCode() + ", errInfo:"
                        + amapLocation.getErrorInfo());
            }
        }
    }
	
	/***
     * 添加定位经纬度圆边框
     * @param location
     * @param accuracy
     */
    private void addCircle(LatLng location, float accuracy) {
        CircleOptions options = new CircleOptions();
        options.strokeWidth(1f);
        options.fillColor(Color.argb(10, 0, 0, 180));//设置填充颜色。
        options.strokeColor(Color.rgb(43,43,43));//设置边框颜色,ARGB格式。
        options.center(location);//设置圆心经纬度坐标。
        options.radius(accuracy);//圆形半径,单位米。
        Log.d("Dong", "accuracy = " +accuracy);
        mCircle = aMap.addCircle(options);
    }
	/****
     * 添加当前位置定位图标
     * @param latlng
     */
    private void addMarker(LatLng latlng) {
        if (marker != null) {
            return;
        }
        Bitmap bMap = BitmapFactory.decodeResource(this.getResources(), R.mipmap.icon_dw);
        BitmapDescriptor des = BitmapDescriptorFactory.fromBitmap(bMap);
//		BitmapDescriptor des = BitmapDescriptorFactory.fromResource(R.drawable.navi_map_gps_locked);
        MarkerOptions options = new MarkerOptions();
        options.draggable(true);
        options.icon(des);
        options.anchor(0.5f, 0.5f);
        options.position(latlng);
        marker = aMap.addMarker(options);
//        marker.setTitle(LOCATION_MARKER_FLAG);

    }
	
	
	//此处可以规划出不同的路线图
	 @Override
    public void onDriveRouteSearched(DriveRouteResult result, int errorCode) {
        if (errorCode == 1000) {
            if (result != null && result.getPaths() != null) {
                if(polyline != null){
                    //画线之前先移除上一条路线
                    polyline.remove();
                    polyline = null;
                }
                DrivePath path = result.getPaths().get(0);
                List<DriveStep> step = path.getSteps();
                List<LatLng> latLngList = new ArrayList<>();
                for (int k = 0; k < step.size();k++) {
                    List<LatLonPoint> points = step.get(k).getPolyline();
                    latLngList.addAll(StringUtils.convertArrList(points));//遍历添加所有顶点
                }
                polyline = aMap.addPolyline(new PolylineOptions().color(Color.parseColor("#3bc162")).addAll(latLngList));//添加线
            }
        }
    }
	
	  /****
     *  激活定位
     * @param onLocationChangedListener
     */
    @Override
    public void activate(OnLocationChangedListener onLocationChangedListener) {
        if (mlocationClient == null) {
            mlocationClient = new AMapLocationClient(this);
            mLocationOption = new AMapLocationClientOption();
            //设置定位监听
            mlocationClient.setLocationListener(this);
            //设置为高精度定位模式
            mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
            //设置定位参数
            mlocationClient.setLocationOption(mLocationOption);
            // 此方法为每隔固定时间会发起一次定位请求,为了减少电量消耗或网络流量消耗,
            // 注意设置合适的定位时间的间隔(最小间隔支持为2000ms),并且在合适时间调用stopLocation()方法来取消定位请求
            // 在定位结束后,在合适的生命周期调用onDestroy()方法
            // 在单次定位情况下,定位无论成功与否,都无需调用stopLocation()方法移除请求,定位sdk内部会移除
            mlocationClient.startLocation();
        }
    }
	
	 /****
     * 停止定位
     */
    @Override
    public void deactivate() {
        if (mlocationClient != null) {
            mlocationClient.stopLocation();
            mlocationClient.onDestroy();
        }
        mlocationClient = null;
    }
Demo地址:http://download.youkuaiyun.com/download/dong5488/10046842
	
	
	




评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值