开发者

Change Pushpin visibility based on Zoom level in the Bing Maps control for Silverlight

So i'm pretty new to using the Bing Maps control in Silverlight, but I have managed to get a collection of pushpin objects (each with lat/long values) plotted on the map.

My question no开发者_运维技巧w is, how can I change the visibility of these based on the current zoom level?.

Say I have 10 locations scattered across the UK, I only want them visible when the UK is in the current view, not when the whole world is in view...

I realise this is a very general question but any help would be fantastic!

Kris


you need to handle one of the map controls events, like viewchangeend or TargetViewChanged and decide whether to show the pins based on the new views zoom level and bounding box (the lat/lons that make up the boundary of the new view)

http://msdn.microsoft.com/en-us/library/microsoft.maps.mapcontrol.map_events.aspx


It will help if you understand that

  • GeoCoordinate and Location are compatible.
  • The clipping rectangle is deliberately somewhat larger than the map so that the edge of the marker for points slightly off the map will be visible, cueing the user to pan.

Set up an extension method.

public static class ExtensionMethods
{
  public static bool Contains(this LocationRect r, GeoCoordinate p)
  {
    double w = r.West - 0.2;
    double e = r.East + 0.2;
    double s = r.South - 0.2;
    double n = r.North + 0.2;
    return w <= p.Longitude && p.Longitude <= e && s <= p.Latitude && p.Latitude <= n;
  }
}

Check whether each pin is in the map bounds with an expression like this.

map.TargetBoundingRectangle.Contains(pin.Location)

If your points are in an IEnumerable then import LINQ and you can process them all in one go with an expression like this, which returns a collection containing only the pins you need to show.

pushpins.Where(pin => map.TargetBoundingRectangle.Contains(pin.Location))

Do the clipping in the map's ViewChangeEnd event handler. If you have a lot of pins, the answer to your next question is "quadtree". Look it up.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