自2018年10月18日起,高德开发者论坛除车机板块外,其他板块将停止发帖与维护,如您有使用问题请 提交工单 联系我们,感谢您的理解。

查看: 368|回复: 1
打印 上一主题 下一主题

[应用案例] 1.我在地图上A点标记marker,然后通过定时移动坐标到B点。...

[复制链接]
最佳答案
0 

8

主题

16

帖子

78

积分

新手上路

Rank: 1

积分
78
跳转到指定楼层
楼主
发表于 2016-5-5 17:29:26 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
发表帖子
API/SDK版本号: 23
产品: 3D地图
手机型号:
使用接口:
1.我在地图上A点标记marker,然后通过定时移动坐标到B点。然后我把A点marker删除,重新设置B点marker,A点会出现一闪而过的现象。下面的代码是我在官网上给的demo基础上修改的,

public class MainActivity extends Activity implements View.OnClickListener, AMap.CancelableCallback, LocationSource, AMapLocationListener {

    private MapView mMapView;
    private AMap mAmap;
    private Polyline mVirtureRoad;
    private Marker mMoveMarker;

    // 通过设置间隔时间和距离可以控制速度和图标移动的距离
    private static final int TIME_INTERVAL = 80;
    private static final double DISTANCE = 0.0001;
    private static final double EARTH_RADIUS = 6378137;

    private ImageView imv_service_tobe_up_down;

    private OnLocationChangedListener mListener;
    private AMapLocationClient mlocationClient;
    private AMapLocationClientOption mLocationOption;


    private Intent mIntent;
    double x = 39.954368;
    double y = 116.478038;
    int z = 1;
    boolean isFlag;
    TextView text;
    LatLng arrag;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mMapView = (MapView) findViewById(R.id.map);
        imv_service_tobe_up_down = (ImageView) findViewById(R.id.imv_service_tobe_up_down);
        imv_service_tobe_up_down.setOnClickListener(this);

        mMapView.onCreate(savedInstanceState);

