【问题标题】:how to add locations(markers) on a google map from database..?如何从数据库中的谷歌地图上添加位置(标记)..?
【发布时间】:2012-11-20 14:14:47
【问题描述】:

我正在创建一个谷歌地图,以显示一个品牌在印度的商店位置...

我已经在我的 SQL Server 数据库中存储了完整的地址,包括纬度和经度...

在这里我可以从数据库中获取地址并显示在我的网页中,但我被困住了,将地址映射到谷歌地图上......

这里我保留了一个文本框,以便用户输入他的州/城市/任何位置名称,然后单击搜索按钮,然后我必须用标记在地图上标记相应的商店位置..

这是我的谷歌地图代码...

<script type="text/javascript">
    var infowindow = null;
    function initialize() {
        //var centerMap = new google.maps.LatLng(13.040547,80.230805);
        var centerMap = new google.maps.LatLng(12.264864, 77.937012);
        var myOptions = {
            zoom: 7,
            center: centerMap,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        }
        var map = new google.maps.Map(document.getElementById("map"), myOptions);
        setMarkers(map, sites);
        infowindow = new google.maps.InfoWindow({ });
    }

    //var sites = '<%=TextBox1.Text%>'
    //alert(sites);
    //[
    //// ['Ambattur', 13.119438,80.148182, 4, 'Ambattur, Chennai, Tamil Nadu, India, 600031'],['Avadi', 13.124453,80.101662,   1, 'Avadi, Tamil Nadu, India, 600017'],  ['Jayanagar', 12.928945,77.590599, 1, 'Jayanagar, Bangalore, Karnataka, India,     560041'],['Indira Nagar', 12.973697,77.641325, 1, 'Indira Nagar, Bangalore, Karnataka, India, 560038'],['TamilNadu',     11.415418,78.662109, 1, 'TamilNadu, India, 600017']
    //];

    function setMarkers(map, markers) {
        for (var i = 0; i < markers.length; i++) {
            var sites = markers[i];
            var siteLatLng = new google.maps.LatLng(sites[1], sites[2]);
            var marker = new google.maps.Marker({
                position: siteLatLng,
                map: map,
                icon: new google.maps.MarkerImage(
                'Images/R.png ',
                null, null, new google.maps.Point(0, 42)),
                title: sites[0],
                zIndex: sites[3],
                html: sites[4]
            });

            google.maps.event.addListener(marker, "click", function () {
                infowindow.setContent(this.html);
                infowindow.open(map, this);
            });
        }
    }
</script>

在这里,我有站点变量,所以我可以在地图上映射我的位置,

但在这里我需要从数据库中映射它们...

任何帮助将不胜感激...

谢谢 夏尔·阿里·谢克

【问题讨论】:

    标签: c# javascript asp.net sql-server google-maps


    【解决方案1】:

    乍一看,您的代码似乎没问题。但这是我的代码,对我来说非常好用,可能对你有帮助。

    myLatlng = new google.maps.LatLng(32.54681317351514, 15.1171875);

    map = new google.maps.Map(document.getElementById("gmap"), {mapTypeId: google.maps.MapTypeId.ROADMAP,zoom:2,center:myLatlng});

    /* loop which plot the markers on the map*/
    // record[i] holds lat&lang in this format'32.54681317351514, 15.1171875'
    
        var points=record[i].geo_location.split(',');
        marker = new google.maps.Marker({
                     position: new google.maps.LatLng(points[0],points[1]),
                     draggable: false,
                     map: map
                     });
    

    【讨论】:

      【解决方案2】:

      Guruparan Giritharan..

      正如我在我的问题中所说的,我想在印度各地找到商店.. 疼痛的详细信息将存储在数据库中.. 现在,我可以获取商店详细信息并通过您的代码在地图上标记他们的位置.. 早些时候,它适用于 pincode,从某种意义上说,如果您在搜索框中提供 pincode,它将检索相应的 店铺详情..

      现在,根据我的代码,在我对您的博客文章的回复中,我只能在文本框中输入字符串值。这里我还需要为数值(pincode)实现它,

      这是我的更新面板和计时器点击事件的代码..

      protected void UpdatePanel1_Load(object sender, EventArgs e)
          {
              //Linq is used to load the table to the code
              DataClassesDataContext data = new DataClassesDataContext();
      
              //Select all from the table
              //List<addressmap> lst = (from u in data.addressmaps select u).ToList();
              List<addressmap> lst = (from u in data.addressmaps where u.StoreLocation == TxtSearch.Text || u.StoreCity == TxtSearch.Text || u.StoreState == TxtSearch.Text || u.StoreCountry == TxtSearch.Text select u).ToList();
      
      
              //add the table contents to the javascript array so that new locations will be loaded
              foreach (addressmap item in lst)
              {
                  ScriptManager.RegisterArrayDeclaration(UpdatePanel1, "infoarray", "'" + item.StoreAddress.ToString() + "'");
                  ScriptManager.RegisterArrayDeclaration(UpdatePanel1, "lonarray", item.StoreLongitude.ToString());
                  ScriptManager.RegisterArrayDeclaration(UpdatePanel1, "latarray", item.StoreLatitude.ToString());
                  ScriptManager.RegisterArrayDeclaration(UpdatePanel1, "bounce", item.StoreBounce.ToString());
              }
          }
      
          protected void Timer1_Tick(object sender, EventArgs e)
          {
              //update the update panel every 10 seconds
              UpdatePanel1.Update();
          }
      

      在 select 语句中,我也需要 pincode..

      任何帮助将不胜感激..

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-11-13
        • 1970-01-01
        • 1970-01-01
        • 2016-03-01
        • 2013-09-01
        • 1970-01-01
        • 2012-05-06
        相关资源
        最近更新 更多