将车辆位置观察者设置为单利模式

This commit is contained in:
董宏宇
2021-04-20 11:06:10 +08:00
parent 1a79d96906
commit 8b93fd267e
3 changed files with 22 additions and 7 deletions

View File

@@ -69,7 +69,7 @@ public class V2XLocationListener
private CarLocationSubject mCarLocationSubject;
private V2XLocationListener() {
mCarLocationSubject = new CarLocationSubject();
mCarLocationSubject = CarLocationSubject.getInstance();
// 注册最优路线的推荐观察者
mCarLocationSubject.registerObserver(
V2XOptimalRouteObserver.TYPE,

View File

@@ -12,12 +12,27 @@ import java.util.Set;
*/
public class CarLocationSubject {
private static CarLocationSubject mCarLocationSubject;
// 车辆位置
public MogoLocation carLocation;
// 观察者集合
private HashMap<String, CarLocationObserver> observers = new HashMap<String, CarLocationObserver>();
private HashMap<String, CarLocationObserver> mObservers = new HashMap<>();
private CarLocationSubject() {
}
public static CarLocationSubject getInstance() {
if (mCarLocationSubject == null) {
synchronized (CarLocationSubject.class) {
if (mCarLocationSubject == null) {
mCarLocationSubject = new CarLocationSubject();
}
}
}
return mCarLocationSubject;
}
/**
* 设置新的车辆位置
*
@@ -35,7 +50,7 @@ public class CarLocationSubject {
* @param observer 新的观察者
*/
public void registerObserver(String observerType, CarLocationObserver observer) {
observers.put(observerType, observer);
mObservers.put(observerType, observer);
}
/**
@@ -44,16 +59,16 @@ public class CarLocationSubject {
* @param observerType 观察者类型
*/
public void removeObserver(String observerType) {
observers.remove(observerType);
mObservers.remove(observerType);
}
/**
* 通知所有观察者更新
*/
private void notifyAllObservers() {
Set<String> keySet = observers.keySet();
Set<String> keySet = mObservers.keySet();
for (String s : keySet) {
CarLocationObserver observer = observers.get(s);
CarLocationObserver observer = mObservers.get(s);
if (observer != null) {
observer.update(carLocation);
}

View File

@@ -57,7 +57,7 @@ public class V2XOptimalRouteOverlay {
}
}
mPolylineColors.addAll(ColorUtils.gradientAlpha("#002965ED", "#FF2965ED", mPolylinePointList.size()/2));
mPolylineColors.addAll(ColorUtils.gradientAlpha("#002965ED", "#FF2965ED", mPolylinePointList.size() / 3));
// 替换路径集合
mPolylineOptions.points(mPolylinePointList);