我已经设法使用谷歌地图的Javascript API在我的网站上获得谷歌地图..它的效果很好......
每个位置/车辆都有一个很棒的标记。但问题是我需要在每个标记中显示一个名称。我从sql接收数据并加载纬度和logitude工作得很好,我只需要更改标记。从数据库中我得到驱动程序名称,纬度和逻辑。我需要在标记中显示驱动程序名称。这些数据取决于站点(从下拉列表中选择)。记录通话时,我们需要将最近的技术发送给客户。
这就是我所做的:
我的剧本:
<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>
答案 0 :(得分:1)
搜索 infowindows ,您将能够得到您想要做的事情。当用户单击地图标记时,您可以在infowindow中显示驱动程序名称。 见下面的链接。
https://developers.google.com/maps/documentation/javascript/infowindows
你也会在这里找到好的例子。 希望这有帮助:)