Skip to content

Conversation

@Motoma
Copy link

@Motoma Motoma commented Oct 14, 2014

Hi there,

I started using your leaflet wrapper and found that I wanted to override the default marker that is created by the addMarker() call. Leaflet already supports this, so I made a minor change to bindings.js to parse out the "icon" option and turn it into an L.Icon.

I'm not a Javascript programmer, but I took a shot at this and it seems to work fine for me.

Cheers,
Chris

@yihui yihui force-pushed the master branch 2 times, most recently from 8fd44f4 to a7af644 Compare October 28, 2014 17:00
@lolokovo
Copy link

lolokovo commented Nov 1, 2014

Hi Chris,

Great job attempting to add this functionality. Thanks very much for that.

I tried to use your solution but was getting an error about the 'in' in the 'if' condition (line 260). This was in Chrome browser. After spending some time modifying the JavaScript I did not succeed to make it working. Even if I succeeded I think your approach would not allow for passing in custom icons through the 'eachOptions' which is something I needed to implement as I need to display various entities on a map with a specific marker icon for each category.

Following implementation of the 'addMarker' method works for 'options' as well as for 'eachOptions':

methods.addMarker = function(lat, lng, layerId, options, eachOptions) {
  var df = dataframe.create()
    .col('lat', lat)
    .col('lng', lng)
    .col('layerId', layerId)
    .cbind(options)
    .cbind(eachOptions);

  for (var i = 0; i < df.nrow(); i++) { 
    (function() {
      var markerOptions = df.get(i);

      try {
          var icon = df.get(i, 'icon');
          icon = L.icon(icon);
          markerOptions.icon = icon;
      }
      catch(err) {}
      finally {
          var marker = L.marker([df.get(i, 'lat'), df.get(i, 'lng')], markerOptions);
          var thisId = df.get(i, 'layerId');
          this.markers.add(marker, thisId);
          marker.on('click', mouseHandler(this.id, thisId, 'marker_click'), this);
          marker.on('mouseover', mouseHandler(this.id, thisId, 'marker_mouseover'), this);
          marker.on('mouseout', mouseHandler(this.id, thisId, 'marker_mouseout'), this);
      }
    }).call(this);
  }
};

Could you please have look at it and let me know if you found any issues with it? I did some basic testing around passing in custom marker icons in various combinations and ways including no custom icon and it seems to be working correctly.

The leaflet-shiny is definitely missing this functionality so whichever solution you find better should get merged to Joe Cheng's master branch.

Your updates in both files were very helpful for me to get this piece working. Thank you again,

Peter

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

Successfully merging this pull request may close these issues.

2 participants