如何简化地图上的图钉?

huangapple go评论112阅读模式
英文:

How can i simplify pins in maps

问题

所以我已经在我的VS Xamarin项目上实现了Google Maps。
我知道有一种方法可以简化我的代码,但我不知道如何更进一步。
我的地图上有很多标记,每次我创建一个标记,我都会创建一个完整的标记,所以我想简化这个过程,如果可能的话,从Excel文件中提取信息。

我的代码:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using Xamarin.Forms;
  8. using Xamarin.Forms.Maps;
  9. namespace ------
  10. {
  11. [DesignTimeVisible(false)]
  12. public partial class MainPage : ContentPage
  13. {
  14. public MainPage()
  15. {
  16. InitializeComponent();
  17. Position position = new Position(36.9628066, -122.0194722);
  18. MapSpan mapSpan = new MapSpan(position, 0.01, 0.01);
  19. Map map = new Map(mapSpan)
  20. {
  21. MapType = MapType.Hybrid
  22. };
  23. Content = map;
  24. Pin pin = new Pin
  25. {
  26. Label = "Santa HEY MAN",
  27. Address = "The city with a boardwalk",
  28. Type = PinType.Place,
  29. Position = new Position(36.9628066, -122.0194722)
  30. };
  31. map.Pins.Add(pin);
  32. Pin pin2 = new Pin
  33. {
  34. Label = "USA",
  35. Address = "2020",
  36. Type = PinType.Place,
  37. Position = new Position(36.9628066, -122.0194722)
  38. };
  39. map.Pins.Add(pin2);
  40. }
  41. }
  42. }

在这里,我只展示了2个标记,但实际上,我有30个标记。
我该如何简化这个过程?
非常感谢! 如何简化地图上的图钉?

英文:

So I have implemented Google Maps on my VS Xamarin project.
I know there is a way to simplify my code but I don't know how more could I do it.
I have a bunch of markers on my map and each time I create one I create at whole so I want to simplify this process and if possible extract the information from an excel file.

My code:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using Xamarin.Forms;
  8. using Xamarin.Forms.Maps;
  9. namespace ------
  10. {
  11. [DesignTimeVisible(false)]
  12. public partial class MainPage : ContentPage
  13. {
  14. public MainPage()
  15. {
  16. InitializeComponent();
  17. Position position = new Position(36.9628066, -122.0194722);
  18. MapSpan mapSpan = new MapSpan(position, 0.01, 0.01);
  19. Map map = new Map(mapSpan)
  20. {
  21. MapType = MapType.Hybrid
  22. };
  23. Content = map;
  24. Pin pin = new Pin
  25. {
  26. Label = "Santa HEY MAN",
  27. Address = "The city with a boardwalk",
  28. Type = PinType.Place,
  29. Position = new Position(36.9628066, -122.0194722)
  30. };
  31. map.Pins.Add(pin);
  32. Pin pin2 = new Pin
  33. {
  34. Label = "USA",
  35. Address = "2020",
  36. Type = PinType.Place,
  37. Position = new Position(36.9628066, -122.0194722)
  38. };
  39. map.Pins.Add(pin2);
  40. }
  41. }
  42. }

Here I only show 2 pins but in reality, I have 30 pins.
How could I make this simpler?
Thanks a lot!
如何简化地图上的图钉?

答案1

得分: 0

以下是翻译好的部分:

首先,创建一个包含您的数据并将其包含在项目中的 JSON 文件。

  1. [
  2. { "Label": "USA", "Address": "2020", "Lat": "36.9628066", "Long": "-122.0194722" },
  3. ...
  4. { "Label": "Santa HEY MAN", "Address": "The city with a boardwalk", "Lat": "36.9628066", "Long": "-122.0194722" }
  5. ]

然后,在您的代码中:

  1. // 读取文件
  2. var json = File.ReadAllText(path);
  3. // 使用 NewtonSoft 进行反序列化
  4. var places = DeserializeObject<List<Place>>(json);
  5. // 然后创建标记点
  6. foreach (var place in places)
  7. {
  8. var pin = new Pin
  9. {
  10. Label = place.Label,
  11. Address = place.Address,
  12. Type = PinType.Place,
  13. Position = new Position(place.Lat, place.Long)
  14. };
  15. map.Pins.Add(pin);
  16. }

请注意,上述代码是伪代码示例,不是 JSON 或 C# 的正确语法。

英文:

note this is all pseudocode, not syntactically correct json/C#

first, create a json file with your data and include it in your project

  1. [
  2. { Label = &quot;USA&quot; Address = &quot;2020&quot;, Lat = &quot;36.9628066&quot; Long = &quot;-122.0194722&quot; },
  3. ...
  4. { Label = &quot;&quot;Santa HEY MAN&quot;, Address = &quot;The city with a boardwalk&quot;, Lat = &quot;36.9628066&quot; Long = &quot;-122.0194722} }
  5. ]

then, in your code

  1. // read the file
  2. var json = File.ReadAllText(path);
  3. // deserialize using NewtonSoft
  4. var places = DeserializeObject&lt;List&lt;Place&gt;&gt;(json);
  5. // then create pins
  6. foreach (var place in places)
  7. {
  8. var pin = new Pin
  9. {
  10. Label = place.Label,
  11. Address = place.Address,
  12. Type = PinType.Place,
  13. Position = new Position(place.Lat, place.Long)
  14. };
  15. map.Pins.Add(pin);
  16. }

huangapple
  • 本文由 发表于 2020年8月7日 19:15:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/63300677.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定