在Google地图上移动标记以及道路或ployline

时间:2016-07-26 09:44:02

标签: android google-maps location google-maps-markers google-direction

我正在尝试跟踪车辆的位置,但面临的问题是标记没有与道路一起动画。我正在使用谷歌方向API。我的折线和标记正确放置在路上,但当我移动标记时,它不会沿着道路移动。

这是我的代码

   public void requestDirection() {
    Snackbar.make(btnRequestDirection, "Direction Requesting...", Snackbar.LENGTH_SHORT).show();
    GoogleDirection.withServerKey(serverKey)
            .from(origin)
            .to(destination)
            .transportMode(TransportMode.TRANSIT)
            .execute(this);
}

@Override
public void onDirectionSuccess(Direction direction, String rawBody) {
    Snackbar.make(btnRequestDirection, "Success with status : " + direction.getStatus(), Snackbar.LENGTH_SHORT).show();
    if (direction.isOK()) {
        final ArrayList<LatLng> sectionPositionList = direction.getRouteList().get(0).getLegList().get(0).getSectionPoint();
        for (LatLng position : sectionPositionList) {
            googleMap.addMarker(new MarkerOptions().position(position));

        }

        List<Step> stepList = direction.getRouteList().get(0).getLegList().get(0).getStepList();
        ArrayList<PolylineOptions> polylineOptionList = DirectionConverter.createTransitPolyline(this, stepList, 5, Color.RED, 3, Color.BLUE);
        for (PolylineOptions polylineOption : polylineOptionList) {
            googleMap.addPolyline(polylineOption);

        }
        my.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                 myMarker = googleMap.addMarker(new MarkerOptions()
                        .position(origin)
                        .icon(BitmapDescriptorFactory.fromResource(R.mipmap.ic_launcher))
                        .title("Hello world"));

                googleMap.moveCamera(CameraUpdateFactory.newCameraPosition(new CameraPosition.Builder()
                        .target(googleMap.getCameraPosition().target)
                        .zoom(17)
                        .bearing(30)
                        .tilt(45)
                        .build()));


                final LatLng startPosition = myMarker.getPosition();
                final LatLng finalPosition = new LatLng(19.103528,72.887962);
                final Handler handler = new Handler();
                final long start = SystemClock.uptimeMillis();
                final Interpolator interpolator = new AccelerateDecelerateInterpolator();
                final float durationInMs = 30000;
                final boolean hideMarker = false;

                handler.post(new Runnable() {
                    long elapsed;
                    float t;
                    float v;

                    @Override
                    public void run() {
                        // Calculate progress using interpolator
                        elapsed = SystemClock.uptimeMillis() - start;
                        t = elapsed / durationInMs;
                        v = interpolator.getInterpolation(t);

                        LatLng currentPosition = new LatLng(
                                startPosition.latitude*(1-t)+finalPosition.latitude*t,
                                startPosition.longitude*(1-t)+finalPosition.longitude*t);

                        myMarker.setPosition(currentPosition);

                        // Repeat till progress is complete.
                        if (t < 1) {
                            // Post again 16ms later.
                            handler.postDelayed(this, 16);
                        } else {
                            if (hideMarker) {
                                myMarker.setVisible(false);
                            } else {
                                myMarker.setVisible(true);
                            }
                        }
                    }
                });


            }
        });

    }
}

1 个答案:

答案 0 :(得分:4)

addMarker返回Marker对象。

Marker marker = googleMap.addMarker(new MarkerOptions().position(entry.getValue()).title(entry.getKey()));

使用此对象更改其位置:

marker.setPosition(new LatLng(5, 5));

每次只做设定位置时不要创建标记

快乐的编码! :)