Problem setting a map marker

Former Member
Former Member
In my widget I am able to initialize MapView and present a map with the given coordinates, but I receive a Symbol Not Found Error when adding a map marker. If I instead show a polyline, everything works fine.

So this works:
var polyline = new WatchUi.MapPolyline();
polyline.setColor(Toybox.Graphics.COLOR_RED);
polyline.setWidth(2);
polyline.addLocation(map_center);
polyline.addLocation(top_left);
MapView.setPolyline(polyline);


This doesn't:
var defaultMarker = new WatchUi.MapMarker(map_center);
defaultMarker.setIcon(WatchUi.MAP_MARKER_ICON_PIN, 0, 0);
MapView.setMapMarker(defaultMarker);


Any ideas why? I call both of the above from within the initialize function for my extended MapView class.
  • Nothing jumps out at me, but do you have a "using Toybox.WatchUi;"?

    I've only used this with an array of markers (see the MapSample) but I don't think that's involved.

    Which specific line are you getting the error on?
  • Former Member
    Former Member over 6 years ago
    Yeah, very strange. I do include Toybox.WatchUi. Now this is only being tested in the simulator, but I would think showing the default map pin would be simple. Here's the line of code generating the error:

    MapView.setMapMarker(defaultMarker);

    Seems like it's saying that defaultMarker is invalid, but I'm just using the same code from the sample.
  • Former Member
    Former Member over 6 years ago
    Here's the entire code...

    Using Toybox.WatchUi;
    Using Toybox.Position;

    class MyMapView extends MapView {

    // Initialize the MapView
    function initialize() {
    MapView.initialize();

    var diff = 0.004;
    var lat = 38.85695;
    var lon = -94.80051;

    // Set the bounds for the Map Visible Area
    var top_left = new Position.Location({:latitude => lat+diff, :longitude => lon-diff, :format => :degrees});
    var bottom_right = new Position.Location({:latitude => lat-diff, :longitude => lon+diff, :format => :degrees});
    MapView.setMapVisibleArea(top_left, bottom_right);

    // Set the area in which to display the selected map area
    MapView.setScreenVisibleArea(0, 0, 240, 240);

    // Set the map mode
    MapView.setMapMode(WatchUi.MAP_MODE_PREVIEW);

    // Map marker
    var map_center = new Position.Location({:latitude => lat, :longitude => lon, :format => :degrees});
    var defaultMarker = new WatchUi.MapMarker(map_center);
    defaultMarker.setIcon(WatchUi.MAP_MARKER_ICON_PIN, 0, 0);

    // Set the Map Marker for the view
    MapView.setMapMarker(defaultMarker);
    }
    }
  • I have similar code in a few apps, but the difference is I pass an array to setMapMarker as is done in the mapSample. Even if there is only one marker.

    Oh, and you may want to try changing the extends MapView to "extends WatchUi.MapView"
  • Her's the code from my U R Here widget that does what it looks like you are trying to do.
    var map_markers = [];

    var marker = new Ui.MapMarker(tView.location);
    marker.setIcon(Ui.MAP_MARKER_ICON_PIN, 0, 0);
    map_markers.add(marker);

    // add map markers to the Map
    MapView.setMapMarker(map_markers);

  • Former Member
    Former Member over 6 years ago
    You called it! I had to pass an array of a single marker. Crazy that in the docs it says an object or array. Whatever, it works!

    Thanks again!
  • Do a bug report in the bug report forum.

    And as a tip, check things on on a watch as soon as you can, and the watch's map setting such as map orientation can impact a few things.
  • Former Member
    Former Member over 6 years ago
    Just filed the bug on the forum. I also checked the updated code on my watch and everything worked fine. Thanks for your help.