Jack Lau
Jack Lau

Reputation: 41

How can I add a customised Carto Map Marker via Carto Map Moblie SDK.UWP?

I'm implementing a Universal Windows Platform (UWP) app, and I m using the Carto Map Mobile SDK (UWP). However, I don't know how to add a .png image as a Map Marker programmatically. Here is my code:

                /// Preparation - create layer and datasource

            // projection will be needed later
            Projection projection = map.Options.BaseProjection;

            // Initialize an local data source - a bucket for your objects created in code
            LocalVectorDataSource datasource = new LocalVectorDataSource(projection);

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

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



            /// Now we real adding objects

            // Create marker style
            MarkerStyleBuilder builder = new MarkerStyleBuilder();
            builder.Size = 20;
            BinaryData iconBytes = AssetUtils.LoadAsset("Z:/FolderName/ProjectName/Assets/markers_mdpi/mapmarker.png");

            byte[] bytearray = iconBytes.GetData();
            int size = Marshal.SizeOf(bytearray[0]) * bytearray.Length;

            IntPtr pnt = Marshal.AllocHGlobal(size);

            builder.Bitmap = new Bitmap(pnt, true);


            MarkerStyle style = null;


            style = builder.BuildStyle();

            // Create a marker with the style we defined previously and add it to the source
            Marker marker = new Marker(position, style);
            datasource.Add(marker);

The Carto Map official technical document didn't help at all, and here is the screenshotCarto Mobile SDK document. However, when I installed the official SDK.UWP via the Nuget, there aren't any relevant functions that mentioned in the document in the library.

Can anyone help me solve this problem? Otherwise it is meaningless for me to create this UWP app further. Many thanks.

Upvotes: 1

Views: 107

Answers (1)

Jack Lau
Jack Lau

Reputation: 41

Okay, I just solved this problem, and the Carto Map Support team replied me as well. The official technical document is not updated in time, so it misleads new people who first contact with the carto map (especially the UWP one).

The solution is:

            /// Preparation - create layer and datasource

            // projection will be needed later
            Projection projection = map.Options.BaseProjection;

            // Initialize an local data source - a bucket for your objects created in code
            LocalVectorDataSource datasource = new LocalVectorDataSource(projection);

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

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

            /// Now we real adding objects

            // Create marker style
            MarkerStyleBuilder builder = new MarkerStyleBuilder();
            builder.Size = 30;
            //here we generate a filePath string then pass it into AssetUtils.LoadAsset 
            string filePath = System.IO.Path.Combine("SubfolderName", "imagefileName.png"); 

            var data = AssetUtils.LoadAsset("SubfolderName\\imagefileName.png");
            var bitmap = Bitmap.CreateFromCompressed(data);

            if (bitmap != null)
            {
                builder.Bitmap = bitmap;
                bitmap.Dispose();
            }

            MarkerStyle style = builder.BuildStyle();

            // Create a marker with the style we defined previously and add it to the source
            Marker marker = new Marker(position, style);
            datasource.Add(marker);

Please make sure all the files/sources come from the Assets folder.

Upvotes: 0

Related Questions