Mapsui / Mapsui

Mapsui is a .NET Map component for: MAUI, WPF, Avalonia, Uno, Blazor, WinUI, Xamarin and Eto
https://mapsui.com
MIT License
1.24k stars 318 forks source link

[Xamarin Forms] Question : How can I use GeoJson in Mapsui? #662

Closed JesrigPineda closed 5 years ago

JesrigPineda commented 5 years ago

Hello, I am trying to work with GeoJson in Mapsui through a Web Service and I would like to save points and consult them and show them on the map. Is this possible?

Mapsui 2.0.0 beta.22

JesrigPineda commented 5 years ago

This is my Json with a GeoJson Point

[{"id":1,"nom_client":"Manuel Loeza","address":"Av Cuauht\u00e9moc S\/N, Parque Ignacio Manuel Altamirano, 39630 Ruta Solidaridad El Roble, Gro","geom":{"type":"Point","coordinates":[-99.886527312673,16.8618566099974]}}]

my class model in this case id, nom_client, address and geom are based on the JSON structure that provides http://json2csharp.com or https://app.quicktype.io

How could I get the point and show it as a point or Pin on the Map?

This is the code that shows dese realizes to show it in a list

    public async Task<IEnumerable<Service>> GetService()
    {

        HttpClient client = GetClient();

        var res = await client.GetAsync(URL);

        if (res.IsSuccessStatusCode)
        {

            string conten = await res.Content.ReadAsStringAsync();

            return JsonConvert.DeserializeObject<IEnumerable<Service>>(conten); 
        }

      return Enumerable.Empty<Service>();
    }
mattmiley commented 5 years ago

You need to convert the geojson to a mapsui feature, then add the feature to a layer, then add the layer to a map. Checkout the [Points Sample].(https://github.com/Mapsui/Mapsui/blob/d6f0ce17d25487b268819b576e40776f4a9939b4/Samples/Mapsui.Samples.Common/Maps/PointsSample.cs#L58)

JesrigPineda commented 5 years ago

@mattmiley thank you for answering, I apologize I had not had time to answer you. I still do not know very clearly, you could explain me a little more in detail, I would appreciate it.

mbaker92 commented 5 years ago

Store the coordinates and whatever other information you want from the GeoJSON. You will need to extract the Latitude and Longitude and pass them into the function below to get a point that will be placed on the map.

This line creates the point from the coordinates. Point PointOfInterest = SphericalMercator.FromLonLat( long , lat )

Once the point is created you will create a feature with the point using

                var feature = new Feature
                {
                    Geometry = PointOfInterest,
                    ["Label"] = Address,  // Address variable that holds GeoJSON address?
                    ["Type"] = "Type"                              
                };

You can then add a style to point (this will be a Green Color) like this feature.Styles.Add(new SymbolStyle { SymbolScale = 0.5, Fill = { Color = new Color(49, 247, 4, 255) } });

And then add the feature to the map. You will have to create loops and lists for multiple roads and if there are multiple points.

pauldendulk commented 5 years ago

Thanks again @mbaker92.

Also there is some use of GeoJson in the vector tile branch. I wrote a converter from GeoJson to Mapsui.Geometries on some point. https://github.com/Mapsui/Mapsui/tree/vectortiles