gpt4 book ai didi

windows-runtime - MapControl 区分用户或程序中心更改

转载 作者:行者123 更新时间:2023-12-01 05:08:29 27 4
gpt4 key购买 nike

在 WinRt/WP 8.1 MapControl 中,如何区分用户何时通过滑动更改屏幕中心与程序更改?

WinRt/WP 8.1 MapControl 有一个 CenterChanged 事件 ( http://msdn.microsoft.com/en-us/library/windows.ui.xaml.controls.maps.mapcontrol.centerchanged.aspx ),但这并没有提供有关导致中心更改的原因的任何信息。

有没有其他方法可以知道用户是否更改了 map 中心?

/* 为了提供更多上下文,我的具体场景如下:
给定一个显示 map 的应用程序,我想跟踪用户的 GPS 位置。

  • 如果找到 gps 位置,我想在 map 上放一个点并将 map 居中到该点。
  • 如果发现 gps 位置变化,我想将 map 居中到该点。
  • 如果用户通过触摸/滑动更改 map 的位置,当 gps 位置更改时,我不再希望将 map 居中。

  • 我可以通过比较 gps 位置和中心来解决这个问题,但是他的 gps 位置 latLng 与 Map.Center latLng 的类型和精度不同。我更喜欢更简单、更简单的解决方案。
    */

    最佳答案

    我通过设置一个 bool 值来解决这个问题 ignoreNextViewportChangestrue在调用等待之前 TrySetViewAsync并将其重置为 false异步操作完成后。

    在事件处理程序中,我立即打破常规然后 ignoreNextViewportChanges仍然是真的。

    所以最后,它看起来像:

    bool ignoreNextViewportChanges;

    public void HandleMapCenterChanged() {
    Map.CenterChanged += (sender, args) => {
    if(ignoreNextViewportChanges)
    return;
    //if you came here, the user has changed the location
    //store this information somewhere and skip SetCenter next time
    }
    }

    public async void SetCenter(BasicGeoposition center) {
    ignoreNextViewportChanges = true;
    await Map.TrySetViewAsync(new Geopoint(Center));
    ignoreNextViewportChanges = false;
    }

    如果您遇到 SetCenter 可能被并行调用两次的情况(因此 SetCenter 的最后一次调用尚未完成,但 SetCenter 再次被调用),您可能需要使用计数器:
    int viewportChangesInProgressCounter;

    public void HandleMapCenterChanged() {
    Map.CenterChanged += (sender, args) => {
    if(viewportChangesInProgressCounter > 0)
    return;
    //if you came here, the user has changed the location
    //store this information somewhere and skip SetCenter next time
    }
    }

    public async void SetCenter(BasicGeoposition center) {
    viewportChangesInProgressCounter++;
    await Map.TrySetViewAsync(new Geopoint(Center));
    viewportChangesInProgressCounter--;
    }

    关于windows-runtime - MapControl 区分用户或程序中心更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26733022/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com