Is there a Geopoint class available for Winforms (or an Analogue thereof)?

拈花ヽ惹草 提交于 2021-01-07 02:34:44

问题


In my UWP app, I use a Geopoint class:

using Windows.Devices.Geolocation;

. . .

List<Geopoint> locations;

In a Winforms app, this is not available - Geopoint is not recognized. Is there an analogous class available for Winforms apps?

The same is true for the BasicGeoposition object - not recognized.

UPDATE

I want the GeoPoint and BasicGeoposition classes so I can do things like this:

BasicGeoposition location = new BasicGeoposition();
location.Latitude = 36.59894360222391; // Monterey == 36.6002° N
location.Longitude = -121.8616426604813; // Monterey == 121.8947° W (West is negative)
Geopoint geop = new Geopoint(location);
await map.TrySetSceneAsync(MapScene.CreateFromLocation(geop));
cmbxZoomLevels.SelectedIndex = Convert.ToInt32(map.ZoomLevel - 1);
map.Style = MapStyle.Aerial3DWithRoads;

UPDATE 2

I tried the code provided in the answer:

this.UserControl1.myMap.AnimationLevel = AnimationLevel.Full;
this.userControl11.myMap.Loaded += MyMap_Loaded;

...but it won't compile. I don't have a UserControl11 (which is what the answer's code has), but I do have a UserControl1, yet it is not recognized:

This is the XAML in question (Bing Maps key obfuscated):

<UserControl x:Class="MyMaps.UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
   xmlns:m="clr-namespace:Microsoft.Maps.MapControl.WPF;assembly=Microsoft.Maps.MapControl.WPF">
    <Grid>
        <m:Map CredentialsProvider="Gr8GooglyMoogly" x:Name="myMap" />
    </Grid>
</UserControl>

回答1:


To set the view of the Bing Maps WPF control, you can use SetView method. The method have different overloads, for example you can pass a Location(which you create based on the latitude and longitude of your desired location) and a zoom-level to the method like this:

var location = new Location(47.604, -122.329);
this.userControl11.myMap.SetView(location, 12);

Same can be achieved by setting Center and ZoomLevel.

Download or Clone the example

You can download or close the working example from here:

  • Clone r-aghaei/WinFormsWpfBingMaps
  • Download master.zip

Step by Step Example - Zoom into Seattle as initial view

  1. Follow instructions in this post to create a Windows Forms project which uses WPF Bing Maps Control.

  2. Handle the Load event of the Form and use the following code:

     private void Form1_Load(object sender, EventArgs e)
     {
         this.userControl11.myMap.AnimationLevel = AnimationLevel.Full;
         this.userControl11.myMap.Loaded += MyMap_Loaded;
     }
     private void MyMap_Loaded(object sender, System.Windows.RoutedEventArgs e)
     {
         var location = new Location(47.604, -122.329);
         this.userControl11.myMap.SetView(location, 12);
     }
    

    Make sure you use using Microsoft.Maps.MapControl.WPF;.

As a result, the map zooms in Seattle as center location:

More information:

You may want to take a look at the following links for more information:

  • How can I add a Bing Maps Component to my C# Winforms app?
  • Bing Maps WPF Control
  • Developing with the Bing Maps WPF Control
  • Bing Maps WPF Control API Reference



回答2:


For those who are looking to use Windows Community Toolkit Map Control which is different from Bing Maps WPF Control, you can follow these steps to use Windows Community Toolkit Map Control for Windows Forms.

Note: Windows 10 (introduced v10.0.17709.0) is a prerequisite.

  1. Create a Windows Forms Application (.NET Framework >=4.6.2 - I tried myself with 4.7.2)

  2. Install Microsoft.Toolkit.Forms.UI.Controls NuGet package.

  3. Add an app.manifest file: Right-click on project → Add New Item → Choose Application Manifest File (Windows Only) which is located under General node.

  4. Open the app.manifest file and uncomment the supportedOS under <!-- Windows 10 -->:

    <!-- Windows 10 -->
    <supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />
    
  5. Handle the Load event of your form and add the following code:

    private void Form1_Load(object sender, EventArgs e)
    {
        var map = new MapControl();
        map.Dock = DockStyle.Fill;
        map.MapServiceToken = "YOUR KEY";
        map.LoadingStatusChanged += async (obj, args) =>
        {
            if (map.LoadingStatus == MapLoadingStatus.Loaded)
            {
                var cityPosition = new BasicGeoposition() { 
                    Latitude = 47.604, Longitude = -122.329 };
                var cityCenter = new Geopoint(cityPosition);
                await map.TrySetViewAsync(cityCenter, 12);
            }
        };
        this.Controls.Add(map);
    }
    

    Also make sure you include required usings:

    using Microsoft.Toolkit.Forms.UI.Controls;
    using Microsoft.Toolkit.Win32.UI.Controls.Interop.WinRT;
    

    Note 1: I was unable to add the control in designer because of an exception on design-time when I tried to drop the control on form, so I decided to use add it at run-time.

    Note 2: You need to Get a Key to use map; however for test purpose you may ignore getting the key.

  6. Run your application and see the result:

More information

  • MapControl for Windows Forms and WPF
  • Source code: Microsoft.Toolkit.Forms.UI.Controls.MapControl
  • WinForms control is a wrapper around WPF Windows.UI.Xaml.Controls.Maps.MapControl
  • Display maps with 2D, 3D, and Streetside views


来源:https://stackoverflow.com/questions/65481096/is-there-a-geopoint-class-available-for-winforms-or-an-analogue-thereof

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