我正在尝试更新地图上的用户位置。因此,我将LocationListener定义如下:
public class UserLocationListener implements LocationListener {
@Override
public void locationUpdated(Location location) {
User.getInstance().actualLocation.set(location);
}
@Override
public void providerStateChanged(int newState) {
}
}
该actualLocation
是一个属性定义以下的属性CN1指南。
在模拟器中,如果我使用位置模拟器移动用户位置,则会触发locationUpdated。
现在在我的MainForm类构造函数中,如果我添加:
User.getInstance().actualLocation.addChangeListener((p) -> {
System.err.println("User location has changed");
// Update user location on the map
}
尽管地图显示有标记,但它从未被触发(请参阅参考资料myMap.addMarker(...)
)。
所以我的问题是:为什么不触发此变更侦听器,我应该在哪里放置它以使所有工作正常进行?
任何帮助表示赞赏,
位置代码重用相同的位置对象实例,只是在其中更改值。set(T)
如果新值!=
是旧值,则property中的方法仅会引发更改事件:
public K set(T value) {
if(this.value != value) {
this.value = value;
firePropertyChanged();
}
if(parent == null) {
// allows properties to work even if they aren't registered in the index
return null;
}
return (K)parent.parent;
}
因此,可以使用以下方法作为变通方法:
public void locationUpdated(Location location) {
Location l = new Location();
l.setLatitude(location.getLatitude());
... // etc. sucks that we don't have new Location(location)
User.getInstance().actualLocation.set(l);
}
我不确定我们是否需要它是错误的。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句