我理解用joomla创建组件的过程,遇到了一个问题。
问题如下。通过objectManager在Yandex地图上打印标签,因此有必要以JSON格式从网站检索数据。
为了测试do-view.josn.php(匆忙中,然后将其存储在控制器中)结论:http://test.joomlazen.com/index.php?option=com_jz_location&视图=ymap&format=json
代码:
class JZLocationViewYmap extends JViewLegacy {
public function getMapObject($data) {
$item = new stdClass();
$item->type = 'Feature';
$item->id = $data->id;
$item->geometry = new stdClass();
$item->geometry->type = 'Point';
$item->geometry->coordinates = '[55.831903,37.411961]';
$item->properties = new stdClass();
$item->properties->balloonContent = 'Content';
$item->properties->clusterCaption = 'Cluste';
$item->properties->hintContent = 'Hint';
return $item;
}
public function getMapFeatures() {
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query
->select('*')
->from($db->quoteName('#__k2_items','a'))
->group($db->quoteName('a.id'))
->order('a.id DESC')
;
$db->setQuery($query);
$rows = $db->loadObjectList();
if ($rows) {
foreach ($rows as $row) {
$main[] = $this->getMapObject($row);
}
return $main;
}
return false;
}
public function display($tpl = NULL) {
$app = JFactory::getApplication();
$app->setHeader('Content-Type', 'application/json; charset=utf-8');
$data = new stdClass();
$data->type = "FeatureCollection";
$data->features = $this->getMapFeatures();
echo json_encode($data);
}
因此,为方便使用一个简单的html而创建的输出(示例取自Yandex MAP上的Sandbox)P.S网站允许访问控制来源:"*"
<!DOCTYPE html>
<html>
<head>
<title>Примеры. Добавление на карту большо числа объектов</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- Если вы используете API локально, то в URL ресурса необходимо указывать протокол в стандартном виде (http://...)-->
<script src="http://api-maps.yandex.ru/2.1/?lang=ru-RU" type="text/javascript"></script>
<script src="http://yandex.st/jquery/2.1.0/jquery.min.js" type="text/javascript"></script>
<script>
ymaps.ready(init);
function init () {
var myMap = new ymaps.Map('map', {
center: [55.831903,37.411961],
zoom: 10
}, {
searchControlProvider: 'yandex#search'
}),
objectManager = new ymaps.ObjectManager({
// Чтобы метки начали кластеризоваться, выставляем опцию.
clusterize: true,
// ObjectManager принимает те же опции, что и кластеризатор.
gridSize: 32
});
// Чтобы задать опции одиночным объектам и кластерам,
// обратимся к дочерним коллекциям ObjectManager.
objectManager.objects.options.set('preset', 'islands#greenDotIcon');
objectManager.clusters.options.set('preset', 'islands#greenClusterIcons');
myMap.geoObjects.add(objectManager);
$.ajax({
url: 'http://test.joomlazen.com/index.php?option=com_jz_location&view=ymap&format=json'
}).done(function(data) {
objectManager.add(data);
});
}</script>
<style>
html, body, #map {
width: 100%; height: 100%; padding: 0; margin: 0;
}
</style>
</head>
<body>
<div id="map"></div>
</body>
</html>
老实说,Ajax和JSON不是很友好。所以请帮助理解。提前感谢,很抱歉我的英语使用谷歌翻译
通过添加控制器解决了问题。控制器代码
class JZLocationControllerYmap extends JControllerForm {
function __construct($config = array()) {
parent::__construct( $config );
}
public function getMapObject($data) {
$item = new stdClass();
$item->type = 'Feature';
$item->id = $data->id;
$item->geometry = new stdClass();
$item->geometry->type = 'Point';
$item->geometry->coordinates = array(55.831903,37.411961);
$item->properties = new stdClass();
$item->properties->balloonContent = 'Content';
$item->properties->clusterCaption = 'Cluste';
$item->properties->hintContent = 'Hint';
return $item;
}
public function getMapFeatures() {
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query
->select('*')
->from($db->quoteName('#__k2_items','a'))
->group($db->quoteName('a.id'))
->order('a.id DESC')
;
$db->setQuery($query,'0','1000000');
$rows = $db->loadObjectList();
if ($rows) {
foreach ($rows as $row) {
$main[] = $this->getMapObject($row);
}
return $main;
}
return false;
}
public function some() {
$app = JFactory::getApplication();
$app->setHeader('Content-Type', 'application/json; charset=utf-8');
$data = new stdClass();
$data->type = "FeatureCollection";
$data->features = $this->getMapFeatures();
echo json_encode($data);
}
此外,坐标也有错误
$item->geometry->coordinates = '[55.831903,37.411961]';
它们必须是阵列
$item->geometry->coordinates = array(55.831903,37.411961);