簡體   English   中英

如何簡化地圖中的引腳

[英]How can i simplify pins in maps

所以我在我的 VS Xamarin 項目上實現了谷歌地圖。 我知道有一種方法可以簡化我的代碼,但我不知道我還能做些什么。 我的 map 上有一堆標記,每次創建一個我都會創建一個整體,所以我想簡化這個過程,如果可能的話,從 excel 文件中提取信息。

我的代碼:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Maps;

namespace ------
{
    
    [DesignTimeVisible(false)]
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
            Position position = new Position(36.9628066, -122.0194722);
            MapSpan mapSpan = new MapSpan(position, 0.01, 0.01);
            Map map = new Map(mapSpan)
            {
               MapType = MapType.Hybrid
            
            };
            Content = map;
            Pin pin = new Pin
            {
                Label = "Santa HEY MAN",
                Address = "The city with a boardwalk",
                Type = PinType.Place,
                Position = new Position(36.9628066, -122.0194722)
            };
            map.Pins.Add(pin);

            Pin pin2 = new Pin
            {
                Label = "USA",
                Address = "2020",
                Type = PinType.Place,
                Position = new Position(36.9628066, -122.0194722)
            };
            map.Pins.Add(pin2);


        }

    }
}

這里我只顯示 2 個引腳,但實際上,我有 30 個引腳。 我怎樣才能讓這更簡單? 非常感謝: :)

請注意,這都是偽代碼,在語法上不正確 json/C#

首先,使用您的數據創建一個 json 文件並將其包含在您的項目中

[ 
  { Label = "USA" Address = "2020", Lat = "36.9628066" Long = "-122.0194722" },
  ... 
  { Label = ""Santa HEY MAN", Address = "The city with a boardwalk", Lat = "36.9628066" Long = "-122.0194722} }
]

然后,在您的代碼中

// read the file
var json = File.ReadAllText(path);

// deserialize using NewtonSoft
var places = DeserializeObject<List<Place>>(json);

// then create pins
foreach (var place in places)
{
   var pin = new Pin
        {
            Label = place.Label,
            Address = place.Address,
            Type = PinType.Place,
            Position = new Position(place.Lat, place.Long)
        };
   map.Pins.Add(pin);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM