0

I am working on this desktop program as a school project in C# and working with WPF, currently I am trying to get the users location using Bing maps and the LocationApiLibrary. Had a few problems with some missing references but I managed to sort it out. Now i am getting this error:

Error CS1503 Argument 2: cannot convert from 'LocationApiLib.Location' >to 'Microsoft.Maps.MapControl.WPF.Location'

I have searched, even here but without finding something that can solve my problem. Basically I want to be able to read the coordinates of the client and show them in a textbox, and save it together with the some other information in the database.

using LocationApiLib;
using Microsoft.Maps.MapControl.WPF;
using System;
using System.Collections.Generic;
using System.ComponentModel;
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.Shapes;
using System.Windows.Threading;

namespace myBMark_Dekstop_Edition
{
    /// <summary>
    /// Interaction logic for Window2.xaml
    /// </summary>
    public partial class Window2 : Window
    {
        private  myMap;

        public Window2()
        {
            InitializeComponent();
            ImageBrush myBrush = new ImageBrush();
            myBrush.ImageSource =
                new BitmapImage(new Uri("C:/Users/AbdulB.M/Documents/Visual Studio 2015/Projects/myBMark Dekstop Edition/1.PNG", UriKind.Absolute));
            this.Background = myBrush;




        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            LocationApiLib.Location myLoc = new LocationApiLib.Location();
            myMap.SetView(myLoc, Convert.ToDouble(14), Convert.ToDouble(0));

            Pushpin myPin = new Pushpin();
            MapLayer.SetPosition(myPin, myLoc);
            myMap.Children.Add(myPin);

            System.Windows.Controls.Label label = new   System.Windows.Controls.Label();
            label.Content = "Here I am!";
            label.Foreground = new SolidColorBrush(Colors.DarkBlue);
            label.Foreground = new SolidColorBrush(Colors.WhiteSmoke);
            label.FontSize = 30;
            MapLayer.SetPosition(label, myLoc);
            myMap.Children.Add(label);
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {

        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {

        }
    }
    }

Xaml part is here:

<Window x:Class="myBMark_Dekstop_Edition.Window2"
    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:m="clr-namespace:Microsoft.Maps.MapControl.WPF;assembly=Microsoft.Maps.MapControl.WPF"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:myBMark_Dekstop_Edition"
    mc:Ignorable="d"
    Title="Home" Height="634.695" Width="695.732" WindowStartupLocation="CenterScreen">
<Grid>
    <TextBox x:Name="textBox" HorizontalAlignment="Left" Height="23" Margin="72,160,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
    <TextBox x:Name="textBox1" HorizontalAlignment="Left" Height="23" Margin="72,207,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
    <TextBox x:Name="textBox2" HorizontalAlignment="Left" Height="23" Margin="72,271,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
    <TextBox x:Name="textBox3" HorizontalAlignment="Left" Height="23" Margin="72,320,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
    <TextBox x:Name="textBox4" HorizontalAlignment="Left" Height="23" Margin="72,377,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
    <TextBox x:Name="textBox5" HorizontalAlignment="Left" Height="23" Margin="72,117,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>

    <m:Map Name="myMap"
       Center="-1.968404, 30.036240"
       ZoomLevel="10"
       CredentialsProvider="..."
       Mode="Road" Margin="245,29,29.6,347.8" ></m:Map>

    <StackPanel HorizontalAlignment="Left"/>
    <StackPanel HorizontalAlignment="Right" VerticalAlignment="Center">
        <Button Content="+" Click="Button_Click_1" Width="105" Height="40"/>
        <Button Content="-" Click="Button_Click_2" Width="105" Height="40"/>
    </StackPanel>
    <Button Content="Get my location" Click="Button_Click" Margin="396,275,188.6,302.8"/>

</Grid>

Clemens
  • 123,504
  • 12
  • 155
  • 268
abmy
  • 39
  • 1
  • 8
  • I am unsure about the type of myMap. But its declared in the xaml, and having it written as private myMap in the code caused the error "windows already contains defination for myMap", but removing it solved that issue. So still stuck with the converting problem. – abmy May 04 '16 at 08:56
  • The two types are very different, so of course you can't assign one to the other. You need to create a `Microsoft.Maps.MapControl.WPF.Location` instance with data taken from the `LocationApiLib.Location` instance. – Corey May 04 '16 at 09:05
  • Should this be outside the method where the "private myMap;" is? – abmy May 04 '16 at 09:25

3 Answers3

0

I suppose that the error is about using myLoc (LocationApiLib.Location) in the SetView function (Microsoft.Maps.MapControl.WPF.Location).

Try extracting coordinates from myLoc and create a new Microsoft.Maps.MapControl.WPF.Location in order to use it in SetView.

EDITED:

This code compiles in your example:

    Location myLoc = new Location();

    myMap.SetView(myLoc, Convert.ToDouble(14), Convert.ToDouble(0));

Just set longitude and latitude to myLoc object before calling SetView. For sure you could get that info from the LocationApiLib object.

    Location myLoc = new Location();
    myLoc.Latitude = value1;
    myLoc.Longitude = value2;
    myLoc.Altitude = value3;
    myMap.SetView(myLoc, Convert.ToDouble(14), Convert.ToDouble(0));
Tuto
  • 36
  • 5
  • I have posted the xaml part. Cant seem to find the error. – abmy May 04 '16 at 09:23
  • Well I am a student so I am trying to understand it. But I have tried declaring the Microsoft.Maps.MapControl.WPF.Location instance but it gives an error saying its a namespace and I cannot use it that way. – abmy May 04 '16 at 15:45
0

Your problem I think from looking at your code is that the two types called Location that you are using (one from namespace LocationApiLib and the other from WPF) do not derive from one another and therefore cannot be cast. The 2nd parameter in MapLayer.SetPosition is the WPF Location type, into which you are passing an object of type LocationApiLib.Location.

One option you could look into to resolve this is to new up a WPF Location object, constructing it and/or setting it up with required long/lat values from the LocationApiLib Location object. There may be some easier ways to create a WPF Location from a LocationApiLib Location but I'd have to Google that myself...

muszeo
  • 2,312
  • 9
  • 13
0

I fixed the error by replacing

LocationApiLib.Location myLoc = new LocationApiLib.Location();

With this and no errors, but at the moment I am having trouble with a wrong location (in the Athletic Ocean!). That I will ask in another question tough. Thanks for your replies.

Microsoft.Maps.MapControl.WPF.Location myLoc = new Microsoft.Maps.MapControl.WPF.Location();
abmy
  • 39
  • 1
  • 8