WP7 Bing Maps Zoom level based on Push Pin collection locations

这一生的挚爱 提交于 2019-12-24 23:26:17

问题


Just a quick question regarding the use of the following code snippet:

var locations = CurrentItems.Select(model => model.Location);
map.SetView(LocationRect.CreateLocationRect(locations));

as suggested in this answer: Zoom to show all locations in bing maps

I am retrieving a list of geocoordinate asynchrounsly and binding these to a Bing Map using an ObservableCollection; copying the resultant data over to the main UI thread using:

Deployment.Current.Dispatcher.BeginInvoke( ()=> {...} )

My problem is that, I can't reference the map control within the Dispatcher (or can I??), so how can I apply the new Pushpin locations to the map using:

map.SetView(LocationRect.CreateLocationRect(locations));

Thanks, S.


回答1:


Because Map ultimately derives from DependencyObject it actually has its own Dispatcher. As such you can do;

map.Dispatcher.BeginInvoke(() => map.SetView(LocationRect.CreateLocationRect(locations)));

Also, it's worth noting you only need to call BeginInvoke() if the CheckAccess() returns false. (CheckAccess is tagged with an EditorBrowsable(EditorBrowsableState.Never) attribute so it won't show up in intellisense, you'll have to type it manually). The common pattern is;

if (map.Dispatcher.CheckAccess() == false) {
  map.Dispatcher.BeginInvoke(() => map.setView(LocationRect.CreateLocationRect(locations)));
} else {
  map.SetView(LocationRect.CreateLocationRect(locations));
}



回答2:


I perhaps you will find this post useful. To bind the view of the map and the ViewModel, the method described use a DependecyPropety : http://sveiberg.wordpress.com/2012/06/24/5/.



来源:https://stackoverflow.com/questions/10515367/wp7-bing-maps-zoom-level-based-on-push-pin-collection-locations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!