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

2

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>
Hirsutism answered 28/12, 2020 at 16:53 Comment(9)
Does an analogous class work for you? While it's pretty straightforward to create a class having similar 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)Haze
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).Hirsutism
Please see my update for why I need the GeoPoint and BasicGeoPosition objectsHirsutism
WPF Map doesn't have TrySetSceneAsync method. You can take a look at its methods here. TrySetSceneAsync belongs to Windows Community Toolkit Map Control.Haze
Is using Windows Community ToolKit Map Control an option for you?Haze
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?Hirsutism
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.Haze
Thanks; my issue is with setting the initial "scene" to the location of my choice now.Hirsutism
Have you tried SetVeiw or setting the Center and ZoomLevel?Haze
H
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:

Haze answered 30/12, 2020 at 21:7 Comment(7)
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...Hirsutism
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 event of the Map control.Haze
Please see my Update 2.Hirsutism
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 userControl11.Haze
Why would it be UserControl11, when my UserControl is named UserControl1?Hirsutism
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.Haze
I created a GitHub repository; you can find the repository address and download link in the answer, hope it helps 😊Haze
H
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

Haze answered 29/12, 2020 at 17:46 Comment(0)
H
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:

Haze answered 30/12, 2020 at 21:7 Comment(7)
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...Hirsutism
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 event of the Map control.Haze
Please see my Update 2.Hirsutism
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 userControl11.Haze
Why would it be UserControl11, when my UserControl is named UserControl1?Hirsutism
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.Haze
I created a GitHub repository; you can find the repository address and download link in the answer, hope it helps 😊Haze

© 2022 - 2024 — McMap. All rights reserved.