gpt4 book ai didi

java - 如何根据方向旋转标记(总线)?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:42:32 27 4
gpt4 key购买 nike

从下面的代码我在 map 上添加标记,每 15 秒刷新一次并从数据库中获取新的纬度和经度。标记(巴士图像)已成功添加到 map 上并从一个位置平稳移动到另一个位置,就像汽车在路上行驶一样。现在我想要的是根据方向旋转总线标记。我怎样才能做到这一点?我不明白 toRotation 和 st 的值(value)是多少?

public Runnable runLocation = new Runnable() {
@Override
public void run() {
gps = new GPSTracker(MapActivity.this);
MyLocation1.clear();
if (gps.CanGetLocation()) {
double latitude = gps.getLatitude();
double longitude = gps.getLongitude();
LatLng mylocation = new LatLng(latitude, longitude);
if (marker != null) {
marker.remove();
}
if (circle != null) {
circle.remove();
}
if (busMarker != null){
lastBusPos = busMarker.getPosition();
}

marker = mMap.addMarker(new MarkerOptions()
.icon(BitmapDescriptorFactory.fromResource(R.drawable.location))
.title("My Location")
.position(mylocation));

circle = mMap.addCircle(new CircleOptions()
.center(mylocation)
.radius(1000)
.strokeColor(0x10000000)
.fillColor(0x10000000));
} else {
// gps.showSettingsAlert();
}

String tag_json_obj = "json_obj_req";
String url = AppConfig.RouteData + "i=1&" + "y=1";

JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {

@Override
public void onResponse(JSONObject response) {
try {

JSONObject jObj = new JSONObject(String.valueOf(response));
JSONArray json_user = jObj.getJSONArray("Message");
for (int i = 0; i < json_user.length(); i++) {

try {

JSONObject obj = json_user.getJSONObject(i);

final Double currLat = obj.getDouble("actual_lat");
final Double currLong = obj.getDouble("actual_long");
final LatLng hcmus = new LatLng(currLat, currLong);


List<LatLng> latList = new ArrayList<LatLng>();
latList.add(hcmus);

if (mMarkers.size() != json_user.length()) {
busMarker = mMap.addMarker(new MarkerOptions()
.icon(BitmapDescriptorFactory.fromResource(R.drawable.bus))
.title("Bus No" + obj.getString("bus_id"))
.position(hcmus));
mMarkers.add(busMarker);
} else {
//busMarker.setPosition(hcmus);
animateMarker(hcmus, false);
rotateMarker(busMarker, 45.0f, 45.0f);
}

} catch (JSONException e) {
e.printStackTrace();
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {

@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MapActivity.this, "Sorry something went wrong..Try again", Toast.LENGTH_SHORT).show();
}
});

// Adding request to request queue
AppController.getInstance().addToRequestQueue(jsonObjReq, tag_json_obj);

MapActivity.this.handler.postDelayed(MapActivity.this.runLocation, 15000);
}

};

/*-------------------------------Animation----------------------------*/
public void rotateMarker(final Marker marker, final float toRotation, final float st) {
final Handler handler = new Handler();
final long start = SystemClock.uptimeMillis();
final float startRotation = st;
final long duration = 1555;

final Interpolator interpolator = new LinearInterpolator();

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

float rot = t * toRotation + (1 - t) * startRotation;

marker.setRotation(-rot > 180 ? rot / 2 : rot);
if (t < 1.0) {
// Post again 16ms later.
handler.postDelayed(this, 16);
}
}
});
}

public void animateMarker(final LatLng toPosition,final boolean hideMarke) {
final Handler handler = new Handler();
final long start = SystemClock.uptimeMillis();
Projection proj = mMap.getProjection();
Point startPoint = proj.toScreenLocation(lastBusPos);
final LatLng startLatLng = proj.fromScreenLocation(startPoint);
final long duration = 5000;

final Interpolator interpolator = new LinearInterpolator();

handler.post(new Runnable() {
@Override
public void run() {
long elapsed = SystemClock.uptimeMillis() - start;
float t = interpolator.getInterpolation((float) elapsed
/ duration);
double lng = t * toPosition.longitude + (1 - t)
* startLatLng.longitude;
double lat = t * toPosition.latitude + (1 - t)
* startLatLng.latitude;
busMarker.setPosition(new LatLng(lat, lng));

if (t < 1.0) {
// Post again 16ms later.
handler.postDelayed(this, 16);
} else {
if (hideMarke) {
busMarker.setVisible(false);
} else {
busMarker.setVisible(true);
}
}
}
});
}

/*--------------------------------------------------------------------*/

最佳答案

您可以引用这个related thread .使用 bearing Location 对象然后将其设置为 CameraPosition

If you use GPS for locating the user then the Location object you get in onLocationChanged contains the bearing.

If you only have the two coordinates (e.g. you only have coordinates from network location provider), you can use Location.bearingTo() to calculate the bearing of two coordinates:

Location prevLoc = ... ;
Location newLoc = ... ;
float bearing = prevLoc.bearingTo(newLoc) ;

If you have a bearing, you can set the rotation of the marker using MarkerOptions.rotation():

mMap.addMarker(new MarkerOptions()
.position(markerLatLng)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.map_marker))
.anchor(0.5f, 0.5f)
.rotation(bearing)
.flat(true));

You have to set the anchor to the point you want to rotate around, and it's also the point you want to be at the position you set to the marker. (0.5, 0.5) is the center of the image.

以下是一些可能也有帮助的帖子:

关于java - 如何根据方向旋转标记(总线)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43224263/

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