none
Silverlight bing maps 怎么显示限定区域和限定zoomlevel的地图 RRS feed

  • 问题

  • 比如我只要显示某个城市的地图,但是我不希望浏览者再往上缩小,并且限制其只能看到本城市的地图,我用的是瓦片数据,谢谢

    2010年5月26日 8:28

答案

  • 您可以定制MapMode,我是根据这位仁兄的代码修改的,具体您可以参考他的blog:

    http://www.cnblogs.com/LionGG/archive/2009/11/17/1604987.html

     

    public class ChinaMode : RoadMode
    {
      private readonly Range<double> validLatitudeRange = new Range<double> (3.92, 53.51);
      private readonly Range<double> validLongitudeRange = new Range<double> (73.60, 135.01);
    
      public ChinaMode ()
      {
        var tileLayer = (MapTileLayer)this.Content;
        tileLayer.TileSources.Clear ();
    
        var tileSourceUri = new UriBuilder (
          "http://r2.tiles.ditu.live.com/tiles/r{quadkey}.png?g=41");
        var tileSource =
          new LocationRectTileSource (tileSourceUri.Uri.ToString (),
          new LocationRect (new Location (-90, -180), new Location (90, 180)),
          new Range<double> (1, 21));
        tileLayer.TileSources.Add (tileSource);
      }
    
      protected override Range<double> GetZoomRange (Location center)
      {
        return new Range<double> (10, 14);
      }
    
      public override bool ConstrainView (Location center, ref double zoomLevel, ref double heading, ref double pitch)
      {
        bool isChanged = base.ConstrainView (center, ref zoomLevel, ref heading, ref pitch);
        double newLatitude = center.Latitude;
        double newLongitude = center.Longitude;
        if (center.Longitude > validLongitudeRange.To)
        {
          newLongitude = validLongitudeRange.To;
        }
        else if (center.Longitude < validLongitudeRange.From)
        {
          newLongitude = validLongitudeRange.From;
        }
        if (center.Latitude > validLatitudeRange.To)
        {
          newLatitude = validLatitudeRange.To;
        }
        else if (center.Latitude < validLatitudeRange.From)
        {
          newLatitude = validLatitudeRange.From;
        }
        if (newLatitude != center.Latitude || newLongitude != center.Longitude)
        {
          center.Latitude = newLatitude;
          center.Longitude = newLongitude;
          isChanged = true;
        }
        Range<double> range = GetZoomRange (center);
        if (zoomLevel > range.To)
        {
          zoomLevel = range.To;
          isChanged = true;
        }
        else if (zoomLevel < range.From)
        {
          zoomLevel = range.From;
          isChanged = true;
        }
        return isChanged;
      }
    }
    在GetZoomRange方法的覆写中,可以限定允许的缩放级别,ConstrainView方法限定显示的区域。

     

    • 已标记为答案 xmuczq 2010年5月27日 0:45
    2010年5月26日 11:18

全部回复

  • 您可以定制MapMode,我是根据这位仁兄的代码修改的,具体您可以参考他的blog:

    http://www.cnblogs.com/LionGG/archive/2009/11/17/1604987.html

     

    public class ChinaMode : RoadMode
    {
      private readonly Range<double> validLatitudeRange = new Range<double> (3.92, 53.51);
      private readonly Range<double> validLongitudeRange = new Range<double> (73.60, 135.01);
    
      public ChinaMode ()
      {
        var tileLayer = (MapTileLayer)this.Content;
        tileLayer.TileSources.Clear ();
    
        var tileSourceUri = new UriBuilder (
          "http://r2.tiles.ditu.live.com/tiles/r{quadkey}.png?g=41");
        var tileSource =
          new LocationRectTileSource (tileSourceUri.Uri.ToString (),
          new LocationRect (new Location (-90, -180), new Location (90, 180)),
          new Range<double> (1, 21));
        tileLayer.TileSources.Add (tileSource);
      }
    
      protected override Range<double> GetZoomRange (Location center)
      {
        return new Range<double> (10, 14);
      }
    
      public override bool ConstrainView (Location center, ref double zoomLevel, ref double heading, ref double pitch)
      {
        bool isChanged = base.ConstrainView (center, ref zoomLevel, ref heading, ref pitch);
        double newLatitude = center.Latitude;
        double newLongitude = center.Longitude;
        if (center.Longitude > validLongitudeRange.To)
        {
          newLongitude = validLongitudeRange.To;
        }
        else if (center.Longitude < validLongitudeRange.From)
        {
          newLongitude = validLongitudeRange.From;
        }
        if (center.Latitude > validLatitudeRange.To)
        {
          newLatitude = validLatitudeRange.To;
        }
        else if (center.Latitude < validLatitudeRange.From)
        {
          newLatitude = validLatitudeRange.From;
        }
        if (newLatitude != center.Latitude || newLongitude != center.Longitude)
        {
          center.Latitude = newLatitude;
          center.Longitude = newLongitude;
          isChanged = true;
        }
        Range<double> range = GetZoomRange (center);
        if (zoomLevel > range.To)
        {
          zoomLevel = range.To;
          isChanged = true;
        }
        else if (zoomLevel < range.From)
        {
          zoomLevel = range.From;
          isChanged = true;
        }
        return isChanged;
      }
    }
    在GetZoomRange方法的覆写中,可以限定允许的缩放级别,ConstrainView方法限定显示的区域。

     

    • 已标记为答案 xmuczq 2010年5月27日 0:45
    2010年5月26日 11:18
  • 学习了,楼上正解。
    BLOG:http://beniao.cnblogs.com MSN:beniao@live.cn
    2010年5月27日 11:52
  • :)我也是前不久才学习的。
    2010年5月27日 14:37