如何将数据存储值传递给javascript?我需要从gae数据存储中获取数据,并需要传递javascript

How to pass the datastore value to javascript? i need to get data from the gae datastore and need to pass javascript

本文关键字:数据 javascript 存储 gae 获取 值传      更新时间:2023-09-26

am使用webapp2、python处理GAE。现在我使用gae在谷歌地图上工作。已成功完成多个位置。

This is my JavaScript:
======================
<script type="text/javascript">
    var USER_POSITION;
    var DESTINATION_MARKER;
    var ALL_MARKERS = [];
    var coordinateArray = [
        {coordinates: new google.maps.LatLng(12.9345494,77.6115174), id: "forum", title: "Forum", address_details: Bengaluru<br/>Karnataka-560095"},
        {coordinates: new google.maps.LatLng(12.973584,77.611293), id: "central", title: "Central", address_details: Bengaluru<br/>Karnataka-560001"},
        {coordinates: new google.maps.LatLng(12.956534,77.701239), id: "busstop", title: "Marathahalli", address_details: Bengaluru<br/>Karnataka-560037"}
    ];
</script>

这里使用的是硬编码值。但是我需要传递我的数据存储值。如何将这些数据存储值传递给我的javascript。

This is my DBModel:
====================
class MapModel(db.Model):
 Lattitude=db.StringProperty()
 Landitude=db.StringProperty()
 Id=db.StringProperty()
 Title=db.StringProperty()
 Address=db.StringProperty()

请提供一些示例代码。

我的.js:

var mapProperty=new google.maps.Map(document.getElementById("example"),myOptions);

        for(var i in coordinateArray ) {
        var id = coordinateArray[i]['id'];
        var title = coordinateArray[i]['title'];
        var position = coordinateArray[i]['coordinates'];
        var address_details = coordinateArray[i]['address_details'];
        var boxText = [
                    '<div class="info_box">',
                        ..info details..
                    '</div>'
                    ].join(" ");
        var markerOptions = {
            animation: markerAnimation,
            position: coordinateArray[i]['coordinates'],
            icon: markerImage,
            map: mapProperty,
            html: boxText,
            id: id
        };

您可以使用webapp2模板将数据传递到HTML页面。

Python代码:

coordinates = MapModel.all()
template_values = {
    'coordinates': coordinates
}    
path = os.path.join(os.path.dirname(__file__), 'YourFile.html')
self.response.out.write(template.render(path, template_values))

在HTML页面中:

<script type="text/javascript">
    var USER_POSITION;
    var DESTINATION_MARKER;
    var ALL_MARKERS = [];
    var coordinateArray = [
        {% for cord in coordinates %}
            {coordinates: new google.maps.LatLng({{cord.Lattitude}},{{cord.Landitude}}), id: "{{cord.id}}", title: "{{cord.title}}", address_details: "{{cord.Address}}"},
        {% endfor %}
    ];
</script>