How to add a pin based on latitude and longitude in Mapsui openstreet map?

853 views Asked by At

I have an openstreet map in which i am trying to add some pins based on Latitudes and Longitudes in a WPF application, I could not find any relevant documentation in the Mapsui page to add pins.

I tried to create an object of the MPoint type and put the X and Y coordinates into it but I wasn't successful, Is there any other function which I am missing or any conversion which i need to do first? This is my code behind

using Mapsui;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Windows.Devices.Geolocation;
using Windows.Services.Maps;
using Windows.Foundation;

namespace Newmap
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MPoint beginPoint=new MPoint();
        public MPoint endPoint=new MPoint();
        public MainWindow()
        {
            InitializeComponent();
            var mapControl = new Mapsui.UI.Wpf.MapControl();

            mapControl.Map?.Layers.Add(Mapsui.Tiling.OpenStreetMap.CreateTileLayer());

            Content = mapControl;
            beginPoint.X = 20.593684;
            beginPoint.Y = 78.96288;
            endPoint.X = 34.544261;
            endPoint.Y = -91.969028;
            //BasicGeoposition snPosition = new BasicGeoposition { Latitude = 47.620, Longitude = -122.349 };
            //Geopoint snPoint = new Geopoint(snPosition);


            //mapControl.ToDeviceIndependentUnits(beginPoint);
            //mapControl.ToDeviceIndependentUnits(endPoint);

            mapControl.ZoomToBox(beginPoint,endPoint);
        }

        

    }
}

And this is my XAML code.

<Window x:Class="Newmap.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Newmap"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        
    </Grid>
</Window>

Also do I have to add images to act as pins?

1

There are 1 answers

4
pauldendulk On

A conversion is needed from the latitude/longitude coordinate system (WGS84 or EPSG:4326) to the coordinate system used in openstreetmap (Spherical Mercator or EPSG:3857). In Mapsui 4.0.0-beta.6 there are two options:

  1. Use the ProjectingProvider. See this sample.

  2. Convert the coordinates themselves use SphericalMercator.FromLonLat, used in this sample.

Also do I have to add images to act as pins?

Yes, see how this could be done in PointProjectionSample or in WriteToLayerSample.