Hello friends. I connect yandexmaps api to js and there is a piece of code in php, the task in general is this. I can not transfer a floating-point number from php to js.

The code below is how to do this?

<?php require __DIR__.'/vendor/autoload.php'; $api = new \Yandex\Geo\Api(); // МоТно ΠΈΡΠΊΠ°Ρ‚ΡŒ ΠΏΠΎ Ρ‚ΠΎΡ‡ΠΊΠ΅ // $api->setPoint(30.5166187, 50.4452705); $adress = $_POST['adress']; // Или ΠΌΠΎΠΆΠ½ΠΎ ΠΈΠΊΠ°Ρ‚ΡŒ ΠΏΠΎ адрСсу $api->setQuery($adress); // Настройка Ρ„ΠΈΠ»ΡŒΡ‚Ρ€ΠΎΠ² $api // ->setLimit(1) // ΠΊΠΎΠ»-Π²ΠΎ Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚ΠΎΠ² ->setLang(\Yandex\Geo\Api::LANG_RU) // локаль ΠΎΡ‚Π²Π΅Ρ‚Π° ->load(); $response = $api->getResponse(); $response->getFoundCount(); // ΠΊΠΎΠ»-Π²ΠΎ Π½Π°ΠΉΠ΄Π΅Π½Π½Ρ‹Ρ… адрСсов $response->getQuery(); // исходный запрос $response->getLatitude(); // ΡˆΠΈΡ€ΠΎΡ‚Π° для исходного запроса $response->getLongitude(); // Π΄ΠΎΠ»Π³ΠΎΡ‚Π° для исходного запроса // Бписок Π½Π°ΠΉΠ΄Π΅Π½Π½Ρ‹Ρ… Ρ‚ΠΎΡ‡Π΅ΠΊ $collection = $response->getList(); foreach ($collection as $item) { $item->getAddress(); // Π²Π΅Ρ€Π½Π΅Ρ‚ адрСс $shirota = $item->getLatitude(); // ΡˆΠΈΡ€ΠΎΡ‚Π° $dolgota = $item->getLongitude(); // Π΄ΠΎΠ»Π³ΠΎΡ‚Π° $item->getData(); // Π½Π΅ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚Π°Π½Π½Ρ‹Π΅ Π΄Π°Π½Π½Ρ‹Π΅ } echo "Π”ΠΎΠ»Π³ΠΎΡ‚Π°: "; $dolg = printf ($dolgota); echo "$dolg"; echo '<script language="javascript">var a = '.$dolg.';</script>'; echo "<br>Π¨ΠΈΡ€ΠΎΡ‚Π°: "; $shir = printf ($shirota); echo "$shir"; echo '<script language="javascript">var b = parseFloat('.$shir.');</script>'; ?> <!DOCTYPE html> <html> <head> <title>ΠŸΡ€ΠΈΠΌΠ΅Ρ€Ρ‹. Π—Π°Π΄Π°Π½ΠΈΠ΅ собствСнного изобраТСния для ΠΌΠ΅Ρ‚ΠΊΠΈ</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <!-- Если Π²Ρ‹ ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΡƒΠ΅Ρ‚Π΅ API локально, Ρ‚ΠΎ Π² URL рСсурса Π½Π΅ΠΎΠ±Ρ…ΠΎΠ΄ΠΈΠΌΠΎ ΡƒΠΊΠ°Π·Ρ‹Π²Π°Ρ‚ΡŒ ΠΏΡ€ΠΎΡ‚ΠΎΠΊΠΎΠ» Π² стандартном Π²ΠΈΠ΄Π΅ (http://...)--> <script src="https://api-maps.yandex.ru/2.1/?lang=ru_RU" type="text/javascript"></script> <!-- <script src="icon_customImage.js" type="text/javascript"></script> --> <script type="text/javascript"> ymaps.ready(function () { var myMap = new ymaps.Map('map', { center: [(b), (a)], //1 - ΡˆΠΈΡ€ΠΎΡ‚Π°. 2 - Π΄ΠΎΠ»Π³ΠΎΡ‚Π° zoom: 9 }, { searchControlProvider: 'yandex#search' }), // Π‘ΠΎΠ·Π΄Π°Ρ‘ΠΌ ΠΌΠ°ΠΊΠ΅Ρ‚ содСрТимого. MyIconContentLayout = ymaps.templateLayoutFactory.createClass( '<div style="color: #FFFFFF; font-weight: bold;">$[properties.iconContent]</div>' ), myPlacemark = new ymaps.Placemark(myMap.getCenter(), { hintContent: 'БобствСнный Π·Π½Π°Ρ‡ΠΎΠΊ ΠΌΠ΅Ρ‚ΠΊΠΈ', balloonContent: 'Π­Ρ‚ΠΎ красивая ΠΌΠ΅Ρ‚ΠΊΠ°' }, { // ΠžΠΏΡ†ΠΈΠΈ. // НСобходимо ΡƒΠΊΠ°Π·Π°Ρ‚ΡŒ Π΄Π°Π½Π½Ρ‹ΠΉ Ρ‚ΠΈΠΏ ΠΌΠ°ΠΊΠ΅Ρ‚Π°. iconLayout: 'default#image', // Π‘Π²ΠΎΡ‘ ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ ΠΈΠΊΠΎΠ½ΠΊΠΈ ΠΌΠ΅Ρ‚ΠΊΠΈ. iconImageHref: 'images/myIcon.gif', // Π Π°Π·ΠΌΠ΅Ρ€Ρ‹ ΠΌΠ΅Ρ‚ΠΊΠΈ. iconImageSize: [30, 42], // Π‘ΠΌΠ΅Ρ‰Π΅Π½ΠΈΠ΅ Π»Π΅Π²ΠΎΠ³ΠΎ Π²Π΅Ρ€Ρ…Π½Π΅Π³ΠΎ ΡƒΠ³Π»Π° ΠΈΠΊΠΎΠ½ΠΊΠΈ ΠΎΡ‚Π½ΠΎΡΠΈΡ‚Π΅Π»ΡŒΠ½ΠΎ // Π΅Ρ‘ "Π½ΠΎΠΆΠΊΠΈ" (Ρ‚ΠΎΡ‡ΠΊΠΈ привязки). iconImageOffset: [-5, -38] }); myMap.geoObjects .add(myPlacemark) .add(myPlacemarkWithContent); }); </script> <style> html, body, #map { width: 400px; height: 400px; padding: 0; margin: 0; } </style> </head> <body> <div id="map"></div> </body> </html> 
  • us with this code, what do we need to do? What exactly is the problem? add a minimum code and description of what is happening. - teran

1 answer 1

The printf function prints the result to the screen, and does not return a value. And you try to assign its result to a variable, naturally there will be emptiness. Perhaps you should use the sprintf () function, which works in the same way as the first one, but instead of displaying the result on the screen, returns it. But you can make everything much easier:

 echo "<br>Π¨ΠΈΡ€ΠΎΡ‚Π°: "; echo $shirota; echo '<script language="javascript">var b = parseFloat('.$shirota.');</script>'; 

The variable $shirota as I understand it, already contains the desired value, so it makes no sense to use (s) the printf function. Unless you need to output a value in a specific format, then you need to do something like this:

 $shir = sprintf('%.5f', $shirota); 
  • Brilliant! Thank you =) I also tried parseFloat but did not return the value, as you said. And so everything is ok. - Sindr0me