首先我们大家都知道百度地图中的Marker是基于经纬度也就是一个点来得到自身存在的位置的,但是我们如果想让Marker 移动呢?跳点我们大家都会,只需要重新为Marker赋值一个坐标点就好了,但是如果我们要平滑移动呢,我的做法就是为坐标点频繁赋值,使其看起来像是移动过去的,好了,废话不多说了,看效果吧
上面花了条线就是为了让我们看到Marker的移动轨迹,但是我们即使是频繁赋值也还是有问题的,我们还要考虑到箭头所指的方向,也就是根据起始点和目标点来计算角度
直接上代码吧:
-
package com.jlau.wjyuezhao.baidumapdemo;
-
-
import android.content.Context;
-
import android.content.Intent;
-
import android.graphics.Color;
-
import android.os.Handler;
-
import android.os.Looper;
-
import android.util.Log;
-
-
import com.baidu.mapapi.map.BaiduMap;
-
import com.baidu.mapapi.map.BitmapDescriptorFactory;
-
import com.baidu.mapapi.map.MapStatus;
-
import com.baidu.mapapi.map.MapStatusUpdateFactory;
-
import com.baidu.mapapi.map.MapView;
-
import com.baidu.mapapi.map.Marker;
-
import com.baidu.mapapi.map.MarkerOptions;
-
import com.baidu.mapapi.map.OverlayOptions;
-
import com.baidu.mapapi.map.Polyline;
-
import com.baidu.mapapi.map.PolylineOptions;
-
import com.baidu.mapapi.model.LatLng;
-
import com.baidu.mapapi.utils.DistanceUtil;
-
-
import java.text.SimpleDateFormat;
-
import java.util.ArrayList;
-
import java.util.Date;
-
import java.util.List;
-
-
-
-
-
-
public class MarkMoveUtil {
-
-
private static double sl0 ;
-
-
private static double slt;
-
-
private static double el0;
-
-
private static double elt;
-
-
private static double param ;
-
private BaiduMap mBaiduMap;
-
private Polyline mPolyline;
-
private Marker mMoveMarker;
-
-
private Handler mHandler;
-
private MapView mMapView;
-
-
private static List latlngs;
-
-
private double time_interval = 8;
-
private Context context;
-
-
private final String ACTION_NAME = "发送广播";
-
-
-
-
-
-
-
-
-
-
-
public MarkMoveUtil(double sl0,double slt,double param,MapView mMapView,Marker marker,Context context
-
){
-
this.sl0 = sl0;
-
this.slt = slt;
-
this.param = param;
-
this.mMapView = mMapView;
-
this.mBaiduMap = mMapView.getMap();
-
this.mMoveMarker = marker;
-
this.context = context;
-
mHandler = new Handler(Looper.getMainLooper());
-
MapStatus.Builder builder = new MapStatus.Builder();
-
builder.target(new LatLng(slt,sl0));
-
builder.zoom(11.0f);
-
mBaiduMap.setMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));
-
}
-
-
-
-
-
-
-
-
-
-
private static void getLatlngs(double sl0, double slt, double el0, double elt){
-
Log.e("CCC",el0+":"+elt);
-
double a = elt - slt;
-
double b = el0 - sl0;
-
double c = a/param;
-
double d = b/param;
-
latlngs = new ArrayList<>();
-
for(int i = 0;i < param;i++){
-
double lat = slt + c * i;
-
double lon = sl0 + d * i;
-
latlngs.add(new LatLng(lat,lon) );
-
}
-
}
-
-
-
-
-
public void drawPolyLine() {
-
List polylines = new ArrayList<>();
-
for (int index = 0; index < latlngs.size(); index++) {
-
polylines.add(latlngs.get(index));
-
}
-
polylines.add(latlngs.get(0));
-
PolylineOptions polylineOptions = new PolylineOptions().points(polylines).width(10).color(Color.RED);
-
mPolyline = (Polyline) mBaiduMap.addOverlay(polylineOptions);
-
}
-
-
-
-
-
private double getAngle(int startIndex) {
-
if ((startIndex + 1) >= mPolyline.getPoints().size()) {
-
throw new RuntimeException("index out of bonds");
-
}
-
LatLng startPoint = mPolyline.getPoints().get(startIndex);
-
LatLng endPoint = mPolyline.getPoints().get(startIndex + 1);
-
return getAngle(startPoint, endPoint);
-
}
-
-
-
-
-
private double getAngle(LatLng fromPoint, LatLng toPoint) {
-
double slope = getSlope(fromPoint, toPoint);
-
if (slope == Double.MAX_VALUE) {
-
if (toPoint.latitude > fromPoint.latitude) {
-
return 0;
-
} else {
-
return 180;
-
}
-
}
-
float deltAngle = 0;
-
if ((toPoint.latitude - fromPoint.latitude) * slope < 0) {
-
deltAngle = 180;
-
}
-
double radio = Math.atan(slope);
-
double angle = 180 * (radio / Math.PI) + deltAngle - 90;
-
return angle;
-
}
-
-
-
-
-
private double getInterception(double slope, LatLng point) {
-
-
double interception = point.latitude - slope * point.longitude;
-
return interception;
-
}
-
-
-
-
-
private double getSlope(LatLng fromPoint, LatLng toPoint) {
-
if (toPoint.longitude == fromPoint.longitude) {
-
return Double.MAX_VALUE;
-
}
-
double slope = ((toPoint.latitude - fromPoint.latitude) / (toPoint.longitude - fromPoint.longitude));
-
return slope;
-
}
-
private double getDistance(){
-
double distance = DistanceUtil.getDistance(latlngs.get(0), latlngs.get((int) param -1));
-
double v = distance /10000;
-
double theDistance = v * (time_interval/1000);
-
return theDistance;
-
}
-
-
-
-
-
public void moveLooper() {
-
new Thread() {
-
-
public void run() {
-
-
-
Log.e("TAG", "start time:"+getCurrentTime());
-
for (int i = 0; i < latlngs.size() - 1; i++) {
-
final LatLng startPoint = latlngs.get(i);
-
final LatLng endPoint = latlngs.get(i+1);
-
mMoveMarker
-
.setPosition(startPoint);
-
-
mHandler.post(new Runnable() {
-
@Override
-
public void run() {
-
-
if (mMapView == null) {
-
return;
-
}
-
mMoveMarker.setRotate((float) getAngle(startPoint,
-
endPoint));
-
}
-
});
-
double slope = getSlope(startPoint, endPoint);
-
-
boolean isReverse = (startPoint.latitude > endPoint.latitude);
-
double intercept = getInterception(slope, startPoint);
-
double xMoveDistance = isReverse ? getDistance() :
-
-1 * getDistance();
-
for (double j = startPoint.latitude; !((j > endPoint.latitude) ^ isReverse);
-
j = j - xMoveDistance) {
-
LatLng latLng = null;
-
if (slope == Double.MAX_VALUE) {
-
latLng = new LatLng(j, startPoint.longitude);
-
} else {
-
latLng = new LatLng(j, (j - intercept) / slope);
-
}
-
final LatLng finalLatLng = latLng;
-
mHandler.post(new Runnable() {
-
@Override
-
public void run() {
-
if (mMapView == null) {
-
return;
-
}
-
mMoveMarker.setPosition(finalLatLng);
-
}
-
});
-
try {
-
Thread.sleep((int)time_interval);
-
} catch (InterruptedException e) {
-
e.printStackTrace();
-
}
-
}
-
}
-
Log.e("TAG", "end time:"+getCurrentTime());
-
sl0 = el0;
-
slt = elt;
-
Intent mIntent = new Intent(ACTION_NAME);
-
mIntent.putExtra("yaner", "发送广播,相当于在这里传送数据");
-
-
context.sendBroadcast(mIntent);
-
-
}
-
}.start();
-
}
-
-
public static void start(LatLng latLng){
-
el0 = latLng.longitude;
-
elt =latLng.latitude;
-
getLatlngs(sl0,slt,el0,elt);
-
}
-
private String getCurrentTime(){
-
SimpleDateFormat formatter = new SimpleDateFormat ("yyyy年MM月dd日 HH:mm:ss ");
-
Date curDate = new Date(System.currentTimeMillis());
-
String str = formatter.format(curDate);
-
return str;
-
}
-
-
-
-
-
-
-
-
}
至于具体的调用方法就不多说了,直接初始化这个工具之后调用划线和移动的方法就好了,这样我们的Marker移动就支持平滑,拐点,计算角度了,由于源码程序太大,暂时不支持CSDN下载,有需要的朋友可以留言。。
阅读(9118) | 评论(1) | 转发(0) |