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:
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);
}
}
}
Here I only show 2 pins but in reality, I have 30 pins. How could I make this simpler? Thanks a lot: :)
note this is all pseudocode, not syntactically correct json/C#
first, create a json file with your data and include it in your project
[
{ 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} }
]
then, in your code
// 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);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.