[英]Rotate marker/car icon in google maps - Android
我正在嘗試創建一個像 ola/uber 這樣的應用程序。 我想在道路左轉或右轉時移動圖標並旋轉。 我正在使用以下代碼。
private void rotateMarker(final Marker marker, final float toRotation) {
if(!isMarkerRotating) {
final Handler handler = new Handler();
final long start = SystemClock.uptimeMillis();
final float startRotation = marker.getRotation();
final long duration = 1000;
final Interpolator interpolator = new LinearInterpolator();
handler.post(new Runnable() {
@Override
public void run() {
isMarkerRotating = true;
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);
} else {
isMarkerRotating = false;
}
}
});
}
}
計算軸承:
currentLocation = location;
if(previousLocaton!=null){
previousLocaton = tempLocation;
tempLocation = currentLocation;
Log.d("previousLocaton=====> ",""+previousLocaton);
Log.d("currentLocation=====> ",""+currentLocation);
bearing = previousLocaton.bearingTo(currentLocation) ;
}else{
previousLocaton = location;
tempLocation = location;
}
要設置軸承:
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(latLng).zoom(14).bearing(bearing).build();
要旋轉標記,我在onLocationChanged
更改方法中調用 roateMarker 方法:
currLocationMarker = mMap.addMarker(markerOptions);
rotateMarker(currLocationMarker,bearing);
現在我的圖標在旋轉。 但谷歌地圖也會旋轉。 我想單獨旋轉圖標。 我參考以下鏈接進行動畫和移動標記。 鏈接 1 。 請讓我有任何想法來解決我的問題。
有一種簡單的方法可用於標記
marker.rotation(float value)
設置標記圍繞標記的錨點順時針旋轉的度數。 旋轉軸垂直於標記。 旋轉 0 對應於標記的默認位置。 當標記在地圖上是平坦的時,默認位置是北對齊,並且旋轉使得標記在地圖上始終保持平坦。 當標記是廣告牌時,默認位置是向上的並且旋轉使得標記始終面向相機。 默認值為 0。
使用setRotation(float)
方法僅將標記集旋轉旋轉到標記。
static public void rotateMarker(final Marker marker, final float toRotation) {
final Handler handler = new Handler();
final long start = SystemClock.uptimeMillis();
final float startRotation = marker.getRotation();
final long duration = 1000;
final Interpolator interpolator = new LinearInterpolator();
L.d("Bearing: "+toRotation);
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 10ms later.
handler.postDelayed(this, 10);
}
}
});
}
嘗試這個 :
public void animateMarker(final LatLng toPosition, final LatLng startLatLng,
final boolean hideMarker) {
final Handler handler = new Handler();
final long start = SystemClock.uptimeMillis();
Projection proj = map.getProjection();
Point startPoint = proj.toScreenLocation(d_marker.getPosition());
final LatLng startLatLng = proj.fromScreenLocation(startPoint);
// final CameraPosition newcameraPosition = null;
final Interpolator interpolator = new LinearInterpolator();
handler.post(new Runnable() {
@Override
public void run() {
Location prevLoc = new Location("service Provider");
prevLoc.setLatitude(startLatLng.latitude);
prevLoc.setLongitude(startLatLng.longitude);
Location newLoc = new Location("service Provider");
newLoc.setLatitude(toPosition.latitude);
newLoc.setLongitude(toPosition.longitude);
System.out.println("Locations ---- " + prevLoc + "-" + newLoc);
float bearing = prevLoc.bearingTo(newLoc);
long elapsed = SystemClock.uptimeMillis() - start;
float t = interpolator.getInterpolation((float) elapsed
/ 1000);
double lng = t * toPosition.longitude + (1 - t)
* startLatLng.longitude;
double lat = t * toPosition.latitude + (1 - t)
* startLatLng.latitude;
d_marker.setPosition(new LatLng(lat, lng));
d_marker.setRotation(bearing);
d_marker.setFlat(true);
// googleMap.moveCamera(CameraUpdateFactory.newCameraPosition(newcameraPosition));
if (t < 1.0) {
// Post again 16ms later.
handler.postDelayed(this, 16);
} else {
if (hideMarker) {
d_marker.setVisible(false);
} else {
d_marker.setVisible(true);
}
}
}
});
}
試試下面的方法:
public void animateMarker(final LatLng toPosition,
final boolean hideMarker) {
final Handler handler = new Handler();
final long start = SystemClock.uptimeMillis();
Projection proj = googleMap.getProjection();
Point startPoint = proj.toScreenLocation(cabMarker.getPosition());
final LatLng startLatLng = proj.fromScreenLocation(startPoint);
// final CameraPosition newcameraPosition = null;
final Interpolator interpolator = new LinearInterpolator();
handler.post(new Runnable() {
@Override
public void run() {
Location prevLoc = new Location("service Provider");
prevLoc.setLatitude(startLatLng.latitude);
prevLoc.setLongitude(startLatLng.longitude);
Location newLoc = new Location("service Provider");
newLoc.setLatitude(toPosition.latitude);
newLoc.setLongitude(toPosition.longitude);
System.out.println("Locations ---- " + prevLoc + "-" + newLoc);
float bearing = prevLoc.bearingTo(newLoc);
long elapsed = SystemClock.uptimeMillis() - start;
float t = interpolator.getInterpolation((float) elapsed
/ 1000);
double lng = t * toPosition.longitude + (1 - t)
* startLatLng.longitude;
double lat = t * toPosition.latitude + (1 - t)
* startLatLng.latitude;
cabMarker.setPosition(new LatLng(lat, lng));
cabMarker.setRotation(bearing);
// googleMap.moveCamera(CameraUpdateFactory.newCameraPosition(newcameraPosition));
if (t < 1.0) {
// Post again 16ms later.
handler.postDelayed(this, 16);
} else {
if (hideMarker) {
cabMarker.setVisible(false);
} else {
cabMarker.setVisible(true);
}
}
}
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.