Google Maps API를 사용하여지도를 만드는 joomla 용 모듈을 만들려고합니다.joomla에서 javascript로 PHP 변수 전달하기
데이터가 javascript 파일에 하드 코드 된 경우 맵 자체가 제대로 작동하지만 여러 사이트에서 쉽게 사용할 수 있기를 원하므로 joomla 백엔드에서 모든 옵션을 설정하는 것이 좋습니다. JSON 호출로이 작업을 시도했지만 페이지가로드 될 때 오류 500을 반환합니다. 내 dev에 사이트는 다음과 같이 http://dev.xander.dk.web1.symatic.dk/
관련 코드는 다음과 같습니다
mod_google_maps.php (변수를 만들고 mod_google_maps.xml의 필드에 연결하기)
<?php
/**
* Hello World! Module Entry Point
*
* @package Joomla.Tutorials
* @subpackage Modules
* @link http://docs.joomla.org/J3.x:Creating_a_simple_module/Developing_a_Basic_Module
* @license GNU/GPL, see LICENSE.php
* mod_helloworld is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
*/
// no direct access
defined('_JEXEC') or die;
// Include the syndicate functions only once
require_once(dirname(__FILE__) . '/helper.php');
$mapCenterLat = $params->get('mapCenterLat', '55.395239');
$mapCenterLng = $params->get('mapCenterLng', '10.380180');
$zoom = $params->get('zoom', '18');
$mapDisableUi = $params->get('mapDisableUi', false);
$markerLat = $params->get('markerLat', '55.395239');
$markerLng = $params->get('markerLng', '10.380180');
$markerTitle = $params->get('markerTitle', 'symatic');
helper.php
<?php
function getMapOptionsAjax(){
$data = array('mapCenterLat' => $mapCenterLat, 'mapCenterLng' => $mapCenterLng, 'zoom' => $zoom, 'mapDisableUi' => $mapDisableUi, 'markerLat' => $markerLat, 'markerLng' => $markerLng, 'markerTitle' => $markerTitle);
echo json_encode($data);
};
?>
maps.js (단지 관련 JSON 호출)
jQuery.get('index.php?option=com_ajax&module=google_maps&method=getMapOptions&format=json', function(data){
var mapResponse = data;
console.log(mapResponse);
});
이 질문과 관련이 없으며 하드 코드 된 값으로 작동하기 때문에 나머지 maps.js 파일을 생략했습니다.
꽤 완벽했습니다. 두 가지 생각 : 먼저 스크립트의 모든 줄 끝에는 세미콜론이 필요합니다. 그렇지 않으면 작동하지 않습니다. 둘째, mapDisableUi는 true/false를 반환해야합니다. 즉, xml 파일에 할당 한 두 값입니다. 그러나 이것은 매우 유용했습니다. 괜찮 으면 나중에 참조 할 수 있도록이 작업과 같은 AJAX 호출 방법을 설명해 주시겠습니까? – xanderh
세미콜론에 @xanderh mybad하지만 방금 브라우저에 입력되었으므로 AJAX 호출과 Joomla는 [Joomla Q & A StackExchange 사이트에서이 응답을 보았습니다.] (http : //joomla.stackexchange.com/a/147/107). – Craig