📜 ⬆️ ⬇️

Weather informer from Yandex with city definition by IP (ready code)

Recently, I became interested in the topic of displaying an informer from Yandex.Waters to a site visitor in accordance with its location . The Yandex informer itself shows the weather only in the city that has chosen the webmaster of the site. In practice, there is little sense in such an informer ( described in the previous article). Therefore, it is necessary to determine the city where the visitor is located, and display the necessary informer for him. In the process of studying the topic, I came to the conclusion that, apart from GeoLite City from MaxMind and CNGeoIP, there are no normal world-wide databases IP-> City. However, the GeoLite City base is not suitable for interaction with the Yandex service.
Thus, it was necessary to stop on the basis of CNGeoIP . A version of the database was purchased and an algorithm for obtaining a city code for the informer on the visitor’s IP was built on it. The written script works here: commontools.net/geoip/ya.w.js . The city is determined by the user's IP, a comparison is made with the Yandex database and the city and country id for the informer is displayed as: var yaCountry = 20; var yaCity = 26850; The script naturally works on the server side and displays only the id for JS. And on the page with the informer, the script is included in the HTML code of the page via script src = "..." . Then another simple script script substitutes the variables in the call code of the informer and the picture shows the weather in the city where the site visitor is located. Under it is a link to the settings of the informer, where the visitor will be able to select another city, and the information will be saved in cookies.
So, the result of works in the form of a beta-version, which, I hope, you will help me to test. The page for getting the code is here: http://commontools.net/geoip/ya.weather.get.html . This is the page to get the informer code. It describes how to get the code and screw to the site.
Although the service is still at the beta testing stage, now you can get the code and place it on your website. The service will be open and on, free and free to be used as intended.
See how the informer works, you can now on my blog .
PS For the curious. The domain commontools.net is exclusively auxiliary, it has never been and will not be any sites. Only services for own and public consumption.

')

Source: https://habr.com/ru/post/22992/


All Articles