我正在尝试将数据从Spring控制器传递到JavaScript,但没有成功。我该使用Ajax来处理吗?请您能否给我一些提示如何处理?最好的方法是什么?
在我的控制器中,我尝试传递数据:
@RequestMapping(value = "/map", method = RequestMethod.GET)
public String map(ModelMap model) {
...
model.addAttribute("trackpoints", json);
return "map";
}
其中json是一个Gson对象(JsonObject),包含以下内容:
{"trackpoints":[{"latitude":52.390556,"longitude":16.920295},
{"latitude":52.390606,"longitude":16.920262}]}
在我的JSP文件中,我有以下内容:
<script type="text/javascript">
var myJSON = {};
myJSON = ${trackpoints};
document.writeln(myJSON.trackpoints);
</script>
但是结果是:
[object Object],[object Object]
我会详细解释一下:
我想使用Google Maps API来显示地图,并从许多纬度和经度中绘制路径坐标。
我认为JSON比列表更好,但我可能错了。
我试图调整文档中的代码-在下面的代码中,我尝试用循环替换硬编码坐标,并使用JSON对象中的值。
<script type="text/javascript">
function initialize() {
var myLatLng = new google.maps.LatLng(0, -180);
var myOptions = {
zoom : 3,
center : myLatLng,
mapTypeId : google.maps.MapTypeId.TERRAIN
};
var map = new google.maps.Map(document.getElementById("map_canvas"),
myOptions);
var flightPlanCoordinates = [
new google.maps.LatLng(37.772323, -122.214897),
new google.maps.LatLng(21.291982, -157.821856),
new google.maps.LatLng(-18.142599, 178.431),
new google.maps.LatLng(-27.46758, 153.027892) ];
var flightPath = new google.maps.Polyline({
path : flightPlanCoordinates,
strokeColor : "#FF0000",
strokeOpacity : 1.0,
strokeWeight : 2
});
flightPath.setMap(map);
}
</script>
我希望您现在更加清楚了:)