gpt4 book ai didi

android - 在谷歌地图上沿道路或多段线移动标记

转载 作者:行者123 更新时间:2023-11-29 15:42:09 24 4
gpt4 key购买 nike

我正在尝试跟踪车辆的位置,但面临着标记未随道路移动的问题。为此,我正在使用 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);
}
}
}
});


}
});

}
}

最佳答案

addMarker 返回标记对象。

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

使用这个对象来改变它的位置:

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

不要每次都创建标记只是设置位置

快乐编码! :)

关于android - 在谷歌地图上沿道路或多段线移动标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38586400/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com