加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

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

发布时间:2020-12-14 05:43:21 所属栏目:Windows 来源:网络整理
导读:在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),但是这并
在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),但是这并不提供有关导致中心更改的信息.

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

/ *为了提供更多上下文,我的具体情况如下:
给定一个显示地图的应用程序,我想跟踪用户的GPS位置.

>如果找到gps位置,我想在地图上放一个点并将地图居中到那一点.
>如果找到gps位置更改,我想将地图居中到那一点.
>如果用户通过触摸/滑动更改地图的位置,我不再希望在gps位置更改时使地图居中.

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

解决方法

我通过在调用等待的TrySetViewAsync之前将bool ignoreNextViewportChanges设置为true来解决此问题,并在异步操作完成后将其重置为false.

在事件处理程序中,我立即打破Routine然后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--;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读