doubo9799 2016-05-10 09:47
浏览 95

Joomla JSON输出AJAX

I understand with the creation of components with joomla and ran into a problem.

The problem is as follows. Print labels on Yandex map through objectManager, therefore it is necessary to retrieve data from the site in JSON format.

To test did view.josn.php (in haste, then store it in the controller) The very conclusion: http://test.joomlazen.com/index.php?option=com_jz_location&view=ymap&format=json

Code:

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);
}

And, accordingly, the output created for the convenience of a simple html (Example taken from the Sandbox on Yandex MAP) P.S the website made the Access-Control-Allow-Origin: "*"

<!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>

To be honest with Ajax and JSON is not very friendly. so please help to understand. Thanks in advance and sorry for my English using google translator

</div>
  • 写回答

1条回答 默认 最新

  • dosrmo0442 2016-05-10 19:00
    关注

    The problem was solved by adding a controller. Controller code

    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);
    
    }
    

    Also, there was an error in the coordinates

    $item->geometry->coordinates = '[55.831903,37.411961]';
    

    They must be an array

        $item->geometry->coordinates = array(55.831903,37.411961);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)