Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Marker disappears after setting icon size or anchor. How to set size or anchor of custom marker icon? #53

Closed
Pastew opened this issue Mar 3, 2021 · 1 comment

Comments

@Pastew
Copy link

Pastew commented Mar 3, 2021

Hi.

I want to set custom marker icon and it works OK:
marker.Icon = new Icon { Url = "/markers/marker_13.png" };

But then I want to set it's size or Anchor and it disappears from map:
marker.Icon = new Icon { Url = "/markers/marker_13.png", Size = new System.Drawing.Size(32, 32) };

How to set size or anchor of custom marker icon?

Full code below:

@page "/map"

@using BlazorLeaflet.Models
@using BlazorLeaflet.Models.Events
@using BlazorLeaflet.Samples.Data
@using BlazorLeaflet
@inject IJSRuntime jsRuntime

<h1>Blazor Leaflet </h1>

<h3>Drag &amp; drop the Marker!</h3>

<div class="form-inline m-2">
    <div class="form-group mb-2">
        <label>Lat </label>
        <input class="form-control ml-2 mr-2" placeholder="city name" @bind="_markerLatLng.Lat" disabled />
    </div>

    <div class="form-group mb-2">
        <label>Lng </label>
        <input class="form-control ml-2 mr-2" placeholder="city name" @bind="_markerLatLng.Lng" disabled />
    </div>
</div>

<div style="height: 500px; width: 500px;">
    <LeafletMap Map="_map" />
</div>

<div class="pt-3">
    <DrawZoneToggleButton ButtonText="Draw Rectangle" DrawHandler="_drawHandler" OnToggleCallback="_drawHandler.OnDrawRectangleToggle" />
    <DrawZoneToggleButton ButtonText="Draw Circle" DrawHandler="_drawHandler" OnToggleCallback="_drawHandler.OnDrawCircleToggle" />
    <DrawZoneToggleButton ButtonText="Draw Polygon" DrawHandler="_drawHandler" OnToggleCallback="_drawHandler.OnDrawPolygonToggle" />
</div>

@code
{
    private Map _map;
    private DrawHandler _drawHandler;
    private LatLng _markerLatLng = new LatLng { Lat = 50.0171428f, Lng = 19.8900518f };

    protected override void OnInitialized()
    {
        var marker = new Marker(_markerLatLng)
        {
            Draggable = true,
            Title = "Marker 1",
            Popup = new Popup { Content = string.Format("I am at {0:0.00}° lat, {1:0.00}° lng", _markerLatLng.Lat, _markerLatLng.Lng) },
            Tooltip = new Tooltip { Content = "Click and drag to move me" }
        };

        marker.Icon = new Icon { Url = "/markers/marker_13.png", Size = new System.Drawing.Size(32, 32) };

        _map = new Map(jsRuntime)
        {
            Center = _markerLatLng,
            Zoom = 4.8f
        };

        _map.OnInitialized += () =>
        {
            _map.AddLayer(new TileLayer
            {
                UrlTemplate = "https://a.tile.openstreetmap.org/{z}/{x}/{y}.png",
                Attribution = "&copy; <a href=\"https://www.openstreetmap.org/copyright\">OpenStreetMap</a> contributors",
            });

            _map.AddLayer(marker);
        };

        _drawHandler = new DrawHandler(_map, jsRuntime);

        marker.OnMove += OnDrag;
        marker.OnMoveEnd += OnDragEnd;
    }

    private void OnDrag(Marker marker, DragEvent evt)
    {
        _markerLatLng = evt.LatLng;
        StateHasChanged();
    }

    private async void OnDragEnd(Marker marker, Event e)
    {
        marker.Position = _markerLatLng;
        marker.Popup.Content = string.Format("I am now at {0:0.00}° lat, {1:0.00}° lng", _markerLatLng.Lat, _markerLatLng.Lng);
        await LeafletInterops.UpdatePopupContent(jsRuntime, _map.Id, marker);
    }
}
@Pastew
Copy link
Author

Pastew commented Mar 3, 2021

OK, I fixed it, here's how:

  1. I implemented changes form this pull request: Likely workaround #41 icon size/anchor in WASM #45
    Thank you @chucker !

  2. I set HasAnchor and HasSize to true when creating icon, code below.

marker.Icon = new Icon {
    Url = "/markers/marker_13.png",
    HasAnchor = true, Anchor = new System.Drawing.Point(37, 133),
    HasSize = true, Size = new System.Drawing.Size(53, 67)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant