如何通过Carto Map Moblie SDK.UWP添加自定义的Carto Map标记?

时间:2018-09-09 08:57:08

标签: c# uwp carto-mobile

我正在实现通用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 document。但是,当我通过Nuget安装官方的SDK.UWP时,库中的文档中没有提到任何相关功能。

有人可以帮我解决这个问题吗?否则,对我而言进一步创建此UWP应用程序毫无意义。非常感谢。

1 个答案:

答案 0 :(得分:0)

好的,我刚刚解决了这个问题,Carto Map支持团队也回答了我。官方技术文档未及时更新,因此会误导首次接触地图的新人(尤其是UWP)。

解决方法是:

            /// 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文件夹。