1

What is the best way to modify the searchbox listener searchbox in the CartoDB map?

I would like to disable the default event listener for the CartoDB searchbox and first to execute my own listener and then decide If I want to execute the default CartoDB event (geolocation) for the searchbox.

How is the best way to do it?

I this is not possible can I add a second input text box in the map?

For now I am using $(".cartodb-map-wrapper").append(' '); but I don't know if this is the best thing.

2

You need to extend the funcitonality of the Searchbox. In the following example, I attach the ', British Columbia, Canada' text to it by default.

You can check the example here: http://bl.ocks.org/iriberri/05e6feec84f12ab362df

Copying it also here for the record:

<script>

  cdb.geo.ui.Search = cdb.geo.ui.Search.extend({

    _submit: function(ev) {
      ev.preventDefault();

      var self = this;
      var address = this.$('input.text').val() + ', British Columbia, Canada';

      // Show geocoder loader
      this._showLoader();

      cdb.geo.geocoder.NOKIA.geocode(address, function(coords) {
        if (coords.length>0) {
          var validBBox = true;

          // check bounding box is valid
          if(!coords[0].boundingbox || coords[0].boundingbox.south == coords[0].boundingbox.north ||
            coords[0].boundingbox.east == coords[0].boundingbox.west) {
            validBBox = false;
          }

          if (validBBox && coords[0].boundingbox) {
            self.model.setBounds([
              [
                parseFloat(coords[0].boundingbox.south),
                parseFloat(coords[0].boundingbox.west)
              ],
              [
                parseFloat(coords[0].boundingbox.north),
                parseFloat(coords[0].boundingbox.east)
              ]
            ]);
          } else if (coords[0].lat && coords[0].lon) {
            self.model.setCenter([coords[0].lat, coords[0].lon]);
            self.model.setZoom(10);
          }
        }

        // Hide geocoder loader
        self._hideLoader();
      });
    }
  });


</script>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.