我在C#中创建了一个在Google地图上绘制标记的类,这个工作正常。现在我想用数据库中的第一个纬度和经度条目动态加载我的地图(目前我对它进行了硬编码)。但是,我似乎陷入困境。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
public static class GPSLib
{
public static String PlotGPSPoints(DataTable tblPoints)
{
try
{
String Locations = "";
String sJScript = "";
String distancescript = "";
String Latitude = "";
String Longitude = "";
int i = 0;
foreach (DataRow r in tblPoints.Rows)
{
// bypass empty rows
if (r["latitude"].ToString().Trim().Length == 0)
continue;
Latitude = r["latitude"].ToString();
Longitude = r["longitude"].ToString();
// create a line of JavaScript for marker on map for this record
Locations += Environment.NewLine + @"path.push(new google.maps.LatLng(" + Latitude + ", " + Longitude + @"));
var marker" + i.ToString() + @" = new google.maps.Marker({position: new google.maps.LatLng(" + Latitude + ", " + Longitude + @"),
title: 'You Have Visited This Place '
+ path.getLength(),map: map });
";
i++;
}
// construct the final script
sJScript = @"<script type='text/javascript'>
var poly;
var map;
var lat;
var lon;
function initialize() {
var cmloc = new google.maps.LatLng(34.0712,74.8106); // here i want map should load dynamicaly with first lat and lon entry.
var myOptions = {
zoom: 15,
center: cmloc,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById('map_canvas'), myOptions);
var polyOptions = {
strokeColor: 'blue',
strokeOpacity: 1.5,
strokeWeight: 3
}
poly = new google.maps.Polyline(polyOptions);
poly.setMap(map);
var path = poly.getPath();
" + Locations + @"
}
</script>";
return sJScript;
}
catch (Exception ex)
{
throw ex;
}
}
}