【问题标题】:How to move marker from one location to another location when we have a list of locations on google map android当我们在谷歌地图android上有一个位置列表时,如何将标记从一个位置移动到另一个位置
【发布时间】:2016-07-20 06:46:19
【问题描述】:

我正在创建带有多个标记的谷歌地图。现在我想在这些标记之间移动一个汽车图标。使用这个Move markers in google map v2 android

我可以将汽车图标从第一点移动到第二点。但它不会从第二点移动到第三点。当我为此使用 for 循环时,它会直接进入最后一点。我也添加了延迟,但没有其他效果我。

提前致谢。

这是我的代码::

public void setAnimation(GoogleMap myMap, final List<LatLng> directionPoint) {

        anim_map = myMap;


        anim_marker = myMap.addMarker(new MarkerOptions()
                .icon(BitmapDescriptorFactory.fromResource(R.drawable.bus_icon))
                .position(directionPoint.get(0))
                .flat(true));


        myMap.animateCamera(CameraUpdateFactory.newLatLngZoom(directionPoint.get(0), 10));

        if (directionPoint.size() >= 2) {

            for (int i = 0; i < directionPoint.size() - 1; i++) {
                h.post(new Runnable() {
                    @Override
                    public void run() {
                        animateMarker(anim_map, anim_marker, directionPoint, false);



                    }
                });


            }

        }


    }

private void animateMarker(GoogleMap myMap, final Marker marker, final List<LatLng> directionPoint,
                               final boolean hideMarker) {

        final long start = SystemClock.uptimeMillis();
        final long duration = 350000;
        final Interpolator interpolator = new LinearInterpolator();


        h.post(new Runnable() {
            int i = 0;

            @Override
            public void run() {
                long elapsed = SystemClock.uptimeMillis() - start;
                float t = interpolator.getInterpolation((float) elapsed
                        / duration);

                //  Log.e("T Location", t + "");


                double lng = t * directionPoint.get(i + 1).longitude + (1 - t) * directionPoint.get(i).longitude;
                double lat = t * directionPoint.get(i + 1).latitude + (1 - t) * directionPoint.get(i).latitude;
                marker.setPosition(new LatLng(lat, lng));

                if (t < 1.0) {
                    h.postDelayed(this, 1);
                } else {
                    if (hideMarker) {
                        marker.setVisible(false);
                    } else {
                        marker.setVisible(true);
                    }
                }


            }
        });


    }

【问题讨论】:

    标签: android google-maps


    【解决方案1】:

    你可以试试这种方式,让标记在GoogleMap 中移动,类似于实时行车方向跟踪

        private Marker mCurrentMarker;
        private float ZOOMLEVEL=18.0f;
        private LatLng previousLatLon;
        private Handler mLocalHandler;
        private GoogleMap mGoogleMap;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            mLocalHandler = new Handler();
            previousLatLon=new LatLng(45.320372, 2.460161);
            //Initialize Marker once Google Map object is created
            mMarkerOptions = new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.drawable.custom_marker_icon));
            mMarkerOptions.position(previousLatLon);
            mCurrentMarker = mGoogleMap.addMarker(mMarkerOptions);
    
    
        }
    
        /**
         * Call this method to move marker in map to new location in map with updated location
         * @param marker
         * @param toPosition
         * @param fromPosition
         */
        public void animateMarker(final Marker marker, final LatLng toPosition,final LatLng fromPosition) {
    
    
            final long duration = 500;
            final Interpolator interpolator = new LinearInterpolator();
    
            mLocalHandler.post(new Runnable() {
                @Override
                public void run() {
                    long elapsed = SystemClock.uptimeMillis() - mStartTime;
                    float t = interpolator.getInterpolation((float) elapsed
                            / duration);
                    marker.setPosition(toPosition);
                    marker.setAnchor(Constants.MAPANCHOR, Constants.MAPANCHOR);
                    mGoogleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(toPosition, ZOOMLEVEL));
                    if (t < 1.0) {
                        // Post again 16ms later.
                        mLocalHandler.postDelayed(this, 16);
                    } else {
                        marker.setVisible(true);
                    }
                    }
                }
            });
            previousLatLon=toPosition;// reassign the previous location to current location
        }
    

    查看我的回答了解更多详情here

    在你的情况下,在 for 循环之后休眠几毫秒

    for (int i = 0; i < directionPoint.size() - 1; i++) {
    try {
     Thread.sleep(5000);
    } catch (InterruptedException e) 
    {
     e.printStackTrace();
    }
    // rest of your code
    

    【讨论】:

    • 我可以得到正确的位置。如果我增加 i 值,它将移动到目标点。但我的问题是,当 i inc i 值时,这并没有缓慢移动。当我打开应用程序时从第一次开始,循环就完成了。我看不到。
    • 据我了解,如果您在循环中对值进行硬编码,则需要放置 delay 。示例:10 秒。然后你就可以看到了。
    • 也许你可以添加相关代码,没有它很难找到问题
    • 在你的 for 循环之后,把这个 try { Thread.sleep(8000); } catch (InterruptedException e) { e.printStackTrace(); }
    【解决方案2】:

    最后,我想出了这个答案。 我们必须根据 arraylist 的大小更新所有值。

      public void setAnimation(GoogleMap myMap, final List<LatLng> directionPoint) {
    
            anim_map = myMap;
    
    
            anim_marker = myMap.addMarker(new MarkerOptions()
                    .icon(BitmapDescriptorFactory.fromResource(R.drawable.bus_icon))
                    .position(directionPoint.get(0))
                    .flat(true));
    
    
            myMap.animateCamera(CameraUpdateFactory.newLatLngZoom(directionPoint.get(0), 10));
    
    
            animateMarker(anim_map, anim_marker, directionPoint, false);
    
    
        }
    
        private void animateMarker(GoogleMap myMap, final Marker marker, final List<LatLng> directionPoint,
                                   final boolean hideMarker) {
    
            final long start = SystemClock.uptimeMillis();
            long duration = 3500;
            final Interpolator interpolator = new LinearInterpolator();
    
            float t = 0;
    
            for (int j = 0; j < directionPoint.size() - 1; j++) {
                Log.e("Location Value", j + " " + directionPoint.get(j).latitude + ", " + directionPoint.get(j).longitude + " : " + directionPoint.get(j + 1).latitude + ", " + directionPoint.get(j + 1).longitude);
                while (t < j + 1) {
                    t = t - j;
                    double lng = t * directionPoint.get(j + 1).longitude + (1 - t) * directionPoint.get(j).longitude;
                    double lat = t * directionPoint.get(j + 1).latitude + (1 - t) * directionPoint.get(j).latitude;
                    LatList.add(new LatLng(lat, lng));
                    long elapsed = SystemClock.uptimeMillis() - start;
                    t = interpolator.getInterpolation((float) elapsed / duration);
                }
            }
            Log.e("LatList value", LatList.size() + "");
            h.post(new Runnable() {
                int i = 0;
    
                @Override
                public void run() {
                    marker.setPosition(LatList.get(i));
                    if ((LatList.size() - 1) > i)
                        h.postDelayed(this, 1);
                    Log.e("I value", i + "");
                    i++;
                }
            });
    
    
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-21
      相关资源
      最近更新 更多