1

I want to use leaflet map kit in xamarin.forms uwp project. For that I have created index.html file in pcl project. But when I'm trying to read that file it always returns null.

index.html

<!DOCTYPE html>
<html>
<head>
    <title>LeafletMap</title>

    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />

    <!--leaflet js-->
    <script src="JS/leaflet.js"></script>
    
    <!--leaflet css-->
    <link rel="stylesheet" href="CSS/leaflet.css" />
   
    <style>
        html, body {
            height: 100%;
            margin: 0;
        }

        #map {
            width: 600px;
            height: 400px;
        }
    </style>

    <style>
        body {
            padding: 0;
            margin: 0;
        }

        #map {
            height: 100%;
            width: 100vw;
        }
    </style>

</head>
<body>
    <div id='map'></div>

    <script type="text/javascript">

        var mbAttr = '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors',
            mbUrl = 'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
            mbUrl2 = 'http://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}',
            mbAttr2 = 'Tiles &copy; Esri &mdash; Source: Esri, i-cubed, USDA, USGS, AEX, GeoEye, Getmapping, Aerogrid, IGN, IGP, UPR-EGP, and the GIS User Community';

       var OSM = L.tileLayer(mbUrl, { id: 'OSM', attribution: mbAttr }),
           Sat = L.tileLayer(mbUrl2, { id: 'Sat', attribution: mbAttr2 });

       var baseLayers = {
            "OSM": OSM,
            "Satellite": Sat
       };
        
       var markerGroup = new L.LayerGroup();

        // Map initialization
        var map = L.map('map',{
            center: [-2.1319654, -79.9319396],
            zoom:5,
            layer: [OSM]
        });

        map.addLayer(markerGroup);

        L.control.layers(baseLayers).addTo(map);

       
    </script>
    
</body>
</html>


InitializeMap

var source = new HtmlWebViewSource();
source.BaseUrl = DependencyService.Get<IBaseUrl>().Get();
var assembly = typeof(MainPage).GetTypeInfo().Assembly;
var stream = assembly.GetManifestResourceStream("LeafletMap.index.html");

StreamReader reader = null;
if (stream != null) 
{
    try
    {
        reader = new StreamReader(stream);
        source.Html = reader.ReadToEnd();
    }
    catch (Exception ex)
    {
        System.Diagnostics.Debug.WriteLine($"Exception \n - {ex.Message}");
    }
    finally 
    {
        if (reader != null) 
        {
            reader.Dispose();
        }
    }
    webView.Source = source;
}

This line always returns null.

assembly.GetManifestResourceStream("LeafletMap.index.html"); 

Anyone know how to use leaflet mapkit in xamarin.forms?

I have referred this sample: https://github.com/osekom/LeafletMapXamarinForms.

It's working fine as it has old versioned xamarin.forms the same is not working with latest version.

Divyesh
  • 2,085
  • 20
  • 35
  • is the file an Embedded Resource? Have you verified that you are using the correct resource id? – Jason Dec 23 '21 at 17:50
  • Yes, I have set the action type to Embedded Resource. Correct resource id means? – Divyesh Dec 23 '21 at 17:52
  • the resource id is the string you use to load the resource. You should be able to look at the properties in VS to see the ID – Jason Dec 23 '21 at 17:57
  • Does this answer your question? [Can't load a manifest resource with GetManifestResourceStream()](https://stackoverflow.com/questions/3068736/cant-load-a-manifest-resource-with-getmanifestresourcestream) – ToolmakerSteve Dec 23 '21 at 19:36
  • @Jason, unfortunately, for an embedded file, its properties only lists the filename and path - there is no "resource ID" property to show the string needed to get as resource. (This must have changed; I remember editing a resource ID there, in the past.) – ToolmakerSteve Dec 23 '21 at 19:56

1 Answers1

1

It worked in original because LeafletMap is the assembly name. Presumably, your project has a different name, e.g. MyProject.

Change

assembly.GetManifestResourceStream("LeafletMap.index.html");

To

assembly.GetManifestResourceStream("MyProject.index.html"); 

Substitute your actual project's Assembly Name for "MyProject". By default, this will be the same as the project's name. If in doubt, open "Properties" for project, Application tab, "Assembly name:".


If still doesn't work, here is how to see all the resource ids of your assembly:

var names = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceNames();

Put a breakpoint on a line after this. Then in VS "Immediate" window, type "names".

ToolmakerSteve
  • 18,547
  • 14
  • 94
  • 196