📜 ⬆️ ⬇️

Yandex Maps: Search for arbitrary objects

image Recently, it was necessary to write a script that, according to a given list, searches for stores on Yandex Maps and shows those available depending on the user's location.
For example, you need to find all the Herbs and Seasonings stores located in the user's city, while not using the database of exact addresses and coordinates of the stores.
A thorough study of the API did not produce results, so it was decided to invent its own search method.
Interested please under the cat.

The documentation states that the search on the map can be performed using the geocode function, passing it a toponym corresponding to the task. For example, to find a street you need to pass the toponym street and so on. more here .
In studying the table of possible values, the value other was found. But checking the response of the geodecoder revealed that he did not know how to look for objects like 'Moscow Herb shop seasoning'.

Request:

'http://geocode-maps.yandex.ru/1.x/?geocode=Moscow, Novy Arbat Street, 24'
')
will give the correct information, but this:

'http://geocode-maps.yandex.ru/1.x/?geocode=Moscow, Herbs and Condiments Store'

will give a bunch of information that is not relevant to the subject of the search

It was decided to look at what can be pulled from the Yandex Maps page, since it is possible to search for almost any objects there.
When parsing the page was found JSON, it begins with the line {"request": {"args":
ends with the tag '</ script>' . It contains all the necessary data, including all found objects, their coordinates, routes, addresses, tags, and so on. It remains to obtain the necessary data so that in the future it can be used. We will do this using PHP.
So, as a result, we obtain an array containing the coordinates, the address and the names of the objects of the search. Then, convert it to JSON, which will then request the script with the map.

<?php $array_shops=array('_1','_2'); $coords_file=array(); $geo_objects=array(); for($i=0;$i<count($array_shops);$i++) { $coords_file[$i]=file_get_contents("http://maps.yandex.ru/?text=".urlencode($_GET['town']."  ".$array_shops[$i])); $start=strpos($coords_file[$i],'{"request":{"args":'); $end=strpos($coords_file[$i],'</script>',$start); $lenght=$end-$start; $js=json_decode(substr($coords_file[$i],$start,$lenght),1); $js=$js['vpage']['data']['businesses']['GeoObjectCollection']['features']; foreach($js as $value) { $geo_objects[]=array( 'coordinates' =>array($value['geometry']['coordinates'][1], $value['geometry']['coordinates'][0]), 'address'=>$value['properties']['CompanyMetaData']['address'], 'name_shop'=>$array_shops[$i] ); } } $js_array=json_encode($geo_objects); echo $js_array; ?> 


I will not describe in detail the code for the card itself, you can read in detail here .

 ymaps.ready(init); function init () { var myMap = new ymaps.Map("map", { //   center: [ymaps.geolocation.latitude, ymaps.geolocation.longitude], zoom: 12}) //   var town=ymaps.geolocation.city; var data_send=new Object(); data_send.town = town; var coords=[]; var address=[]; var names_shop=[]; var json_array; jQuery.ajax( { url:"json_response.php", data: data_send, dataType: 'json', success: function(data){ json_array=data; for(key in json_array){ coords.push(json_array[key].coordinates); address.push(json_array[key].address); names_shop.push(json_array[key].name_shop); } //  var myCollection = new ymaps.GeoObjectCollection({}, {preset: 'twirl#shopIcon'}); for (var i = 0; i < coords.length; i++){ myCollection.add(new ymaps.Placemark(coords[i], { balloonContentHeader:names_shop[i], balloonContent: address[i], })); } myMap.geoObjects.add(myCollection); } }); //  myMap.controls.add(new ymaps.control.ZoomControl()); myMap.controls.add('mapTools'); myMap.controls.add('typeSelector'); } 


Total:
we can search for objects by name, without a database with coordinates and addresses that must be maintained and updated.

The result can be found here . Hopefully, the ability to search for arbitrary objects will be added to the Yandex Maps API.
Thanks for attention.

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


All Articles