168 lines
4.5 KiB
JavaScript
168 lines
4.5 KiB
JavaScript
/*
|
|
* Let's GO!
|
|
*/
|
|
|
|
ymaps.ready(init);
|
|
var myMap;
|
|
var categories;
|
|
var points = Array();
|
|
|
|
function init() {
|
|
myMap = new ymaps.Map("map", {
|
|
center: [59.9, 30.3],
|
|
zoom: 9,
|
|
behaviors: ['default', 'scrollZoom']
|
|
});
|
|
|
|
myMap.controls.add('mapTools');
|
|
myMap.controls.add('typeSelector');
|
|
myMap.controls.add('smallZoomControl');
|
|
myMap.controls.add('scaleLine');
|
|
|
|
initCategories()
|
|
getPoints();
|
|
|
|
myMap.events.add('click', function (e) {
|
|
if (!myMap.balloon.isOpen()) {
|
|
if ( $('#addPointButton').hasClass('btn-success') )
|
|
{
|
|
coords = e.get('coordPosition');
|
|
$('#lat').val(coords[0].toPrecision(11));
|
|
$('#lng').val(coords[1].toPrecision(11));
|
|
$('#addPointModal').modal('show');
|
|
}
|
|
}
|
|
else {
|
|
myMap.balloon.close();
|
|
}
|
|
});
|
|
}
|
|
|
|
function getPoints()
|
|
{
|
|
$.ajax({
|
|
url: "/json/points/",
|
|
success: function(data){
|
|
for(var k in data) {
|
|
id = data[k]['id'];
|
|
points[id] = new ymaps.Placemark(
|
|
[data[k]['lat'], data[k]['lng']], {
|
|
hintContent: data[k]['name'],
|
|
id: id
|
|
},{
|
|
iconImageHref: data[k]['categoryIcon'],
|
|
iconImageSize: [32, 37],
|
|
openEmptyBalloon: true,
|
|
balloonCloseButton: false
|
|
});
|
|
|
|
points[id].events.add('click', function (e) {
|
|
showInfo(e.get('target').properties.get('id'));
|
|
});
|
|
myMap.geoObjects.add(points[id]);
|
|
}
|
|
}
|
|
});
|
|
|
|
return true;
|
|
}
|
|
|
|
function showInfo(id)
|
|
{
|
|
if (!(id in points) || points[id].properties.get('balloonContent') == undefined)
|
|
{
|
|
points[id].properties.set('balloonContent', 'Загрузка...');
|
|
$.ajax({
|
|
url: "/json/point/id/" + id,
|
|
success: function(data) {
|
|
description = '<div class="infoCard">' +
|
|
'<h3>' + data.name + '</h3>' +
|
|
'<img src="' + data.img + '" />' +
|
|
data.description +
|
|
' <a href="' + data.source + '" target="blank">Подробнее...</a></div>'
|
|
points[id].properties.set('balloonContent', description);
|
|
}
|
|
});
|
|
}
|
|
return true;
|
|
}
|
|
|
|
function initCategories()
|
|
{
|
|
$.ajax({
|
|
url: "/json/categories/",
|
|
success: function(data){
|
|
for(var k in data) {
|
|
categories = categories + '<option value="'+data[k]['id']+'">'+data[k]['name']+'</option>';
|
|
}
|
|
}
|
|
});
|
|
|
|
return true;
|
|
}
|
|
|
|
function searchAdderess()
|
|
{
|
|
var myGeocoder = ymaps.geocode($('#addressField').val());
|
|
myGeocoder.then(
|
|
function (res) {
|
|
myMap.setCenter(res.geoObjects.get(0).geometry.getCoordinates(), 13);
|
|
},
|
|
function (err) {}
|
|
);
|
|
}
|
|
|
|
function showMeOnTheMap()
|
|
{
|
|
var userLat = 0;
|
|
var userLng = 0;
|
|
navigator.geolocation.getCurrentPosition(function(pos) {
|
|
userLat = pos.coords.latitude;
|
|
userLng = pos.coords.longitude;
|
|
|
|
myMap.geoObjects.add(
|
|
new ymaps.Placemark(
|
|
[userLat, userLng],
|
|
{
|
|
balloonContentHeader: ymaps.geolocation.country,
|
|
balloonContent: ymaps.geolocation.city,
|
|
balloonContentFooter: ymaps.geolocation.region
|
|
},
|
|
{
|
|
iconImageHref: '/ico/man.png',
|
|
iconImageSize: [32, 37]
|
|
}
|
|
)
|
|
);
|
|
myMap.setCenter([userLat, userLng]);
|
|
}, function(){
|
|
userLat = ymaps.geolocation.latitude;
|
|
userLng = ymaps.geolocation.longitude;
|
|
|
|
myMap.geoObjects.add(
|
|
new ymaps.Placemark(
|
|
[userLat, userLng],
|
|
{
|
|
balloonContentHeader: ymaps.geolocation.country,
|
|
balloonContent: ymaps.geolocation.city,
|
|
balloonContentFooter: ymaps.geolocation.region
|
|
},
|
|
{
|
|
iconImageHref: '/ico/man.png',
|
|
iconImageSize: [32, 37]
|
|
}
|
|
)
|
|
);
|
|
myMap.setCenter([userLat, userLng]);
|
|
});
|
|
}
|
|
|
|
function tryAddPoint(el)
|
|
{
|
|
$(el).toggleClass('btn-success').toggleClass('btn-default');
|
|
if ( $('#addPointButton').hasClass('btn-success') )
|
|
myMap.cursors.push('crosshair');
|
|
else
|
|
myMap.cursors.push('arrow');
|
|
}
|