こんにちは、drupal 7 で ajax を使用して、マップの境界からデータを取得し、それを文字列としてシリアル化し、サーバーへの投稿として送信して、ビューのフィルターとして追加し、表示をズームと配列の境界の変更に応答させます。
成功のコールバックが呼び出されますが、返されるデータはページ コールバック関数の結果ではなく、firebug の xhtml 応答は Web ページ全体です。ページ コールバック関数の値が返されない原因を知っていただければ幸いです。
以下はJavaScriptコードです:
(function ($) {
Drupal.behaviors.mapresults = {
attach: function() {
// Dynamically show the marker nodes
// first find the right map
$.each(Drupal.settings.getlocations, function (key, settings) {
// this is the one we want
alert("0");
if (1 == 1) {
// an event handler on map zoom to check if the view results needs to be updated
on zoom
// hooking another event and checking zoom may work better.
google.maps.event.addListener(getlocations_map[key], 'bounds_changed', function()
{
// count number of markers <= 10'
// global_settings.mgr
//mgr = new MarkerManager(getlocations_map[key]);
//zoomcount = mgr.getMarkerCount(getlocations_map[key].getZoom());
zoomcount = 10;
if(zoomcount < 100){
// get bounds
marker_bounds = getlocations_map[key].getBounds();
// calculate horizontal distance from bounds to get max and min latitude and
longitude for the for box
// submit data with jquery get bounds etc.
var marker_bounds_string = JSON.stringify(marker_bounds);
// ajax link to submit data by post
$.ajax({
type: 'POST',
url: 'ajax/mapresults',
dataType: "json",
data : {marker_bounds : marker_bounds_string},
success: function (data)
{document.getElementById('map_results').innerHTML = "success";},
error: function (xmlhttp)
{document.getElementById('map_results').innerHTML = xmlhttp.status;}
});
}
else{
}
});
}
});
}
};
}(jQuery));
drupal モジュールのコードは次のとおりです。
<?php
function mapresults_menu() {
$items['ajax/mapresults'] = array(
'title' => t('mapresults AJAX'),
'type' => MENU_CALLBACK,
'page callback' => 'mapresults_ajax',
'access arguments' => array('access content'),
);
return $items;
}
function mapresults_ajax() {
// call back for display map options
drupal_exit();
$view = views_get_view('search');
// set display
$display_id = 'default';
// add filters from ajax
// float
$bounds_array = json_decode($_POST['marker_bounds'])
//get cordinates from bounds_array
$lat_min = $bounds_array[0][0];
$lat_max = $bounds_array[1][0];
$lon_min = $bounds_array[0][1];
$lon_max = $bounds_array[1][1];
// add filters to view
$view->exposed_input['ubicacion_latitiude'] = $lat_min;
$view->exposed_input['ubicacion_latitiude_1'] = $lat_max;
$view->exposed_input['ubicacion_longitude'] = $lon_min;
$view->exposed_input['ubicacion_longitude_1'] = $lon_max;
$view->pre_execute();
$view->execute();
// display ajax results in container
$data = $view->preview($display_id);
$results_count = count($view->result);
if($results_count <= 10){
// Return json
drupal_json_output($data);//
drupal_exit();
}
else{
drupal_json_output("ih");
drupal_exit();
}
}
?>