0

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:

enter image description here

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>
B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862
  • 2
    Does an analogous class work for you? While it's pretty straightforward to create a class having similar [properties](https://learn.microsoft.com/en-us/uwp/api/windows.devices.geolocation.geopoint?view=winrt-19041&WT.mc_id=DT-MVP-5003235#properties) with similar types, but I'm not sure it can be of much of help. What's your exact requirement/problem? (Also please specify .NET Version, OS, and if you are going to use WPF Bing Maps or UWP Bing Maps) – Reza Aghaei Dec 28 '20 at 17:23
  • I want to use the class to store GeoPoint data in a database. .NET version is 4.6.2, OS is Windwos 10, and I'm using WPF Bing Maps (in a Winforms app). – B. Clay Shannon-B. Crow Raven Dec 29 '20 at 15:26
  • Please see my update for why I need the GeoPoint and BasicGeoPosition objects – B. Clay Shannon-B. Crow Raven Dec 29 '20 at 15:43
  • 1
    WPF Map doesn't have `TrySetSceneAsync` method. You can take a look at its methods [here](https://learn.microsoft.com/en-us/previous-versions/bing/wpf-control/hh709541(v=msdn.10)?WT.mc_id=DT-MVP-5003235#methods). [`TrySetSceneAsync`](https://learn.microsoft.com/en-us/uwp/api/windows.ui.xaml.controls.maps.mapcontrol.trysetsceneasync?view=winrt-19041&WT.mc_id=DT-MVP-5003235) belongs to [Windows Community Toolkit Map Control](https://learn.microsoft.com/en-us/windows/communitytoolkit/controls/wpf-winforms/mapcontrol?WT.mc_id=DT-MVP-5003235). – Reza Aghaei Dec 29 '20 at 16:19
  • 1
    Is using Windows Community ToolKit Map Control an option for you? – Reza Aghaei Dec 29 '20 at 16:34
  • It could be; I've already got the WPF BingMaps control working, though (you helped me with it, in fact); if there is a good reason to switch, though, I will. Details? – B. Clay Shannon-B. Crow Raven Dec 30 '20 at 12:19
  • 1
    Cool, so you have a working solution now. WPF Bing Maps is good enough, I just asked to make sure which one you are going to use, to see whether I can help with the issue. – Reza Aghaei Dec 30 '20 at 14:28
  • Thanks; my issue is with setting the initial "scene" to the location of my choice now. – B. Clay Shannon-B. Crow Raven Dec 30 '20 at 18:58
  • 1
    Have you tried `SetVeiw` or setting the `Center` and `ZoomLevel`? – Reza Aghaei Dec 30 '20 at 21:00

2 Answers2

1

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:

enter image description here

More information

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
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:

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: enter image description here

More information:

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

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • Winforms doesn't seem to have a Loaded event for the form (just Load). Oh, I see, the loaded event is for the map element... – B. Clay Shannon-B. Crow Raven Dec 30 '20 at 23:06
  • `Load` event belongs to `Form`. I have handled it and then inside the load event handler, I've registered an event handler for the [`Loaded`](https://learn.microsoft.com/en-us/dotnet/api/system.windows.frameworkelement.loaded?redirectedfrom=MSDN&view=net-5.0&WT.mc_id=DT-MVP-5003235) event of the `Map` control. – Reza Aghaei Dec 30 '20 at 23:08
  • Please see my Update 2. – B. Clay Shannon-B. Crow Raven Dec 31 '20 at 10:52
  • Did you followed the instruction in the linked post (your previous question)? When you drop an ElementHost on form and then assign it's UserControl, then an instance of the UserControl will be added to your form and you can manipulate it. No magic here, you probably are missing something, for example I see you have used `this.UserControl1.myMap.AnimationLevel = AnimationLevel.Full;` which is wrong, should be **u**serControl11. – Reza Aghaei Dec 31 '20 at 11:00
  • Why would it be UserControl11, when my UserControl is named UserControl1? – B. Clay Shannon-B. Crow Raven Jan 01 '21 at 14:07
  • Well, whatever it is, it should be name of the instance of the UserControl, usually when control name is Abcd, the instance name is abcd1. I'll create a GitHub repo if it helps. – Reza Aghaei Jan 01 '21 at 14:11
  • 1
    I created a GitHub repository; you can find the repository address and download link in the answer, hope it helps – Reza Aghaei Jan 01 '21 at 14:37