Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Steady-Entertainment's avatar

Adding icon as marker

Hello community,

I want to change the default marker symbol to another marker symbol.

This is home.blade.php file

My problem is that I want to reference another png as my marker.

But I really don't where I have to put the donut.png inside my working tree so i can reference it correctly in the script tag.

<div
    id="mapid"
    class="h-screen"
></div >

<script >
    let mymap = L.map('mapid').setView([0, 0], 3);

    const myIcon = L.icon({
        iconUrl: 'svg/donut.png',      <------------ WHAT IS THE CORRECT PATH?
        iconSize: [50, 33],
        iconAnchor: [22, 94],

    });

    const marker = L.marker([0, 0, {icon: myIcon}]).addTo(mymap);

    marker.setLatLng([50, 20]);

At the moment I have everything like this, but it doesn't work:

https://i.imgur.com/ez16NaC.png

the icon is saved under public/svg

https://i.imgur.com/PMyNxxH.png

0 likes
35 replies
automica's avatar

@steady-entertainment

change the path to

/images/yourfile.png

and store your files in public/images directory.

automica's avatar

@steady-entertainment your grab further up shows an image being served from

livewiretest.test/donut.png

where did you put the file to get that url? are you using valet to serve this?

automica's avatar

Where did you place that donut file though? Did you drop that in public next to your index.php?

Steady-Entertainment's avatar

I placed it everywhere in my working tree tried every location.

I had it in

public, public/svg, public/images in the same folder as the blade.php

I also tried url() and asset() helpers in many different variations

also with mustache syntax

automica's avatar

@steady-entertainment if you place your image in /public/images/ you should be able to view it in the browser by accessing

livewiretest.test/images/donut.png

if you cant view the image, check you have correct permissions on folder of 755 and on the image as 644.

you dont need to use any of the url or asset helpers. just ensure you have a / at the front when you add to your javascript so its served from site root.

automica's avatar

What’s the permissions on the subfolder images?

There is no problem with your js btw.

I tested by just putting a

<img src=‘/images/donut.png’> 

in the blade. If you can see the image then your permissions are correct

Steady-Entertainment's avatar

But when I do:

    let offline = L.icon({
        shadowUrl: 'leaf-shadow.png',
        iconUrl: {{ URL::asset('/images/iss.png') }},

        iconSize: [38, 95], // size of the icon
        iconAnchor: [22, 94], // point of the icon which will correspond to marker's location
    });

the whole map disappears and nothing works

Snapey's avatar

basic tests

1, Put what you think the url should be in your browser address bar. If the icon does not appear, don't bother going any further until you find out why your web server cannot serve a static image. At this point this IS NOT a javascript or Laravel issue.

2, Assuming you know the correct url for the icon, put it in your code. Hard code it. Not asset, not URL helper, just the raw URI

Open the browser network tools. Refresh the page. See the assets being loaded. Look for the name of your file. Is it found and loaded? If it is in red then it was not found. Check the url being requested. Does it match the path that you think the image is located at? Is the image listed? if it is not even in the list of assets loaded then there is a javascript issue where your request to change the icon is not even considered.

If the browser network tools are new to you, step back for a moment and watch some tutorials on how to use them. They will be used over and over so is a key skill for a web developer.

Steady-Entertainment's avatar

basic tests

1, Put what you think the url should be in your browser address bar. If the icon does not appear, don't bother going any further until you find out why your web server cannot serve a static image. At this point this IS NOT a javascript or Laravel issue.

it works http://livewiretest.test/images/iss.png no problem

2, Assuming you know the correct url for the icon, put it in your code. Hard code it. Not asset, not URL helper, just the raw URI

if I do

const iss = 'http://livewiretest.test/images/iss.png';

    const offline = L.icon({
        iconUrl: iss,
        iconSize: [50, 32], // size of the icon
        iconAnchor: [22, 16], // point of the icon which will correspond to marker's location
    });

that doesn't work and has the same effect like

const iss = '{{ URL::asset('/images/test.png') }}';

const offline = L.icon({
    iconUrl: iss,
    iconSize: [50, 32], // size of the icon
    iconAnchor: [22, 16], // point of the icon which will correspond to marker's location
});

Open the browser network tools. Refresh the page. See the assets being loaded. Look for the name of your file. Is it found and loaded? If it is in red then it was not found. Check the url being requested. Does it match the path that you think the image is located at? Is the image listed? if it is not even in the list of assets loaded then there is a javascript issue where your request to change the icon is not even considered.

Yes the image is not even listed but instead the standard image this blue standard marker is being pulled in via cdn

If the browser network tools are new to you, step back for a moment and watch some tutorials on how to use them. They will be used over and over so is a key skill for a web developer.

Steady-Entertainment's avatar

Thank you Snapey no I have this:

const iss = '{{ URL::asset('/images/test.png') }}';
    
    const offline = L.icon({
        iconUrl: iss,
        iconSize: [50, 32], // size of the icon
        iconAnchor: [22, 16], // point of the icon which will correspond to marker's location
    });

it is still not working but at least the image is displayed when i render it in the HTML part in the JS part it is still not rendering but if i put it like this it doesn't blow up the map so i think I am pretty close

Snapey's avatar

i doubt that..

Make sure you have the browser console open and check for javascript errors.

Don't just disregard the advice. You could stumble around for days, not learning anything. Your URL:asset... makes no sense at all.

Snapey's avatar

