0

我不确定我是否使用了正确的术语,但这就是我想要做的......我有一个加载谷歌地图的 asp.net 页面,然后从创建的 JSON 数据中添加标记代码隐藏页面上的方法。最初加载时一切正常。但是,如果我通过单击按钮调用函数 refreshTheMarkers(),populateTheMap 函数(第三行代码)中的“卡车”变量不会得到更新的 JSON 字符串,它仍然是与页面相同的 JSON 字符串最初加载。有没有办法“强制”刷新 ConvertDataTabletoString 方法?

<script type="text/javascript">
    var map;
    var infoWindow;
    var markersArray = [];
    var sbarDiv;
    var trucks;

    function initialize() {
        var mapOptions = {
            center: new google.maps.LatLng(50.08, -82.8),
            zoom: 11,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };

        infoWindow = new google.maps.InfoWindow();
        map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
        sbarDiv = document.getElementById('sbDivMain');

        populateTheMap(true);
    }

    function populateTheMap(onStartup) {
        clearExistingMarkers();
        var sbarhtml = '';
        trucks = JSON.parse('<%=ConvertDataTabletoString() %>');
        var bounds = new google.maps.LatLngBounds();

        for (var i = 0; i < trucks.length; i++) {
            var truck = trucks[i];
            var point = new google.maps.LatLng(parseFloat(truck.Latitude), parseFloat(truck.Longitude));
            bounds.extend(point);

            var marker = createMarker(point, truck);
            var sidebarEntry = createSidebarEntry(marker, truck);
            sbarDiv.appendChild(sidebarEntry);
        }

        // Center the map only on startup
        if (onStartup) {
            var pointCenter = bounds.getCenter();
            var iZoomLevel = map.fitBounds(bounds);
            if (iZoomLevel > 15) { iZoomLevel = 15; }
            map.setCenter(pointCenter, iZoomLevel);
        }
    }

    function refreshTheMarkers() {
        clearExistingMarkers();
        populateTheMap(false);
    }

    function clearExistingMarkers() {
        if (markersArray) {
            for (i in markersArray) {
                markersArray[i].setMap(null);
            }
            markersArray.length = 0;
        }
        //clear side bar entries
        sbarDiv.innerHTML = '';

    }
// plus some additional code...

这是代码隐藏页面...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace LiveCadWebApp
{
    public partial class GPSTracking : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        // Convert the DataTable to a JSON string
        public string ConvertDataTabletoString()
        {
            DataTable dt = new DataTable();
            using (SqlConnection con = new SqlConnection(@"Server=.\SQLExpress;Database=MyDatabase;Trusted_Connection=True;"))
            {
                using (SqlCommand cmd = new SqlCommand("SELECT * FROM vwTruckGPSStatusInformation", con))
                {
                    con.Open();
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    da.Fill(dt);

                    return GetJSONFromDataTable(dt);
                }
            }
        }

基本上我想要做的是每隔几秒钟重新加载标记而不重新加载谷歌地图......

4

1 回答 1

0

您对 Web 方法进行 ajax 调用以获取更新的 json

检查这些示例 http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/

于 2013-09-06T03:40:34.733 回答