none
Bing Maps Control Cannot add Map Icon after building an app package

    Question

  • I am trying to add a MapIcon to my Bing maps control. When I am running the application in my debugging environment on visual studio I don't have any issues. However once I build my app package and run it, adding a map Icon crashes the application and throws this an exception that reads:

     "Unable to cast object of type Windows.UI.Xaml.Controls.Maps.MapIcon to type Windows.UI.Xaml.Controls.Maps.IMapElement4"

    Here is the simple code that instantiates the MapIcon. It is important to know that I do not have any issues adding these map Icons in debug mode. The problem only exists once the app package has been built and run. I was able to pinpoint the exception using a try/catch to display the exception while the released app is running. If anyone can help me to get rid of this exception, or have any advice for a work around, it would be much appreciated.

    MapIcon messageicon2_2 = new MapIcon
                            {
                                Location = message_position,
                                NormalizedAnchorPoint = new Point(0.5, 1.0),
                                ZIndex = 0,
                                Title = "msg " + count + "(J2.2)",
                                IsEnabled = true,                            
                                CollisionBehaviorDesired = MapElementCollisionBehavior.RemainVisible
                            };
    
                            MyWaypoints.Add(messageicon2_2);
                            JmessageLayer.MapElements = MyWaypoints;

    [EDIT To Clarify]: I know that this bug is coming specifically from the instantiation of the Mapicon. This code works perfectly fine when it is released and run on a Windows 10 17134 machine.... The machine that I need it to run is Windows 10 16299. This machine can not be updated to a newer version of windows. I am in need of a way to display these map icons on this older release of windows.


    • Edited by kdun0508 Tuesday, August 14, 2018 6:26 PM
    Tuesday, August 14, 2018 12:22 PM

All replies

  • Hi kdun0508,

    The call to waypoints is likely your issue.  messageicon2_2 is a MapIcon ( https://docs.microsoft.com/en-us/uwp/api/Windows.UI.Xaml.Controls.Maps.MapIcon ) while waypoint APIs tend to want a location/position co-ordinate which is likely the Map Element it needs.  

    Try  MyWaypoints.Add(message_position); rather than passing the Map Icon

    Sincerely,

    IoTGirl

    Tuesday, August 14, 2018 5:04 PM
    Owner
  • What is the minimum and target version of the Windows platform that you are using for this project? (shown in the project properties page)?

    It sounds like you're running into an issue where the project is being built for a newer Windows SDK version and then run on an OS version that is older.

    You may also want to consider using the newer Layer API for adding MapIcons instead of setting the MapElements collection directly - it's more full-featured and easier to use.

    Tuesday, August 14, 2018 5:25 PM
    Moderator
  • @kdun0508 Hope you found your problem.

    I have the same problem here, in that 17134 and greater works that should work in the 16299 version.

    In my case, the following code causes the Invalid Cast Exception, and by rights, it doesn't seem as if it should.

    var theirIcon = new MapIcon();
    theirIcon.IsEnabled = true;   // << Invalid Cast Exception
               // Unable to cast object of type 'Windows.UI.Xaml.Controls.Maps.MapIcon' to type 'Windows.UI.Xaml.Controls.Maps.IMapElement4'.
    

    @Duncan Lawler according to the docs, I'm using the correct versions.  My target and minimum version are 17134 and 16299 respectively.

    According to what I've been reading the IsEnabled property has been there for a while, and hasn't been updated for 16299, right? 

    -thanks
    -e


    -- me --

    Monday, November 26, 2018 4:38 AM
  • Hi Chief Scientist,

    I believe "IsEnabled" is a property not a method so you can read it but not set it.  You should probably use get; and set; methods to change characteristics of your instance.

    I don't have a sample but I suspect it might be something like 

    public static void SetIsEnabled(DependencyObject DepObject, bool value)
        {
            DepObject.SetValue(IsEnabledProperty, value);
        }
    
    public static bool GetIsEnabled(DependencyObject DepObject)
        {
            return (bool)DepObject.GetValue(IsEnabledProperty);
        }

    Not sure why you are interested in changing the enabled value.  Have you tried steps like these http://blog.devcrux.com/soludayo/2016/12/adding-mapicon-pushpin-to-your-map-in-uwp-part-1/?

    Sincerely,

    IoTGirl



    Monday, November 26, 2018 8:09 AM
    Owner
  • The IsEnabled property was added for OS version 17134, so it's expected that it won't work if you run on earlier OS versions (like 16299).

    Monday, November 26, 2018 8:46 PM
    Moderator