首页 \ 问答 \ GoogleMaps v2 for Android:在渲染地图时无法删除标记(GoogleMaps v2 for Android: Cannot remove marker while map is being rendered)

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
更新时间:2023-10-31 21:10

最满意答案

是的,取消引用值和转换数字类型在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 than f32 can represent accurately

Unfortunately, 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:

相关问答

更多
  • 简短回答:为了获得最大的灵活性,您可以将回调存储为装箱的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 / ...
  • 您需要将实际模式传递给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 ...
  • 这是一个开始,有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 ...
  • 如果您计算许多数字的平均值,则您的公式是正确的。 在这种情况下,您可以执行以下操作: μn= 1 /nΣxi 但是在这里添加第101个数字时,你需要将x 101添加到μ100,其中μ100与x 101相比可能相当大,所以你会失去一些精度。 为了避免这个问题,你可以这样: μ101=μ100+ 1 / n(x 101 - μ100) 如果x i具有相同的数量级,则此公式要好得多,因为您避免处理两个大数和x i之间的算术运算。 您可能希望阅读文章数值稳定的算术计算方法 让我们看看数字在IEEE浮点中的表示方式。 ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。