Recently, I faced an interesting task - to display a map on the site, with various objects, and these objects can be dozens and hundreds, and any below-middle manager should be able to manage them.
More formally, the task was to enable users to set a certain list of geo-objects, give them the ability to easily and quickly manage these objects and automatically display these objects on a certain map.
To implement the idea, a bunch of Google Docs and 2GIS map service API were chosen. The solution turned out really simple, in the spirit of the famous "30 lines" :)
Step 1. Create a table for the data.
Open Google Docs and create a table. In my table there were quite a few unnecessary fields for me, but very necessary for other services, so I’ll go for a little trick and I’ll have a separate one for testing.
')
Here is a link to it:
docs.google.com/spreadsheet/ccc?key=0AvdXWIJtCGnndG0tODY5MzRsVFhnbkJseFk0aWJkaUE&usp=sharingAs test data, I took the addresses of houses belonging to two small polling stations in the city of Kostroma. Site number, street, house number and number of apartments.
Using Google Drive as a data source removes many problems with the collaboration of specialists of different degrees of training and with prompt data updates.
Now the table needs to be published. To do this, go to the menu "File - Publish on the Internet." In the window that appears, select "Start publication", in the lower part - select "CSV (values ​​separated by commas)" and get a link to our worksheet as a CSV.
It turns out this link:
docs.google.com/spreadsheet/pub?key=0AvdXWIJtCGnndG0tODY5MzRsVFhnbkJseFk0aWJkaUE&single=true&gid=0&output=csvStep 2. Prepare the data for the map.
Let's write a simple PHP script and call it getMapData.php:
define('CSV_DATA_URL', 'https://docs.google.com/spreadsheet/pub?key=0AvdXWIJtCGnndG0tODY5MzRsVFhnbkJseFk0aWJkaUE&single=true&gid=0&output=csv'); define('DEFAULT_CITY', ''); $points = array(); $csvDataHandle = fopen(CSV_DATA_URL, 'r'); $i = 0; while ( false !== ($line = fgetcsv($csvDataHandle, null, ',', '"')) ) {
Here it should be noted that most likely when you first start you will get the error "Warning: fopen (): Unable to find the wrapper" https ". This is due to the fact that in the default PHP installation, the OpenSSL module is not included. Find the line in the php.ini file
;extension=php_openssl.so
and uncomment it by removing the semicolon. This is usually enough.
Of course, the $ points array needs to be cached somewhere, so as not to “jerk” Google Docs every time a map is drawn.
Now we have the next task. It is necessary to translate the addresses of geo objects into coordinates on the map. For this, 2GIS has a corresponding API:
api.2gis.ru/doc/geo/searchHere is the simplest code for working with the geocoder API:
define('DGIS_API_KEY', 'XXXXXX');
Finally, let's go over the $ points array by our “geocoder” and display the result in JSON format:
foreach ( $points as &$address ) { $address['coords'] = getGeoObjectInfo($address['address']); sleep(1); } echo json_encode($points);
Pay attention to the strange line sleep (1); in the code. The fact is that I use test access to the API 2GIS, and with it the frequency of requests is limited to one per second. After gaining full access, this line must, of course, be removed.
Well, do not forget again to cache the data, so as not to create an extra load for 2GIS, and for yourself - too much script time.
Step 3. Show the map.
Here I will give the full code page. There is nothing complicated in it, especially since 2GIS has good documentation, you can read it at
api.2gis.ru/doc/maps/info <!DOCTYPE html> <html> <head> <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> <script type="text/javascript" src="http://maps.api.2gis.ru/1.0?loadByRequire=1"></script> <title> </title> </head> <body> <div id="DGMap" style="width:100%; height:600px"></div> <script type="text/javascript"> $(DG.load(function() { </script> </body> </html>
On this, in fact, everything. We got a simple but functional system that displays objects on a map according to the list in Google Docs. Here is a mash-up :)
PS
I do not give the link to the turned-out card because I am afraid for the server which can easily lie under a habraeffekt. But I can not insert a picture:

Pps
Actually, nothing prevents to do the same “trick” with the cards, for example, from Yandex. It will turn out even more beautiful, due to the set of built-in styles and clustering technology of markers on the map. Initially, I went this way, but the tests showed that the 2GIS geocoder is still much more accurate. Yandex copes with obtaining coordinates from about 95% of the addresses, provided that they are thoroughly prepared, 2GIS - almost all 100%.