
On the site
Yandex.Pogoda there is a
service of informers . When installing an informer on your site, you must select the city that will be displayed on the informer. And how to show on the informer not the chosen city, but the city where the visitor is located?
The forecast on the service from Yandex is pretty accurate. I use it all the time. Therefore, I chose the informer on my sites from this service. It is beautiful, informative, stable (Yandex falls extremely ... very rarely).
However, when installing an informer to your site, you must select a city, the weather in which will be displayed on the informer. Well, if the whole party on the site - visitors from one city. And what if not (probably 98% of cases)? For example, the blog audience is usually completely divided into different countries, not to mention the cities. Then this informer is not very practical, because very few people are interested in the weather in your area.
There are three ways out.
The first is simpler, but solves only half of the problem. When registering a user in the system, make him enter the city. And, if the city coincides with the
list of Yandex , then display the corresponding informer for it. You can use the same list as an automatic substitution in the city input field during registration. Solves half of the problem, because it works only after registration.
The second way is to use the base of cities by IP addresses. But it should be borne in mind that such databases are paid, they need to be constantly updated and the data in them is accurate only with some degree (for example, 90%). Well, do not forget that some use anonymous proxies. Databases:
MaxMind GeoIP® City Database ,
CNGeoip ,
IP2City .
The third way is the easiest, but it has a big drawback. Actually, in order. You hang informer with some city by default. Below it (or above it) makes the link “Customize region”. Or even instead of the default informer itself, you can place this link. A person clicks on it, he is given a list of cities from Yandex. The visitor chooses his city, the information is stored in the cookie to the user. And the next time you load the page of the site, the information from the cookies is inserted into the informer, and the visitor sees the weather in his hometown. The big disadvantage of this method is that it is applicable only on the site with a permanent audience. Because Visitors of the same blogs usually leave the site quickly and rarely return to it; it simply does not make sense to give them an informer setting. (So ​​I didn’t do this on my blog.)
Implementation for option 3 has already been written. A page with a working JS code, which saves the settings in cookies, is displayed. You can use the page on your sites (open the HTML code and embed it yourself).
The list of cities in text form can be obtained
in my blog , if you need it (I decided not to clutter up this post).