gpt4 book ai didi

c# - 在 Google map 标记中添加文本

转载 作者:行者123 更新时间:2023-12-03 12:16:33 28 4
gpt4 key购买 nike

我已经成功使用谷歌地图的 Javascript api 在我的网站上获取了谷歌地图..并且效果很好...

每个地点/车辆都有一个很棒的标记。但问题是我需要在每个标记中显示一个名称。我从 sql 接收数据并加载纬度和经度效果很好,我只需要更改标记。我从数据库中获取驾驶员姓名、纬度和经度。我需要在标记中显示驱动程序名称。这些数据取决于站点(从下拉列表中选择)。记录调用后,我们需要将最近的技术发送给客户。

这就是我所做的:

我的头部脚本:

     <script src="http://maps.google.com/maps?file=api&amp;v=2&amp;sensor=false
&amp;key=AIzaSyCRuaf5l3GKHKII8uG8TDUFW35xr34PgEw" type="text/javascript">
</script>

我的 map 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>

这显示了 map 并添加了位置:

            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>

最佳答案

搜索infowindows,您将能够得到您想要做的事情。当用户单击 map 标记时,您可以在信息窗口中显示驱动程序名称。请参阅下面的链接。

https://developers.google.com/maps/documentation/javascript/infowindows

您也可以在这里找到很好的例子。希望这有帮助:)

关于c# - 在 Google map 标记中添加文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24670752/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com