添加固定的MapLayer一次

时间:2014-03-10 08:11:29

标签: windows-phone-8 map

在我的应用程序中,我有一张地图,我做的第一件事(启动时)是添加一个自定义MapLayer(我用许多MapOverlays / pushpins填充)。

当我浏览应用程序的另一个页面然后返回到地图页面时,所有内容(即我在地图上绘制的MapLayer)都消失了。

每次用户导航到地图页面时都需要花费一些时间来重新添加它,所以我希望它能够被修复,绘制/添加一次。

有什么建议吗?

编辑,添加了代码[我删除了一些细节,结构保持不变]:

private async void drawStations()
        {

            SQLiteAsyncConnection conn = new SQLiteAsyncConnection("stasy.sqlite");

            List<line1_stations> lines = await conn.QueryAsync<line1_stations>("select *...");

            Microsoft.Phone.Maps.Controls.MapLayer layer = new Microsoft.Phone.Maps.Controls.MapLayer();

            Pushpin p;

            foreach (line1_stations a in lines)
            {
                double sLat = Convert.ToDouble(a.lat);
                double sLon = Convert.ToDouble(a.lon);

                p = new Pushpin();
                p.Location = new GeoCoordinate(sLat, sLon);
                p.Tap += img_Tap;
                p.Content = "...";
                p.Foreground = new SolidColorBrush(Colors.Transparent);
                p.Width = 30;
                p.Height = 30;

                MapOverlay overlay1 = new MapOverlay();
                overlay1.Content = p;
                overlay1.GeoCoordinate = new GeoCoordinate(sLat, sLon);
                overlay1.PositionOrigin = new Point(0.0, 1.0);
                layer.Add(overlay1);
            }

            myMap.Layers.Add(layer);
        }

2 个答案:

答案 0 :(得分:0)

您可以尝试在App.xaml.cs中将MapLayer创建为公共属性,并仅在您的应用首次运行时设置它。

然后使用地图在页面内创建应用实例,并将MapLayer添加到OnNavigatedTo事件内的地图图层。

答案 1 :(得分:0)

在应用级别定义一个标志。这是样本

// In App.xaml.cs
public static bool IsLaunched = false;
private void Application_Launching(object sender, LaunchingEventArgs e)
        {
           IsLaunched =true;
        }

//In your map screen

protected override void OnNavigatedTo(NavigationEventArgs e)
        {
         if(App.IsLaunched)
           {
             drawStations();
             //Set App flage to false
             App.IsLaunched = false;
           }
        }

private async void drawStations()
        {

            SQLiteAsyncConnection conn = new SQLiteAsyncConnection("stasy.sqlite");

            List<line1_stations> lines = await conn.QueryAsync<line1_stations>("select *...");

            Microsoft.Phone.Maps.Controls.MapLayer layer = new Microsoft.Phone.Maps.Controls.MapLayer();

            Pushpin p;

            foreach (line1_stations a in lines)
            {
                double sLat = Convert.ToDouble(a.lat);
                double sLon = Convert.ToDouble(a.lon);

                p = new Pushpin();
                p.Location = new GeoCoordinate(sLat, sLon);
                p.Tap += img_Tap;
                p.Content = "...";
                p.Foreground = new SolidColorBrush(Colors.Transparent);
                p.Width = 30;
                p.Height = 30;

                MapOverlay overlay1 = new MapOverlay();
                overlay1.Content = p;
                overlay1.GeoCoordinate = new GeoCoordinate(sLat, sLon);
                overlay1.PositionOrigin = new Point(0.0, 1.0);
                layer.Add(overlay1);
            }

            myMap.Layers.Add(layer);
        }