2016-05-10 10 views
0

Ich verstehe mit der Erstellung von Komponenten mit Joomla und lief auf ein Problem.Joomla JSON Ausgabe AJAX

Das Problem ist wie folgt. Drucken Sie die Etiketten auf der Yandex-Karte über den ObjectManager, deshalb müssen Sie die Daten von der Site im JSON-Format abrufen.

Zum Test tat view.josn.php (in Eile, dann speichern Sie es in der Steuerung) Das sehr Fazit: 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); 
} 

Und dementsprechend die Ausgabe erstellt für die Bequemlichkeit eines einfachen hTML (Beispiel aus der Sandbox auf Yandex MAP genommen) PS hat die Website die 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> 
 
\t <style> 
 
     html, body, #map { 
 
      width: 100%; height: 100%; padding: 0; margin: 0; 
 
     } 
 
    </style> 
 
</head> 
 
<body> 
 
<div id="map"></div> 
 
</body> 
 
</html>

Um ehrlich zu sein mit Ajax und JSON ist nicht sehr freundlich. also bitte hilf mit zu verstehen. Vielen Dank im Voraus und Entschuldigung für mein Englisch mit Google Übersetzer

Antwort

0

Das Problem wurde durch Hinzufügen eines Controllers gelöst. 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); 

} 

Auch war es ein Fehler in den Koordinaten

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

Sie müssen ein Array sein

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