0

I m using the Carto Map Mobile SDK (UWP) to implement the customized carto map on a UWP app.

However, I do not know how can I use the MapEventListener and VectorElementEventListener on my app (using UWP C# language).

I added some markers by different lat/long on the map, and I want to click each of them to generate a popup infowindow (balloon) with different contents on that infowindow.

Here is my VectorElementsEventListener class:

public class VectorElementsEventListener : VectorElementEventListener
{
    public const string NullString = "null";

    public const string RouteSearchTitle = "route_search_title";
    public const string RouteSearchDescription = "route_search_description";

    LocalVectorDataSource source;

    BalloonPopup previous;

    public VectorElementsEventListener(LocalVectorDataSource dataSource)
    {
        source = dataSource;
    }

    public override bool OnVectorElementClicked(VectorElementClickInfo clickInfo)
    {
        if (previous != null)
        {
            source.Remove(previous);
        }

        VectorElement element = clickInfo.VectorElement;

        BalloonPopupStyleBuilder builder = new BalloonPopupStyleBuilder();
        builder.LeftMargins = new BalloonPopupMargins(0, 0, 0, 0);
        builder.RightMargins = new BalloonPopupMargins(6, 3, 6, 3);
        builder.PlacementPriority = 10;
        builder.CornerRadius = 5;
        builder.TitleFontSize = 12;
        builder.DescriptionFontSize = 10;

        var navy = new Color(22, 41, 69, 255);
        builder.TitleColor = navy;
        builder.DescriptionColor = navy;

        var animationBuilder = new AnimationStyleBuilder();
        animationBuilder.RelativeSpeed = 2.0f;
        animationBuilder.SizeAnimationType = AnimationType.AnimationTypeSpring;
        builder.AnimationStyle = animationBuilder.BuildStyle();

        BalloonPopupStyle style = builder.BuildStyle();

        string title = element.GetMetaDataElement("ClickText").String;
        string description = "";

        if (!element.GetMetaDataElement(RouteSearchTitle).String.Equals(NullString))
        {
            // Route search has a special click text
            title = element.GetMetaDataElement(RouteSearchTitle).String;
            description = element.GetMetaDataElement(RouteSearchDescription).String;
        }
        else
        {
            for (int i = 0; i < element.MetaData.Count; i++)
            {
                string key = element.MetaData.Keys.ToList()[i];
                description += key + " = " + element.GetMetaDataElement(key) + "; ";
            }
        }


        BalloonPopup popup;

        if (element is BalloonPopup)
        {
            Billboard billboard = (Billboard)element;
            popup = new BalloonPopup(billboard, style, title, description);
        }
        else
        {
            MapPos position = clickInfo.ClickPos;
            popup = new BalloonPopup(position, style, title, description);
        }

        source.Add(popup);
        previous = popup;

        return true;
    }
}

Then I initialize a VectorLayer and initialize the VectorElementEventlistener on that layer

            // Initialize a vector layer with the previous data source
            VectorLayer layer = new VectorLayer(datasource);

            // Add layer to map
            map.Layers.Add(layer);

            layer.VectorElementEventListener = new VectorElementEventListener();

How can I initialize a 'VectorElementClickInfo' variable then pass it to the onvectorElementclicked function?

  • Normally you do not call it, this is done 'automatically' when user clicks the element. Or you want to 'simulate' the click programmatically? – JaakL Sep 11 '18 at 5:23
  • Hi JaakL, thank you for your help. However, it seems that it does not work for me. When I click the added markers, no popup balloon displayed on the map. – Jack Lau Sep 11 '18 at 5:52

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.