I'm not sure if I'm using the right terminology, but here is what I'm trying to do... I have an asp.net page that loads a google map and then add markers from JSON data that is created in a method on the code-behind page. Everything works fine when initially loaded. However, if I call the function refreshTheMarkers() via a button click, the 'trucks' variable in the populateTheMap function (third line of code) does not get an updated JSON string, it is still the same JSON string as when the page was initially loaded. Is there a way to "force" the ConvertDataTabletoString method to be refreshed?
<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...
Here is the code-behind page...
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);
}
}
}
Essentially what I want to do is to reload the markers every couple of seconds without reloading the google map...