smeijer / leaflet-geosearch

A geocoding/address-lookup library supporting various api providers.
https://smeijer.github.io/leaflet-geosearch/
MIT License
1.03k stars 273 forks source link

Manually Setting Content of Marker's Popup #121

Closed joshuaballas closed 7 years ago

joshuaballas commented 7 years ago

I am having a hard time figuring out how to set the content of a popup. I have a GeoSearchControl created with showPopup = true. I also see popupFormat, but not sure what to change it to in order to set the content within the map.on('geosearch/showlocation') function. Could I get a few pointers on this? Thanks.

As of right now, I am using jquery to change the html of the leaflet-popup-content class.

smeijer commented 7 years ago

showPopup is required to, well. Show the popup. The popupFormat is for changing the default result label to something that is more up to your needs. It get's the query and result as arguments, and requires a string to be returned.

const searchControl = new GeoSearchControl({
  provider: provider,
  showPopup: true,
  popupFormat: function(response) {   // pass this function to see the full result
    return JSON.stringify(response);  // and format as you like, just return a string
  }
});

map.addControl(searchControl);

When now searching for Amsterdam, you'll get a popup with the text:

{  
   "query":"Amsterdam, Spitsbergen, Noorwegen",
   "result":{  
      "x":"10.833333",
      "y":"79.75",
      "label":"Amsterdam, Spitsbergen, Noorwegen",
      "bounds":[  
         [  
            79.43,
            10.513333
         ],
         [  
            80.07,
            11.153333
         ]
      ],
      "raw":{  
         "place_id":"1298730",
         "licence":"Data © OpenStreetMap contributors, ODbL 1.0. http://www.openstreetmap.org/copyright",
         "osm_type":"node",
         "osm_id":"302068013",
         "boundingbox":[  
            "79.43",
            "80.07",
            "10.513333",
            "11.153333"
         ],
         "lat":"79.75",
         "lon":"10.833333",
         "display_name":"Amsterdam, Spitsbergen, Noorwegen",
         "class":"place",
         "type":"island",
         "importance":0.655
      }
   }
}

Or something like:

// es2016
const searchControl = new GeoSearchControl({
  provider: new OpenStreetMapProvider(),
  showPopup: true,
  popupFormat: ({ query, result }) => `
    You've searched for: ${query}, <br />
    We found it at ${result.x.substr(0, 5)}, ${result.y.substr(0, 5)}
  `,
});

image

"old skool" would look like:

// es5
const searchControl = new GeoSearchControl({
  provider: provider,
  showPopup: true,
  popupFormat: function(response) {
    return [
      "You've searched for: ", response.query, 
      "<br />",
      "We found it at ", response.result.x.substr(0, 5), ", ", response.result.y.substr(0, 5)
    ].join('');
  }
});
joshuaballas commented 7 years ago

Great explanation, thanks!

smeijer commented 7 years ago

No problem. Please let me know if there is anything else I can help with.