GoogleMaps v2 for Android:在渲染地图时无法删除标记(GoogleMaps v2 for Android: Cannot remove marker while map is being rendered)
我有一个片段:
- GoogleMap v2片段
- 用于添加标记的“位置”按钮(用户的位置)
- “删除地点”按钮(删除用户生成的标记)
- 其他标记(如一般兴趣点)
- 自定义图块叠加
当用户点击“places”按钮时,app会存储一个带有标记引用的hashmap,并将对象放在WeakHashMap中。 当用户点击“删除地点”时,应用会迭代调用marker.remove()的地图键。
完全渲染地图后,将正确删除标记,但是,如果在渲染地图时按下按钮,则不会删除标记。
任何人都遇到过同样的问题? 怎么解决?
我不能使用map.clear(),因为它删除了所有标记,折线,叠加等。我只想删除以前存储的标记列表(用户的位置)而不是一切。
I have a fragment with:
- GoogleMap v2 fragment
- A "places" button to add markers (user's places)
- A "remove places" button (remove user generated markers)
- Additional markers (such as general point of interests)
- Custom tile overlay
When user clicks on "places" button, the app stores a hashmap with references to markers and places object in a WeakHashMap. When user clicks "remove places" the app iterates over the map keys calling marker.remove().
When the map is completely rendered, the markers are removed properly, but, if the button is pressed while map is being rendered, then the markers are not removed.
Anyone has experienced the same problem? How to solve it?
I cannot use map.clear() since it removes all markers, polylines, overlays, etc. I just want to remove a previously stored list of markers (user's places) not everything.
原文:https://stackoverflow.com/questions/25422488
最满意答案
是的,取消引用值和转换数字类型在Rust中是正常的。 这些转换有助于程序员识别边缘情况。 正如loganfsmyth指出 :
i32
可以保存大于f32
值,可以准确表示不幸的是,编译器无法判断出你的情况是什么“正确”,所以你仍然必须保持警惕。
为了它的价值,我将使用
Iterator::sum
编写您当前的实现:fn mean(items: &[i32]) -> f64 { let sum: f64 = items.iter().map(|&v| v as f64).sum(); sum / (items.len() as f64) }
您还应该处理输入为空的情况,以避免除以零:
fn mean(items: &[i32]) -> Option<f64> { let len = items.len(); if len == 0 { None } else { let sum: f64 = items.iter().map(|&v| v as f64).sum(); Some(sum / (len as f64)) } }
使用什么是一个很好的解决方案来计算所有值的总和超过双倍限制的平均值? ,但做了更多迭代器:
fn mean2(ary: &[i32]) -> f64 { ary.iter().enumerate().fold(0.0, |avg, (i, &x)| { avg + ((x as f64 - avg) / (i + 1) as f64) }) }
也可以看看:
Yes, dereferencing values and converting numeric types is normal in Rust. These conversions help the programmer recognize that edge cases are possible. As loganfsmyth points out:
An
i32
can hold values greater thanf32
can represent accuratelyUnfortunately, the compiler can't tell what's "correct" for your case, so you still have to be on guard.
For what it's worth, I'd write your current implementation using
Iterator::sum
:fn mean(items: &[i32]) -> f64 { let sum: f64 = items.iter().map(|&v| v as f64).sum(); sum / (items.len() as f64) }
You should also probably handle the case where the input is empty to avoid dividing by zero:
fn mean(items: &[i32]) -> Option<f64> { let len = items.len(); if len == 0 { None } else { let sum: f64 = items.iter().map(|&v| v as f64).sum(); Some(sum / (len as f64)) } }
Using the method from What is a good solution for calculating an average where the sum of all values exceeds a double's limits?, but made a bit more iterator-heavy:
fn mean2(ary: &[i32]) -> f64 { ary.iter().enumerate().fold(0.0, |avg, (i, &x)| { avg + ((x as f64 - avg) / (i + 1) as f64) }) }
See also:
相关问答
更多-
Rust中的习惯性回调(Idiomatic callbacks in Rust)[2021-08-02]
简短回答:为了获得最大的灵活性,您可以将回调存储为装箱的FnMut对象,回调设置器通用回调类型。 这个代码显示在答案的最后一个例子中。 有关更详细的解释,请继续阅读。 “函数指针”:回调为fn 在问题中最接近的C ++代码将声明回调为fn类型。 fn封装了由fn关键字定义的函数,很像C ++的函数指针: type Callback = fn(); struct Processor { callback: Callback, } impl Processor { fn set_callba ... -
是的,取消引用值和转换数字类型在Rust中是正常的。 这些转换有助于程序员识别边缘情况。 正如loganfsmyth指出 : i32可以保存大于f32值,可以准确表示 不幸的是,编译器无法判断出你的情况是什么“正确”,所以你仍然必须保持警惕。 为了它的价值,我将使用Iterator::sum编写您当前的实现: fn mean(items: &[i32]) -> f64 { let sum: f64 = items.iter().map(|&v| v as f64).sum(); sum / ...
-
Rust“如果让”不工作?(Rust “if let” not working?)[2023-07-01]
您需要将实际模式传递给if let (与Swift之类的语言不同,特殊情况if let Option类型): if let Some(doc) = xml::parse_file("filename") { doc.simple_function(); } You need to pass the actual pattern to if let (unlike languages like Swift which special case if let for Option types): if ... -
按R计算平均值(Computing averages by groups with R)[2023-09-08]
这是一个开始,有data.table , plyr和base函数,还有很多其他方法...... 首先,一些示例数据...... dput(examp) structure(list(P = structure(c(1L, 1L, 1L, 2L), .Label = c("P1", "P2"), class = "factor"), M = structure(c(1L, 1L, 2L, 2L), .Label = c("M1", "M2"), class = "factor"), R = structu ... -
基于tribonacci序列的定义,我发现你可以用更简洁的方式编写它,如下所示: fn options(stairs: i32) -> i32 { match stairs { 0 => 0, 1 => 1, 2 => 1, 3 => 2, _ => options(stairs - 1) + options(stairs - 2) + options(stairs - 3) } } 我还建议将功能定义更改为仅 ...
-
tl; dr:Rust中的“拥有”类型并不是什么魔术,它们当然也不会硬编码到编译器或语言中。 它们只是以某种方式编写的类型(不实现Copy且可能具有析构函数),并且具有通过非可复制性和析构函数强制执行的某些语义。 Rust核心的所有制机制非常简单,规则非常简单。 首先,我们来定义什么是移动 。 这很简单 - 当一个新名称变为可用时,就会说这个值被移动 ,并停止在旧名称下可用: struct X(u32); let x1 = X(12); let x2 = x1; // x1 is no longer acc ...
-
这是一个简单的基于列表的解决方案,虽然需要更多的内存,但它足够惯用且速度足够快。 import Data.List (tails) mavg :: Fractional b => Int -> [b] -> [b] mavg k lst = take (length lst-k) $ map average $ tails lst where average = (/ fromIntegral k) . sum . take k 该解决方案允许在移动窗口中使用任何函数而不是average 。 以下 ...
-
是的,你应该尝试强制A使用类似于df['A'] = df['A'].astype(int) 。 可能值得检查初始数据读入中是否有任何内容导致它成为对象而不是数字。 Yeah, you should try coercing A to numeric with something like df['A'] = df['A'].astype(int). Might be worth checking if there's anything in the initial data read-in that cau ...
-
这就是std::collections::hash_map::Entry的用途: 查看地图中的单个条目,可以是空置或占用。 match hashmap_cache.entry(key) { Entry::Vacant(entry) => *entry.insert(value), Entry::Occupied(entry) => *entry.get(), } 如果条目是空的,则将值插入高速缓存中,否则从高速缓存接收该值。 操场 This is what std::collections ...
-
计算平均值的舍入误差(Rounding error in computing average)[2023-04-07]
如果您计算许多数字的平均值,则您的公式是正确的。 在这种情况下,您可以执行以下操作: μn= 1 /nΣxi 但是在这里添加第101个数字时,你需要将x 101添加到μ100,其中μ100与x 101相比可能相当大,所以你会失去一些精度。 为了避免这个问题,你可以这样: μ101=μ100+ 1 / n(x 101 - μ100) 如果x i具有相同的数量级,则此公式要好得多,因为您避免处理两个大数和x i之间的算术运算。 您可能希望阅读文章数值稳定的算术计算方法 让我们看看数字在IEEE浮点中的表示方式。 ...