Leaflet Markers
Asked Answered
S

5

7

I’m new to leaflet, and I’m trying to display the markers. The tutorials don’t seem to work for me. The map shows up fine, however I just can’t get a marker to display. below is my sample code:

wax.tilejson('http://localhost:8888/v2/DigitalHumanities.json',
  function(tilejson) {
        var map = new L.Map('map-div')
            .addLayer(new wax.leaf.connector(tilejson))
            .setView(new L.LatLng(-17.1828,137.4609), 4);

    var markers = new L.marker(-17.1828,137.4609);
    map.addLayer(markers);

    var markerx = new L.marker(137.4609,-17.1828);
    map.addLayer(markerx);

  });

I’ve tried the samples in the tutorials i.e.: .addTo(map); , map.addLayer(markers); etc.

Samsun answered 15/2, 2013 at 2:36 Comment(0)
S
14

The L.marker constructor should be used as:

var markers = L.marker([-17.1828,137.4609]);
map.addLayer(markers);

You can check the API reference here

Sachet answered 15/2, 2013 at 7:10 Comment(1)
Is that new supposed to be there? Its supposed to be a factory method isn't it.Employee
P
1

The actual syntax for creating leaflet marker is

L.marker(<LatLng> latlng, <Marker options> options? );

You can check the API reference here
Below is your code

correct code

wax.tilejson('http://localhost:8888/v2/DigitalHumanities.json',
  function(tilejson) {
        var map = new L.Map('map-div')
            .addLayer(new wax.leaf.connector(tilejson))
            .setView(new L.LatLng(-17.1828,137.4609), 4);

    var markers = new L.marker([-17.1828,137.4609],{clickable:true});
    map.addLayer(markers);

    var markerx = new L.marker([137.4609,-17.1828]);
    map.addLayer(markerx);

  });
Pyrometer answered 13/1, 2016 at 13:4 Comment(0)
U
1

You can find the working example here< https://jsfiddle.net/viswanathamsantosh/x63kzb31/ >. The below line would add a marker and also a popup to your map when clicked on the marker.

new L.Marker([46.947, 7.4448]).addTo(map).bindPopup('hello world!!!');
Unvarnished answered 29/8, 2017 at 7:47 Comment(0)
J
0
let markers = L.marker([-17.1828,137.4609]);

or

let markers = L.marker({lat: -17.1828,lng: 137.4609});`

then:

map.addLayer(markers);
Jaggery answered 19/1, 2017 at 6:48 Comment(0)
S
0

You can do it by either using the factory or the 'new' keyword on the class (which I believe is what the factory does anyway. The difference is in the case used.

I believe these should both work the same way:

var markerx = new L.Marker(L.latLng(137.4609,-17.1828));
map.addLayer(markerx);

.

var markerx = L.marker(L.latLng(137.4609,-17.1828));
map.addLayer(markerx);

But you cannot combine them.

Shinberg answered 20/7, 2017 at 0:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.