在谷歌地图标记中添加文本

本文关键字:添加 文本 图标 谷歌 地图 | 更新日期: 2023-09-27 18:12:28

我已经设法在我的网站上使用谷歌地图的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>

地图的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。净

    <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,您将能够得到您想要做的。当用户点击地图标记时,可以在信息窗口中显示驱动程序名称。请看下面的链接。

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

你也会在这里找到很好的例子。希望有帮助