首页 > 解决方案 > 如何将路线上的对象从起点移动到终点

问题描述

请我知道我是否使用当前位置,当时我看到了很多答案。但是我的位置来自主要问题的服务。服务后,我将获得所有起点和终点。现在我怎样才能动画这个起点到终点?我已经完成了 0 索引到 1 索引的动画移动,但其余部分我没有。现在我需要帮助来解决这个问题。

我有一个从我的服务中获得的位置,我在谷歌地图中创建了路线图。但现在我想显示一个移动的标记或车辆,并通过起点到终点对其进行动画处理。

在此处输入图像描述

如何实现从起点到终点的动画?这是我的方法,我用折线显示路线起点到终点。

    private void setMapMarker(GoogleMap googleMap) {
    googleMap.clear();
    MapsInitializer.initialize(Objects.requireNonNull(getActivity()).getApplicationContext());
    api = ApiClient.getClient().create(ApiInterface.class);

    LocationInfo locationInfo = new LocationInfo();
    locationInfo.setMOV_DATE(fromDate.getText().toString());
    locationInfo.setEMPLOYE_ID(employeeID);
    locationInfo.setSTART_TIME(fromTime.getText().toString());
    locationInfo.setEND_TIME(toTime.getText().toString());

    Call<List<RouteList>> listCall = api.getLocation(locationInfo);
    APIHelper.enqueueWithRetry(listCall, new Callback<List<RouteList>>() {
        @Override
        public void onResponse(Call<List<RouteList>> call, Response<List<RouteList>> response) {
            try {
                if (response.isSuccessful()) {
                    List<RouteList> list = response.body();

                    Map<String, List<RouteList>> map = getEmployeeList(list);

                    if (list == null || list.isEmpty() || list.equals(0)) {
                        googleMap.clear();
                        Toast.makeText(getActivity(), "No data found", Toast.LENGTH_SHORT).show();
                    } else {
                        for (Map.Entry<String, List<RouteList>> entry : map.entrySet()) {
                            String employee = entry.getKey();
                            List<RouteList> list1 = map.get(employee);

                            if (list1.size() > 0) {

                                List<LatLng> latlng = new ArrayList<>();
                                for (int i = 0; i < list1.size(); i++) {
                                    double lat = Double.parseDouble(list1.get(i).getM_LATITUDE().trim());
                                    double lng = Double.parseDouble(list1.get(i).getM_LONGITDE().trim());
                                    String name = list1.get(i).getEMGIS_TIME();
                                    latlng.add(new LatLng(lat, lng));
                                    LatLng mLatlng = new LatLng(lat, lng);

                                    if (googleMap != null) {
                                        googleMap.addMarker(new MarkerOptions().position(mLatlng).title(name).snippet(""));
                                        cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lng), 12.0f);
                                       googleMap.animateCamera(cameraUpdate);

                                }

                                PolylineOptions rectOptions = new PolylineOptions().addAll(latlng);
                                rectOptions.color(generator.getRandomColor());
                                Objects.requireNonNull(googleMap).addPolyline(rectOptions);

                            }
            } catch (Exception e) {
                Toast.makeText(getActivity(), "Error:" + e, Toast.LENGTH_SHORT).show();
                e.printStackTrace();
            }
        }

        @Override
        public void onFailure(Call<List<RouteList>> call, Throwable t) {
            call.cancel();
            Toast.makeText(getActivity(), t.getMessage(), Toast.LENGTH_LONG).show();
        }
    });
}

已经显示路线图。

标签: androidgoogle-maps

解决方案


I would firstly like to say that you should be more clear in your question.

It is important to know if you want to set the marker as the vehicle is driving. I will assume this is what you want to do and I will provide an answer according to this.


You already have a method that sets the marker -> setMapMarker

All you need to do now is to call this method every second or so.
You can do this by using a Thread, as shown below:

//Declare Thread
Thread thread;

void startSetMarker() {
    thread = new Thread() {
        @Override
        public void run() {
            try {
                while (!thread.isInterrupted()) {
                    Thread.sleep(1000);
                    runOnUiThread(new Runnable() {
                        @Override
                            public void run() {
                                //Call setMapMarker here
                            }
                        });
                    }
                } catch (InterruptedException e) {
            }
        }
    };
    thread.start();
}

//You can cancel the thread like this:

void cancelThread(){
    thread.interrupt();
}

By doing this, the marker will be updated every second.

When you want to start setting the marker you can call startSetMarker(); and when the user reaches the end destination, you can cancel the thread by calling cancelThread();


推荐阅读