I am creating a Xamarin.Forms.Maps.Polyline
in runtime. How can I append positions dynamically, given that the Polyline.Geopath
property is read-only?
Createing a polyline in runtime
Following the document to create a polyline : Xamarin.Forms Map Polygons and Polylines. this link is a tutorial with fixed position in the code. How to assign position in runtime dynamically.
using Xamarin.Forms.Maps;
// ...
Map map = new Map
{
// ...
};
Create a object to store routing data (data extracted from json)
public class MapRoute
{
//[JsonPropertyName("timestamp")]
public long timestamp { get; set; }
//[JsonPropertyName("lat")]
public double lat { get; set; }
//[JsonPropertyName("lng")]
public double lng { get; set; }
public MapRoute(long v1, double v2, double v3)
{
timestamp = v1;
lat = v2;
lng = v3;
}
}
Serialize routing object to JsonString.
public void RouteAppend(MapRoute route)
{
JsonString.Append(JsonSerializer.Serialize(route));
JsonString.Append(",");
}
In real life, there are more than 2 elements in jsonString (there are more than 1000 elements which stored in jsonString)
readonly string jsonString = " [ {\"timestamp\": 1514172600000, \"Lat\": 37.33417925, \"Lng\": -122.04153133}, " + "{\"timestamp\": 1514172610000, \"Lat\": 37.33419725, \"Lng\": -122.04151333} ]";
JsonDocument doc;
JsonElement root;
private IList<Position> pos;
doc = Parse(testString);
root = doc.RootElement;
var routes = root.EnumerateArray();
while (routes.MoveNext())
{
var user = routes.Current;
pos.Add(new Position(Convert.ToDouble(user.GetProperty("lat")), Convert.ToDouble(user.GetProperty("lng"))));
}
Finally, there is a list pos
with a lot of Position
, I would assign the pos
to Geopath
. Unfortunately, it isn't allowed. It is a readonly property:
// instantiate a polyline
Polyline polyline = new Polyline
{
StrokeColor = Color.Blue,
StrokeWidth = 12,
Geopath = pos // It is a readonly property, cannot assign pos to Geopath
}
// add the polyline to the map's MapElements collection
map.MapElements.Add(polyline);
How can this problem be resolved?