How to make it so that in this code from Yandex the search took place automatically without pressing a button. Code in the sandbox.

function init () { //... ymaps.util.augment(GeolocationButton, ymaps.control.Button, { /** * ΠœΠ΅Ρ‚ΠΎΠ΄ Π±ΡƒΠ΄Π΅Ρ‚ Π²Ρ‹Π·Π²Π°Π½ ΠΏΡ€ΠΈ Π΄ΠΎΠ±Π°Π²Π»Π΅Π½ΠΈΠΈ ΠΊΠ½ΠΎΠΏΠΊΠΈ Π½Π° ΠΊΠ°Ρ€Ρ‚Ρƒ. * @function * @name GeolocationButton.onAddToMap * @param {ymaps.Map} map ΠšΠ°Ρ€Ρ‚Π° Π½Π° ΠΊΠΎΡ‚ΠΎΡ€ΡƒΡŽ добавляСтся ΠΊΠ½ΠΎΠΏΠΊΠ°. */ onAddToMap: function () { GeolocationButton.superclass.onAddToMap.apply(this, arguments); ymaps.option.presetStorage.add('geolocation#icon', { iconImageHref: 'man.png', iconImageSize: [27, 26], iconImageOffset: [-10, -24] }); this.hint = new GeolocationButtonHint(this); // ΠžΠ±Ρ€Π°Π±Π°Ρ‚Ρ‹Π²Π°Π΅ΠΌ ΠΊΠ»ΠΈΠΊ Π½Π° ΠΊΠ½ΠΎΠΏΠΊΠ΅. this.events.add('click', this.onGeolocationButtonClick, this); }, /** * ΠœΠ΅Ρ‚ΠΎΠ΄ Π±ΡƒΠ΄Π΅Ρ‚ Π²Ρ‹Π·Π²Π°Π½ ΠΏΡ€ΠΈ ΡƒΠ΄Π°Π»Π΅Π½ΠΈΠΈ ΠΊΠ½ΠΎΠΏΠΊΠΈ с ΠΊΠ°Ρ€Ρ‚Ρ‹. * @function * @name GeolocationButton.onRemoveFromMap * @param {ymaps.Map} map ΠšΠ°Ρ€Ρ‚Π° с ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠΉ удаляСтся ΠΊΠ½ΠΎΠΏΠΊΠ°. */ onRemoveFromMap: function () { this.events.remove('click', this.onGeolocationButtonClick, this); this.hint = null; ymaps.option.presetStorage.remove('geolocation#icon'); GeolocationButton.superclass.onRemoveFromMap.apply(this, arguments); }, /** * ΠžΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊ ΠΊΠ»ΠΈΠΊΠ° Π½Π° ΠΊΠ½ΠΎΠΏΠΊΠ΅. * @function * @private * @name GeolocationButton.onGeolocationButtonClick * @param {ymaps.Event} e ΠžΠ±ΡŠΠ΅ΠΊΡ‚ события. */ onGeolocationButtonClick: function (e) { //... }, //... }); //... var myMap = new ymaps.Map("map", { center: [55.755768, 37.617671], zoom: 10, behaviors: ["default", "scrollZoom"] }), myButton = new GeolocationButton({ data: { image: baseImageURL + 'wifi.png', title: 'ΠžΠΏΡ€Π΅Π΄Π΅Π»ΠΈΡ‚ΡŒ мСстополоТСниС' }, options: { // Π Π΅ΠΆΠΈΠΌ получСния Π½Π°ΠΈΠ±ΠΎΠ»Π΅Π΅ Ρ‚ΠΎΡ‡Π½Ρ‹Ρ… Π΄Π°Π½Π½Ρ‹Ρ…. enableHighAccuracy: true } }); myMap.controls.add(myButton, {top: 5, left: 5}); } ymaps.ready(init); 
  • and when should it occur? - Ivan Pshenitsyn
  • Immediately after downloading all the content, that is, the map - Vladimir Alexandrov

1 answer 1

Add after this line (50 in the sandbox)

 this.events.add('click', this.onGeolocationButtonClick, this); 

such code

 this.onGeolocationButtonClick.call(this); 

Thus, the call to the function of pressing the button will occur immediately when you create a map.

  • Thank you very much! Here I am a narrow-minded person, I added such code there, but I called the button just Button, I don’t know why I decided that it is called that. in general, thank you! - Vladimir Alexandrov