1

我正在实现一个通用 Windows 平台 (UWP) 应用程序,并且正在使用 Carto Map Mobile SDK (UWP)。但是,我不知道如何以编程方式将 .png 图像添加为地图标记。这是我的代码:

                /// 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);

Carto Map 官方技术文档一点帮助都没有,这里是Carto Mobile SDK 文档的截图。但是,当我通过 Nuget 安装官方 SDK.UWP 时,库中没有文档中提到的任何相关功能。

谁能帮我解决这个问题?否则我进一步创建这个 UWP 应用程序是没有意义的。非常感谢。

4

1 回答 1

0

好的,我刚刚解决了这个问题,Carto Map Support 团队也回复了我。官方技术文档更新不及时,误导了初次接触carto map(尤其是UWP)的新人。

解决办法是:</p>

            /// 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);

请确保所有文件/来源都来自 Assets 文件夹。

于 2018-09-10T22:51:42.233 回答