简体   繁体   English

在Google Map Markers中添加文字

[英]Add text in Google Map Markers

I have managed to get a google map on my site using Javascript api of google maps.. and it works great... 我已经成功地使用Google Maps的Javascript API在我的网站上获取了Google Map。

Each Location/vehicle has a marker which is great. 每个位置/车辆都有一个很棒的标记。 But the problem is I need to display A name in each marker. 但是问题是我需要在每个标记中显示一个名称。 I receive my data from sql and loading the latitudes and logitudes works great, I just need to change the marker. 我从sql接收数据,并且加载纬度和经度效果很好,我只需要更改标记即可。 From the database I get the Driver name, latitude and logitude. 从数据库中,我得到驱动程序名称,纬度和对数。 I need to display the Driver name in the marker. 我需要在标记中显示驱动程序名称。 These data would depend on a site (which is chosen from a dropdownlist). 这些数据将取决于站点(从下拉列表中选择)。 When a call is logged we need to send the nearest technition out to the client. 记录呼叫后,我们需要将最近的技术发送给客户。

This is what I have done: 这是我所做的:

My script in the head: 我的脚本在头:

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

My div for the 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>

And this is showing the map and adding the positions: 这将显示地图并添加位置:

            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> "; 
    }

I'm very new to javascript and google maps. 我是JavaScript和Google地图的新手。 If anybody can please lead me in the right direction I would really appreciate it! 如果有人可以引导我朝正确的方向前进,我将不胜感激!


ANSWER 回答

This is the solution: 这是解决方案:

Code Behind: 背后的代码:

     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 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>

Search for infowindows and you will be able to get what you want to do. 搜索信息窗口 ,您将能够获得想要的操作。 You can display driver name in infowindow when user click on your map marker. 用户单击地图标记时,可以在信息窗口中显示驱动程序名称。 See below links. 请参阅以下链接。

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

you will find good example here also. 您也会在这里找到很好的例子。 Hope this help :) 希望这个帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM