我有一个网站,用户可以从下拉菜单中选择一个值。单击按钮后,带有ajax的请求被发送到服务器,因此我得到一些文本(我使用ajax,因为我不希望页面刷新)。 当我想使用googlemaps api以便与文本一起获得带有一些标记的地图时,会出现问题。我试图在$ .ajax的成功函数中添加相应的代码来创建标记,但它似乎不起作用。其实我不知道这是不是正确的方法。
我将在此处发布我在成功函数中添加的部分代码:
$.ajax({
type:'POST',
url:'activities_code.php',
data: {datastr:datastr, datastr1:datastr1},
success:function(response){
$("#msg").html(response);
map = new google.maps.Map(document.getElementById("map"), {
center: new google.maps.LatLng(0, 0),
zoom: 14,
mapTypeId: google.maps.MapTypeId.ROADMAP,
mapTypeControl: false,
mapTypeControlOptions: {
style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR
},
navigationControl: true,
navigationControlOptions: {
style: google.maps.NavigationControlStyle.SMALL
}
});
<?php
$category = $_POST['category'];
$query = mysql_query("SELECT activities,city,latt,lonn FROM activities where activities='$category'");
while ($row = mysql_fetch_array($query)){
$name=$row['activities'];
$lat=$row['latt'];
$lon=$row['lonn'];
$desc=$row['city'];
echo ("addMarker($lat, $lon,'<b>$activities</b><br/>$city');\n");
}
?>
center = bounds.getCenter();
map.fitBounds(bounds);
}
});
});
});
在php部分我回声
echo ("addMarker($lat, $lon,'<b>$activities</b><br/>$city');\n");
addMarker就是这个函数:
function addMarker(lat, lng, info) {
var pt = new google.maps.LatLng(lat, lng);
bounds.extend(pt);
var marker = new google.maps.Marker({
position: pt,
icon: icon,
map: map
});
var popup = new google.maps.InfoWindow({
content: info,
maxWidth: 300
});
google.maps.event.addListener(marker, "click", function() {
if (currentPopup != null) {
currentPopup.close();
currentPopup = null;
}
popup.open(map, marker);
currentPopup = popup;
});
google.maps.event.addListener(popup, "closeclick", function() {
map.panTo(center);
currentPopup = null;
});
}