如何在Xamarin表单地图中设置动态缩放级别

时间:2018-12-06 06:49:18

标签: c# xamarin.forms

我想在Xamarin Form Map加载地图时动态设置地图的缩放级别。我正在尝试使用Working with Maps。缩放级别正在更改滑块值的代码中起作用。

slider.ValueChanged += (sender, e) => {
            var zoomLevel = e.NewValue; // between 1 and 18
          var latlongdegrees = 360 / (Math.Pow(2, zoomLevel));
          // // Debug.WriteLine(zoomLevel + " -> " + latlongdegrees);
            if (map.VisibleRegion != null)
                map.MoveToRegion(new MapSpan(map.VisibleRegion.Center, latlongdegrees, latlongdegrees));
        };

但是我想移除滑块并在设备上加载地图时设置缩放级别16.71,并且不想使用滑块。我该怎么做

我使用的完整代码

 map = new Map
        {
            //IsShowingUser = true,
            HeightRequest = 100,
            WidthRequest = 960,
            VerticalOptions = LayoutOptions.FillAndExpand,
            MapType=MapType.Street,

        };
       // map.MoveToRegion(new MapSpan(new Position(28.6587067, 77.14514),360,360));
        map.MoveToRegion(MapSpan.FromCenterAndRadius(new Position(28.6587067, 77.14514), Distance.FromMiles(3)));
        var position = new Position(28.6587067, 77.14514);
        var pin = new Pin
        {
            Type = PinType.Place,
            Position = position,
            Label = "xx"
            //,
            //Address = "We are here"
        };
        map.Pins.Add(pin);
       // var latlongdegrees = 360 / (Math.Pow(2, 17.51));
        //if (map.VisibleRegion != null)
         //   map.MoveToRegion(new MapSpan(map.VisibleRegion.Center, latlongdegrees, latlongdegrees));

        // add the slider
        var slider = new Slider(16, 18, 1);
        slider.ValueChanged += (sender, e) => {
            var zoomLevel = e.NewValue; // between 1 and 18
          var latlongdegrees = 360 / (Math.Pow(2, zoomLevel));
          // // Debug.WriteLine(zoomLevel + " -> " + latlongdegrees);
            if (map.VisibleRegion != null)
                map.MoveToRegion(new MapSpan(map.VisibleRegion.Center, latlongdegrees, latlongdegrees));
        };


        // create map style buttons
        var street = new Button { Text = "Street" };
        var hybrid = new Button { Text = "Hybrid" };
        var satellite = new Button { Text = "Satellite" };
        street.Clicked += HandleClicked;
        hybrid.Clicked += HandleClicked;
        satellite.Clicked += HandleClicked;
        var segments = new StackLayout
        {
            Spacing = 30,
            HorizontalOptions = LayoutOptions.CenterAndExpand,
            Orientation = StackOrientation.Horizontal,
            Children = { street, hybrid, satellite }
        };


        // put the page together
        var stack = new StackLayout { Spacing = 0 };
        stack.Children.Add(map);
        stack.Children.Add(slider);
        stack.Children.Add(segments);
        Content = stack;


        // for debugging output only
        map.PropertyChanged += (sender, e) => {
            Debug.WriteLine(e.PropertyName + " just changed!");
            if (e.PropertyName == "VisibleRegion" && map.VisibleRegion != null)
                CalculateBoundingCoordinates(map.VisibleRegion);
        };

1 个答案:

答案 0 :(得分:1)

实际上很简单:

  • 假设MyMap是地图的对象参考,则可以使用MoveToRegion方法设置缩放级别:

    MyMap.MoveToRegion(MapSpan.FromCenterAndRadius(
    new Position(39,-132), Distance.FromMiles(2))); // accepts position, distance
    
  • 上面的示例将地图移至某个位置并尝试将其插入视口约两英里,或者您可以通过指定“纬度”和“经度”来创建“ span”这个:

    new MapSpan(new Position(30.235, -97.733), 0.05, 0.05)
    
  • 在指定“纬度”和“经度”时,您可以将其转换为更简单的值(例如1到18之间的范围)

    var zoomLevel = 5; // pick a value between 1 and 18
    var latlongdeg = 360 / (Math.Pow(2, zoomLevel));
    map.MoveToRegion(new MapSpan (map.VisibleRegion.Center, latlongdeg, latlongdeg));
    
  • 注意,使用VisibleRegion.Center属性将意味着在缩放级别更改时,地图将保持在当前位置的中心。

更新

定义新的地图对象后,您需要执行以下操作:

map = new Map
    {
        //IsShowingUser = true,
        HeightRequest = 100,
        WidthRequest = 960,
        VerticalOptions = LayoutOptions.FillAndExpand,
        MapType=MapType.Street,

    };
var zoomLevel = 16.71; // pick a value between 1 and 18
var latlongdeg = 360 / (Math.Pow(2, zoomLevel));
map.MoveToRegion(new MapSpan (map.VisibleRegion.Center, latlongdeg, latlongdeg));