在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--; }