This is what I do... no drama

    function init_map() {

        var map = new google.maps.Map(
            document.getElementById('gmap'), { zoom: 16 });

        var santa = {
            url: '/images/santa1.png',
            size: new google.maps.Size(50, 50),
            scaledSize: new google.maps.Size(50, 50),
            origin: new google.maps.Point(0,0),
            anchor: new google.maps.Point(25,25),
        }

        var marker = new google.maps.Marker({
            map: map,
            icon: santa,
            optimized: false,
        });   

santa is declared as an object with all its attributes

marker is added to the map and passes santa as the icon attribute.

The png file is located in /images/

Snapey's avatar

why strange? Yes its in a scripts section, in a blade file, but could equally be a js file

1 like
Steady-Entertainment's avatar

i meant strange that it works like that with you and at my site I am experiencing that problem

I am still wondering about this problem :D maybe it will cost me days to solve :D

how would you go if you experience a problem that you cannot solve at the moment?

Just work on another part and come back later?

automica's avatar

@steady-entertainment I would just find some easy dev to do so you can catch up if you are behind.

Depending on who is paying for the project,I would be clear to raise the problem you are having as there might be a budget hit. You may find that what you've got is good enough, and that will atleast bat the problem away until you can work it out.

Steady-Entertainment's avatar

I don't have anyone who pays me I just do it for everybody.

The idea is to have a platform where you can add offers.

The offers have a location and you can offer something for free or you can ask for something in exchange.

You can mark your offer on the map and other people can so find local offers.

The project is called offline and I am unfortunately working on it alone (at least from the programming) there are a lot of people who want to share content as it should also allow to upload videos that explain the offers.

Then you have a rating system where people can rate the interaction when an offer is completed so you will have a rating for the offers you exchanged and become a master (level system in your area)

Also there is 100 coins that everybody can receive when he/she joins the system and you can trade the coins with each other but you cannot buy the coins.

My idea is to have livewire or jetstream and laravel as back end to have EsXi as scalable servers and to use leaflet for maps and FFMPEG and the laravel/FFMEP package for the video upload and a basic forum and rating and tagging system for the offers....

I know that is a very hard project to do that is why I am learning programming since 2 years but always I run into problems that stop me for days or weeks and this forum has been the greatest repository of help especially with snappy and sinnbeck or you and others.

If I would be as good in programming as you guys are I would have finished it many months ago :D

But I don't know people who are as good in programming as you are and want to work on such a project.

Normal people always want to get paid and I just work for fun not for money :D.

automica's avatar

@steady-entertainment if you want a hand with the project, and are ok to push it to a public repo, i'd be happy to install it at my end and see I can crack this for you,

hopefully @snapey's steps will sort it though.

Snapey's avatar

Did you try the steps I gave earlier?

Steady-Entertainment's avatar
<div
    id="mapid"
    class="h-screen w-full z-0"
></div >

@push('head-scripts')

    <script
        src="https://unpkg.com/[email protected]/dist/leaflet.js"
        integrity="sha512-XQoYMqMTK8LvdxXYG3nZ448hOEQiglfqkJs1NOQV44cWnUrBc8PkAOcXy20w0vlaXaVUearIOBhiXZ5V3ynxwA=="
        crossorigin=""
    >
    </script>

@endpush

    <script>

    //Making map and tiles
    const mymap = L.map('mapid').setView([0, 0], 3);

    L.tileLayer('https://api.mapbox.com/styles/v1/{id}/tiles/{z}/{x}/{y}?access_token={accessToken}', {
        maxZoom: 18,
        id: 'mapbox/streets-v11',
        tileSize: 512,
        zoomOffset: -1,
        accessToken: 'pk.eyJ1IjoicmVuaXIiLCJhIjoiY2toMW54eXF4MDlwZjJybmFoem5zaHoyOCJ9.vMLPuxo_2duOAym6w5q8rg'
    }).addTo(mymap);

    const iss = 'http://livewiretest.test/images/iss.png';

    const offline = L.icon({
        iconUrl: iss,
        iconSize: [50, 32], // size of the icon
        iconAnchor: [22, 16], // point of the icon which will correspond to marker's location
    });

    const marker = L.marker([0, 0, {icon: offline}]).addTo(mymap);


    marker.setLatLng([50, 20]);

</script >

Steady-Entertainment's avatar
Level 11

I really don't know what did the trick but I changed the tile provider and I changed some variable types from const to var and the other way around

I really don't know what was wrong before but now its working seamlessly :D

https://i.imgur.com/deLQOwC.png

{{--<img src={{ URL::asset('/images/iss.png') }} alt="">--}}
<div
    id="mapid"
    class="h-screen"
></div >

@push('head-scripts')

    <script
        src="https://unpkg.com/[email protected]/dist/leaflet.js"
        integrity="sha512-XQoYMqMTK8LvdxXYG3nZ448hOEQiglfqkJs1NOQV44cWnUrBc8PkAOcXy20w0vlaXaVUearIOBhiXZ5V3ynxwA=="
        crossorigin=""
    >
    </script >

@endpush

<script >
          lat = 22;
          lon = 22;
          const mymap = L.map('mapid').setView([lat, lon], 3);


          var redcircle = L.circle([51.508, -0.11], {
              color: 'red',
              fillColor: '#f03',
              fillOpacity: 0.5,
              radius: 500
          }).addTo(mymap);

          var myIcon = L.icon({
              iconUrl: 'http://livewiretest.test/images/donut.png',
              iconSize: [38, 95],
              iconAnchor: [22, 94],
          });

          L.marker([50.505, 30.57], {icon: myIcon}).addTo(mymap);

          const tileUrl = 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png';
          const tiles = L.tileLayer(tileUrl);
          tiles.addTo(mymap);


          // const marker = L.marker([lat, lon]).addTo(mymap);

    </script >
1 like
Snapey's avatar

Sorry should have picked up the hints that you were using leaflet and not google maps.

1 like

Please or to participate in this conversation.