如何保存并附加到地图图层

如何保存并附加到地图图层

本文介绍了如何保存并附加到地图图层?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种方法可以将图钉绘制到工作正常的地图上,但是我需要在第一层附加第二个图钉.我猜这将涉及保存第一层,以便以后可以对其进行操作.没有人知道我如何保存该图层吗?

I have a method to draw pushpins to a map which works fine, but I need to append a second pushpin to the first layer. I'm guessing this will involve saving the first layer so that it can be manipulated later. Does anyone have any idea how I would go about this as I'm not quite sure how to save a layer?

目前,在应用程序中两次调用了DrawPushPin方法,因此第一次创建新图层,第二次创建相同图层,但这并不理想,因为我需要追加到第一层而不创建新图层.

At present the DrawPushPin method is called twice in the app, so the first time a new layer is created and same for the second time but this is not ideal as I need to append to the first layer not create a new one.

该方法称为DrawPushPinCurrent(MyGeoPosition, pushPinName);,下面是draw方法.

The method is called like this DrawPushPinCurrent(MyGeoPosition, pushPinName); and below is the draw method.

private void DrawPushPin(GeoCoordinate MyGeoPosition,string pushPinName)
        {
            MapLayer layer1 = new MapLayer();
            Pushpin pushpin1 = new Pushpin();

            pushpin1.GeoCoordinate = MyGeoPosition;
            pushpin1.Content = pushPinName;


            MapOverlay overlay1 = new MapOverlay();
            overlay1.Content = pushpin1;
            overlay1.GeoCoordinate = MyGeoPosition;
            layer1.Add(overlay1);

            MyMap.Layers.Add(layer1);
            MyMap.Center = MyGeoPosition;
            MyMap.ZoomLevel = 15;


        }

推荐答案

我要做的是将MapLayer移到方法之外,并使它们成为全局类(在类级别).然后,我将创建另一个这样的方法:

What I would do is move the MapLayer outside of the method and make them global (at the class level). Then I would create another method like this:

MapLayer layer1;

public MyClass()
{
   layer1 = new MapLayer;
}

private void AppendPushpin(GeoCoordinate MyGeoPosition, string pushpinName)
{
   Pushpin pushpin1 = new Pushpin();

   pushpin1.GeoCoordinate = MyGeoPosition;
   pushpin1.Content = pushPinName;

   MapOverlay overlay1 = new MapOverlay();
   overlay1.Content = pushpin1;
   overlay1.GeoCoordinate = MyGeoPosition;
   layer1.Add(overlay1);
}

*编辑

在您的情况下,您需要在应用程序范围内存储该变量,那么您将希望持久执行该MapLayer对象,就像这样:

In your case, where you need to store that variable application-wide, you would want to persist that MapLayer object doing something like this:

Windows.Storage.ApplicationDataContainer localSettings =   Windows.Storage.ApplicationData.Current.LocalSettings;
localSettings.Values["MapLayer"] = layer1;

由您决定需要在哪里设置和获取此全局变量.

Its up to you on where you need to set and get this global variable.

这篇关于如何保存并附加到地图图层?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 17:52