【问题标题】:Is there a Geopoint class available for Winforms (or an Analogue thereof)?是否有可用于 Winforms(或其类似物)的 Geopoint 类?
【发布时间】:2020-12-28 16:53:42
【问题描述】:

在我的 UWP 应用中,我使用 Geopoint 类:

using Windows.Devices.Geolocation;

. . .

List<Geopoint> locations;

在 Winforms 应用程序中,此功能不可用 - 无法识别 Geopoint。是否有可用于 Winforms 应用程序的类似类?

BasicGeoposition 对象也是如此 - 无法识别。

更新

我想要 GeoPoint 和 BasicGeoposition 类,所以我可以这样做:

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;

更新 2

我尝试了答案中提供的代码:

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

...但它不会编译。我没有 UserControl11(这是答案的代码所具有的),但我有一个 UserControl1,但无法识别:

这是有问题的 XAML(必应地图密钥已混淆):

<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>

【问题讨论】:

  • 类似的课程对你有用吗?虽然创建一个具有相似类型的 properties 的类非常简单,但我不确定它是否有很大帮助。您的确切要求/问题是什么? (另外请指定 .NET 版本、操作系统,以及是否要使用 WPF 必应地图或 UWP 必应地图)
  • WPF Map 没有TrySetSceneAsync 方法。你可以看看它的方法hereTrySetSceneAsync 属于 Windows Community Toolkit Map Control
  • 您是否可以选择使用 Windows Community ToolKit Map Control?
  • 酷,所以你现在有了一个可行的解决方案。 WPF Bing Maps 已经足够好了,我只是问一下你要使用哪一个,看看我是否可以帮助解决这个问题。
  • 你试过SetVeiw或者设置CenterZoomLevel吗?

标签: winforms bing-maps geopoints uwp-maps


【解决方案1】:

要设置Bing Maps WPF control的视图,可以使用SetView方法。该方法有不同的重载,例如您可以将Location(您根据所需位置的纬度和经度创建)和缩放级别传递给这样的方法:

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

同样可以通过设置CenterZoomLevel来实现。

下载或克隆示例

您可以从这里下载或关闭工作示例:

分步示例 - 放大西雅图作为初始视图

  1. 按照this post 中的说明创建一个使用 WPF Bing 地图控件的 Windows 窗体项目。

  2. 处理Form的Load事件,使用如下代码:

     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);
     }
    

    确保您使用using Microsoft.Maps.MapControl.WPF;

因此,地图以西雅图为中心位置进行缩放:

更多信息:

您可能需要查看以下链接以获取更多信息:

【讨论】:

  • Winforms 似乎没有表单的 Loaded 事件(只是 Load)。哦,我明白了,加载的事件是针对地图元素的...
  • Load 事件属于Form。我已经处理了它,然后在加载事件处理程序中,我为Map 控件的Loaded 事件注册了一个事件处理程序。
  • 请参阅我的更新 2。
  • 您是否按照链接帖子中的说明进行操作(您之前的问题)?当您将 ElementHost 放在表单上,​​然后将其指定为 UserControl 时,UserControl 的一个实例将被添加到您的表单中,您可以对其进行操作。这里没有魔法,你可能遗漏了一些东西,例如我看到你使用了 this.UserControl1.myMap.AnimationLevel = AnimationLevel.Full; 这是错误的,应该是 userControl11.
  • 我创建了一个 GitHub 仓库;您可以在答案中找到存储库地址和下载链接,希望对您有所帮助?
【解决方案2】:

对于那些希望使用不同于Bing Maps WPF ControlWindows Community Toolkit Map Control 的用户,您可以按照以下步骤使用Windows 窗体的Windows Community Toolkit Map Control

注意:Windows 10(引入 v10.0.17709.0)是先决条件。

  1. 创建一个 Windows 窗体应用程序(.NET Framework >=4.6.2 - 我自己尝试使用 4.7.2)

  2. 安装Microsoft.Toolkit.Forms.UI.Controls NuGet 包。

  3. 添加一个 app.manifest 文件:右键单击项目 → 添加新项目 → 选择 Application Manifest File (Windows Only) 其中位于General节点下。

  4. 打开app.manifest文件,取消&lt;!-- Windows 10 --&gt;supportedOS的注释:

    <!-- Windows 10 -->
    <supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />
    
  5. 处理表单的Load 事件并添加以下代码:

    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);
    }
    

    还要确保包含所需的用途:

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

    注 1: 我无法在设计器中添加控件,因为当我试图将控件放在表单上时,设计时出现异常,所以我决定在运行时使用添加它 -时间。

    注意2:你需要Get a Key才能使用地图;但是出于测试目的,您可能会忽略获取密钥。

  6. 运行您的应用程序并查看结果:

更多信息

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多