我已经设法使用谷歌地图的 Javascript api 在我的网站上获得了谷歌地图.. 它工作得很好......
每个位置/车辆都有一个很棒的标记。但问题是我需要在每个标记中显示一个名称。我从 sql 接收数据并加载纬度和对数效果很好,我只需要更改标记。从数据库中,我得到驱动程序名称、纬度和对数。我需要在标记中显示驱动程序名称。这些数据将取决于站点(从下拉列表中选择)。记录呼叫时,我们需要将最近的技术发送给客户端。
这就是我所做的:
我的剧本在头脑中:
<script src="http://maps.google.com/maps?file=api&v=2&sensor=false
&key=AIzaSyCRuaf5l3GKHKII8uG8TDUFW35xr34PgEw" type="text/javascript">
</script>
我的地图 div:
<asp:Panel ID="Panel1" runat="server">
<asp:Literal ID="js" runat="server"></asp:Literal>
<div id="map_canvas" style="width: 400px; height: 400px; margin-bottom: 2px;">
</div>
<br />
</asp:Panel>
这是显示地图并添加位置:
private void BuildScript(DataTable tbl)
{
string custLocationLat, custLocationLong;
String Locations = "";
string marker;
if (lblCustLatitude.Text == "")
custLocationLat = Convert.ToString(-26.172116);
else
custLocationLat = Convert.ToString(lblCustLatitude.Text);
if (lblCustLongtitude.Text == "")
custLocationLong = Convert.ToString(28.130557);
else
custLocationLong = Convert.ToString(lblCustLongtitude.Text);
marker = "var = new GMarker(new GPoint(" + custLocationLat + ", " + custLocationLong + "));";
foreach (DataRow r in tbl.Rows)
{
if (r["Latitude"].ToString().Trim().Length == 0)
continue;
string Latitude = r["Latitude"].ToString();
string Longitude = r["Longitude"].ToString();
string markerPoints = r["DrivName"].ToString();
int i= 0;
Locations += Environment.NewLine + " map.addOverlay(new GMarker(new GLatLng(" + Latitude + "," + Longitude + ")));";
marker += Environment.NewLine + "var" + i + " = new GMarker(new GPoint(" + Latitude + ", " + Longitude + "));";
i++;
}
js.Text = @"<script type='text/javascript'>
function initialize() {
if (GBrowserIsCompatible()) {
var map = new GMap2(document.getElementById('map_canvas'));
map.setCenter(new GLatLng(" + custLocationLat + "," + custLocationLong + @"), 8);
" + Locations + @";
map.setUIToDefault();
}
}
</script> ";
}
我对 javascript 和谷歌地图很陌生。如果有人能引导我朝着正确的方向前进,我将不胜感激!
回答
这是解决方案:
代码背后:
public class Marker
{
public string title;
public string lat;
public string lng;
public string description;
}
private void BuildScript(DataTable tbl)
{
string custLocationLat, custLocationLong;
List<Marker> valueList = new List<Marker>();
if (lblCustLatitude.Text == "")
custLocationLat = Convert.ToString(-26.172116);
else
custLocationLat = Convert.ToString(lblCustLatitude.Text);
if (lblCustLongtitude.Text == "")
custLocationLong = Convert.ToString(28.130557);
else
custLocationLong = Convert.ToString(lblCustLongtitude.Text);
Marker center = new Marker { title = "Smart Office", lat = custLocationLat, lng = custLocationLong, description = "0" };
List<Marker> marker = new List<Marker>();
foreach (DataRow r in tbl.Rows)
{
if (r["Latitude"].ToString().Trim().Length == 0)
continue;
string Latitude = r["Latitude"].ToString();
string Longitude = r["Longitude"].ToString();
string driver = r["DrivName"].ToString();
string distance = r["Distance"].ToString();
marker.Add(new Marker { title = driver, lat = Latitude, lng = Longitude, description = distance });
}
JavaScriptSerializer ser = new JavaScriptSerializer();
hdnMarkers.Value = ser.Serialize(marker);
}
APS.Net
<script type="text/javascript">
var markers = document.getElementById("<%=hdnMarkers.Value%>").value;
function initialize()
{
var mapOptions = {
center: new google.maps.LatLng(markers[0].lat, markers[0].lng),
zoom: 8,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var infoWindow = new google.maps.InfoWindow();
var map = new google.maps.Map(document.getElementById("dvMap"), mapOptions);
var poly = new google.maps.Polyline({ map: map, strokeColor: '#FF8200' });
var lat_lng = new Array();
for (i = 0; i < markers.length; i++)
{
var data = markers[i]
var myLatlng = new google.maps.LatLng(data.lat, data.lng); lat_lng.push(myLatlng);
var marker = new StyledMarker({
styleIcon: new StyledIcon(StyledIconTypes.BUBBLE, { color: "2590BA", text: data.title }),
position: myLatlng, map: map, title: data.title
});
(function (marker, data)
{
google.maps.event.addListener(marker, "click", function (e) {
infoWindow.setContent(data.description); infoWindow.open(map, marker);
});
})(marker, data);
}
} </script>