        mAmap = mMapView.getMap();
        setUpMap();


        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new MyTasks(), 0, 1000);

    }

    class MyTasks extends TimerTask {

        @Override
        public void run() {
            Message message = Message.obtain();
            message.what = 1;
            handler.sendMessage(message);
        }
    }


    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                x = x + 0.00001;
                y = y + 0.00001;
                initData(x, y);
                moveLooper();
            }
        }
    };

    private void initData(double x, double y) {
        LatLng arrag = new LatLng(x, y);
        List<LatLng> latLngs = setData(arrag);

        PolylineOptions polylineOptions = new PolylineOptions();
        MarkerOptions markerOptions = new MarkerOptions();

        polylineOptions.addAll(latLngs);
        polylineOptions.width(10);
        polylineOptions.color(Color.RED);
        mVirtureRoad = mAmap.addPolyline(polylineOptions);
        markerOptions.setFlat(true);
        markerOptions.anchor(0.5f, 0.5f);

        if (mMoveMarker != null) {
            mMoveMarker.destroy();
        }
        LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
        View view = inflater.inflate(R.layout.layout_ic, null);
        text = (TextView) view.findViewById(R.id.text);

        markerOptions.icon(BitmapDescriptorFactory.fromView(view));
        markerOptions.position(polylineOptions.getPoints().get(0));
        mMoveMarker = mAmap.addMarker(markerOptions);


    }


    /**
     * 根据点获取图标转的角度
     */
    private double getAngle(int startIndex) {
        if ((startIndex + 1) >= mVirtureRoad.getPoints().size()) {
            throw new RuntimeException("index out of bonds");
        }
        LatLng startPoint = mVirtureRoad.getPoints().get(startIndex);
        LatLng endPoint = mVirtureRoad.getPoints().get(startIndex + 1);
        return getAngle(startPoint, endPoint);
    }

    /**
     * 根据两点算取图标转的角度
     */
    private double getAngle(LatLng fromPoint, LatLng toPoint) {
        double slope = getSlope(fromPoint, toPoint);
        if (slope == Double.MAX_VALUE) {
            if (toPoint.latitude > fromPoint.latitude) {
                return 0;
            } else {
                return 180;
            }
        }
        float deltAngle = 0;
        if ((toPoint.latitude - fromPoint.latitude) * slope < 0) {
            deltAngle = 180;
        }
        double radio = Math.atan(slope);
        double angle = 180 * (radio / Math.PI) + deltAngle - 90;
        return angle;
    }

    /**
     * 根据点和斜率算取截距
     */
    private double getInterception(double slope, LatLng point) {

        double interception = point.latitude - slope * point.longitude;
        return interception;
    }

    /**
     * 算取斜率
     */
    private double getSlope(int startIndex) {
        if ((startIndex + 1) >= mVirtureRoad.getPoints().size()) {
            throw new RuntimeException("index out of bonds");
        }
        LatLng startPoint = mVirtureRoad.getPoints().get(startIndex);
        LatLng endPoint = mVirtureRoad.getPoints().get(startIndex + 1);
        return getSlope(startPoint, endPoint);
    }

    /**
     * 算斜率
     */
    private double getSlope(LatLng fromPoint, LatLng toPoint) {
        if (toPoint.longitude == fromPoint.longitude) {
            return Double.MAX_VALUE;
        }
        double slope = ((toPoint.latitude - fromPoint.latitude) / (toPoint.longitude - fromPoint.longitude));
        return slope;

    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onResume() {
        super.onResume();
        mMapView.onResume();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onPause() {
        super.onPause();
        mMapView.onPause();
        deactivate();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mMapView.onSaveInstanceState(outState);
    }

    /**
     * 计算x方向每次移动的距离
     */
    private double getXMoveDistance(double slope) {
        if (slope == Double.MAX_VALUE) {
            return DISTANCE;
        }
        return Math.abs((DISTANCE * slope) / Math.sqrt(1 + slope * slope));
    }

    /**
     * 循环进行移动逻辑
     */
    public void moveLooper() {

        new Thread() {

            @Override
            public void run() {
                super.run();
                for (int i = 0; i < mVirtureRoad.getPoints().size() - 1; i++) {
                    LatLng startPoint = mVirtureRoad.getPoints().get(i);
                    LatLng endPoint = mVirtureRoad.getPoints().get(i + 1);

                    mMoveMarker.setPosition(startPoint);

                    //mMoveMarker.setRotateAngle((float) getAngle(startPoint, endPoint));

                    double slope = getSlope(startPoint, endPoint);
                    //是不是正向的标示(向上设为正向)
                    boolean isReverse = (startPoint.latitude > endPoint.latitude);

                    double intercept = getInterception(slope, startPoint);

                    double xMoveDistance = isReverse ? getXMoveDistance(slope)
                            : -1 * getXMoveDistance(slope);

                    for (double j = startPoint.latitude; !((j > endPoint.latitude) ^ isReverse); j = j - xMoveDistance) {
                        LatLng latLng = null;
                        if (slope != Double.MAX_VALUE) {
                            latLng = new LatLng(j, (j - intercept) / slope);
                        } else {
                            latLng = new LatLng(j, startPoint.longitude);
                        }
                        mMoveMarker.setPosition(latLng);
                        try {
                            Thread.sleep(TIME_INTERVAL);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }

            }
        }.start();
    }

    @Override
    public void onClick(View view) {

        Toast.makeText(this, "我是你大爷", Toast.LENGTH_LONG).show();


    }

    @Override
    public void onFinish() {


    }

    @Override
    public void onCancel() {

    }

    @Override
    public void activate(OnLocationChangedListener listener) {
        mListener = listener;
        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内部会移除
            mAmap.animateCamera(CameraUpdateFactory.zoomIn(), 800, null);
            mlocationClient.startLocation();
        }

    }

    @Override
    public void deactivate() {
        mListener = null;
        if (mlocationClient != null) {
            mlocationClient.stopLocation();
            mlocationClient.onDestroy();
        }
        mlocationClient = null;

    }

    @Override
    public void onLocationChanged(AMapLocation amapLocation) {
        if (mListener != null && amapLocation != null) {
            if (amapLocation != null && amapLocation.getErrorCode() == 0) {

                mListener.onLocationChanged(amapLocation);// 显示系统小蓝点
            } else {
                String errText = "定位失败," + amapLocation.getErrorCode() + ": " + amapLocation.getErrorInfo();
                Log.e("AmapErr", errText);
            }
        }
    }

    /**
     * 设置一些amap的属性
     */
    private void setUpMap() {
        MyLocationStyle myLocationStyle = new MyLocationStyle();
        myLocationStyle.myLocationIcon(BitmapDescriptorFactory.fromResource(R.drawable.img_sendcar_location_point));// 设置小蓝点的图标
        myLocationStyle.anchor(0.5f, 0.5f);//设置小蓝点的锚点
        myLocationStyle.strokeColor(Color.BLACK);// 设置圆形的边框颜色
        myLocationStyle.radiusFillColor(Color.argb(100, 0, 0, 180));// 设置圆形的填充颜色
        myLocationStyle.strokeWidth(1.0f);// 设置圆形的边框粗细
        mAmap.setMyLocationStyle(myLocationStyle);

        mAmap.setLocationSource(this);// 设置定位监听
        mAmap.getUiSettings().setMyLocationButtonEnabled(true);// 设置默认定位按钮是否显示
        mAmap.setMyLocationEnabled(true);// 设置为true表示显示定位层并可触发定位,false表示隐藏定位层并不可触发定位,默认是false
        // 设置定位的类型为定位模式 ,可以由定位、跟随或地图根据面向方向旋转几种
        mAmap.setMyLocationType(AMap.LOCATION_TYPE_LOCATE);


    }


    /**
     * 方法必须重写
     */
    @Override
    protected void onDestroy() {

        mMapView.onDestroy();
        super.onDestroy();
    }


    /**
     * 过滤只每次保留两个点经纬度。
     */

    private List<LatLng> latLngs = new ArrayList<>();
    private boolean flag;
    private boolean tag;

    private List<LatLng> setData(LatLng latLng) {
        if (!flag) {
            flag = true;
            if (latLngs != null) {
                latLngs.clear();
            }
            latLngs.add(0, latLng);
            latLngs.add(1, new LatLng(0, 0));
        } else {
            if (!tag) {
                tag = true;
            } else {
                LatLng _latLng = latLngs.get(1);
                latLngs.set(0, _latLng);
            }
            latLngs.set(1, latLng);
        }
        return latLngs;
    }}

回复

使用道具 举报

最佳答案
334 

8

主题

2587

帖子

5772

积分

超级版主

Rank: 8Rank: 8

积分
5772
沙发
发表于 2016-5-9 11:47:15 | 只看该作者
建议楼主把经纬度打印出来看一下,我发现它还有回到起点的效果,是不是计算的问题啊
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|小黑屋|高德开发者论坛

Copyright ©2014 高德开发者论坛.All Rights Reserved |京ICP证070711号

意见反馈 常见问题 服务条款 联系我们
快速回复 返回顶部 返回列表