-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
98 lines (86 loc) · 3.98 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<!DOCTYPE html>
<head>
<title>yandex geocode</title>
<meta http-equiv='x-ua-compatible' content='IE=edge' />
<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
<style>
.layer1 {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
</style>
<script src='https://api-maps.yandex.ru/2.1/?lang=ru_RU' type='text/javascript'></script>
<script type='text/javascript'>
var Map;
var myObjects;
ymaps.ready(init);
function init()
{
Map = new ymaps.Map('map', { center: [55.76, 37.64], zoom: 7 });
myObjects = ymaps.geoQuery(Map.geoObjects);
}
function AddGeoObject(index, encodingtxt)
{
var geoObject = myObjects.get(index);
if (geoObject != undefined && encodingtxt == "")
{
geoObject.options.set('visible', false);
SetupMap();
return;
}
var Geocoder = ymaps.geocode(encodingtxt, { results: 1 });
Geocoder.then(function (res)
{
var firstGeoObject = res.geoObjects.get(0),
coords = firstGeoObject.geometry.getCoordinates(),
// Область видимости геообъекта.
bounds = firstGeoObject.properties.get('boundedBy');
// Настраиваем геообъект: Получаем строку с адресом и выводим в иконке геообъекта.
firstGeoObject.options.set('preset', 'islands#darkBlueDotIconWithCaption');
firstGeoObject.options.set('visible', true);
firstGeoObject.properties.set('iconCaption', firstGeoObject.getAddressLine());
firstGeoObject.properties.set('id', index);
// Добавляем новый на карту или замещаем найденным геообъектом коллекцию
if (geoObject != undefined)
Map.geoObjects.splice(index, 1, firstGeoObject);
else
Map.geoObjects.add(firstGeoObject);
UpdateIcon(index);
SetupMap();
},
function (err)
{
alert('Произошла ошибка геокодирования.');
}
);
}
function SetupMap()
{
//Масштабируем карту на области видимости всех видимых объектов
myObjects = ymaps.geoQuery(Map.geoObjects);
var geoVisibleResult = myObjects.search('options.visible = true');
//если геообект один то установим вручную центр и масштаб = 15
if (geoVisibleResult.getLength() == 1)
Map.setCenter(geoVisibleResult.get(0).geometry.getCoordinates(), 15);
else
Map.setBounds(geoVisibleResult.getBounds());
}
function UpdateIcon(index)
{
Map.geoObjects.each(function (obj) {
var indexObj = obj.properties.get('id');
if (indexObj == index)
obj.options.set("iconColor", "red");
else
obj.options.unset("iconColor");
})
}
</script>
</head>
<body>
<div id='map' class='layer1'></div>
</body>
</html>