我有三个Javascript函数:
功能1)从XML文件加载谷歌地图及其标记,并将其传递给功能2.
功能2)使用功能1中的数据在地图上创建标记。
功能3)使用Ajax对数据库的请求加载连接到标记/事件的用户。
我在函数2中调用函数3,这样得到字符串函数3产生:
var memberslist = showEventMembers(MapTitle)
但是当我引用字符串时,字符串undefined
仅打印到HTML。我不明白为什么函数3中的值没有传递给函数2中的var memberlist
。
函数3返回一个用户字符串,我已使用document.getElementById("event_members").innerHTML = this.responseText;
确认了这一点,并更改了我放在脚本标记之外的div对象的文本。但是,它不能更改我在函数2中生成的span标记的html文本。
所以我需要的是将函数3的返回值传递给函数2中脚本生成的HTML标记,但是我无法使用它来替换HTML,或者将值传递给变量并使用它生成的HTML中的变量。
希望我的问题很清楚,英语不是我的第一语言,非常感谢任何帮助!
(我使用jQuery和Laravel,如果它有任何重要性,那么PHP就在Laravel"控制器")。
功能1 /加载地图和标记数据:
$(document).ready(function() {
var mapCenter = new google.maps.LatLng(47.6145, -122.3418); //Google map Coordinates
map_initialize(); // initialize google map
function map_initialize() {
var googleMapOptions = {
center: mapCenter, // map center
zoom: 17, //zoom level, 0 = earth view to higher value
panControl: true, //enable pan Control
zoomControl: true, //enable zoom control
zoomControlOptions: {
style: google.maps.ZoomControlStyle.SMALL //zoom control size
},
scaleControl: true, // enable scale control
mapTypeId: google.maps.MapTypeId.ROADMAP // google map type
};
map = new google.maps.Map(document.getElementById("map"), googleMapOptions);
//var map = new google.maps.Map(document.getElementById('map'), {
// zoom: 4,
// center: {lat: -25.363882, lng: 131.044922 }
//});
//Load Markers from the XML File, Check (/laravel/stumbler/stumbler/public/phppages/map_process.php)
$.get("/laravel/stumbler/stumbler/markerxml", function(data) {
$(data).find("marker").each(function() {
//Get user input values for the marker from the form
var author = $(this).attr('author');
var name = $(this).attr('name');
var address = '<p>' + $(this).attr('address') + '</p>';
var type = $(this).attr('type');
var point = new google.maps.LatLng(parseFloat($(this).attr('lat')), parseFloat($(this).attr('lng')));
//call create_marker() function for xml loaded maker
create_marker(author, point, name, address, false, false, false, "/laravel/stumbler/stumbler/public/images/icons/pin_blue.png");
});
});
//drop a new marker on right click
google.maps.event.addListener(map, 'rightclick', function(event) {
//Edit form to be displayed with new marker
var EditForm = '<p><div class="marker-edit">' +
'</div></p>';
//call create_marker() function
create_marker('', event.latLng, 'Exiting event!', 'Log in or Sign up to create an event here :)', true, true, true, "/laravel/stumbler/stumbler/public/images/icons/pin_green.png");
});
}
});
功能2 /创建标记并放在地图上
function create_marker(MapAuth, MapPos, MapTitle, MapDesc, InfoOpenDefault, DragAble, Removable, iconPath) {
//new marker
var marker = new google.maps.Marker({
position: MapPos,
map: map,
draggable: DragAble,
animation: google.maps.Animation.DROP,
title: "Hello World!",
icon: iconPath
});
showEventMembers(MapTitle);
var memberslist = showEventMembers(MapTitle);
//Content structure of info Window for the Markers
var contentString = $('<div class="marker-info-win">' +
'<div class="marker-inner-win"><span class="info-content">' +
'<h1 class="marker-heading">' + MapTitle + '</h1>' +
MapDesc + '<p><strong>Creator: </strong>' + MapAuth + '</p>' +
'</span>' +
'Event members: ' + '<span id="event_members">This text is not being replaced</span><br>' +
memberslist + //this returns the string "undefined"
'<p><br>Sign up or Log in to join this event!</p>' +
'</div></div>');
//Create an infoWindow
var infowindow = new google.maps.InfoWindow();
//set the content of infoWindow
infowindow.setContent(contentString[0]);
// add click listener for the marker
google.maps.event.addListener(marker, 'click', function() {
infowindow.open(map, marker); // click on marker opens info window
});
if (InfoOpenDefault) //whether info window should be open by default
{
infowindow.open(map, marker);
}
}
功能3 /加载与活动相关的用户
function showEventMembers(str) {
if (str == "") {
document.getElementById("event_members").innerHTML = "";
return;
} else {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("event_members").innerHTML = this.responseText;
return this.responseText;
}
};
xmlhttp.open("GET", "/laravel/stumbler/stumbler/get_event_members/" + str);
xmlhttp.send();
}
}
PHP(返回用户和标记连接)
public function get_event_members($eventname){
$signed_up_users = DB::table('users_to_events')->where('event_name', '=', $eventname)->get();
$userlist ="";
foreach ($signed_up_users as $signed_up_user)
{
$userlist .= $signed_up_user->username.'<br>';
}
exit($userlist);}