2013-11-26 81 views
-2

嗨我有一個數據表,我用JavaScriptSerializer。如何在覆蓋現有地圖時創建靜態標記?

http://pastebin.com/Tz5icNzz

public string Branches() 
    { 
    DataTable dt = new DataTable(); 

    using (SqlConnection con = new SqlConnection("Data Source=localhost;Initial Catalog=DB;User ID=sa;Password=")) 

    { 


    new SqlCommand("Select title=BranchName,lat=Lat,lng=Lng,city=City,BranchID=BranchID 

    from Dealer where Lat >30", con)) 


    con.Open(); 
    SqlDataAdapter da = new SqlDataAdapter(cmd); 
    da.Fill(dt); 

    System.Web.Script.Serialization.JavaScriptSerializer serializer = new 

    System.Web.Script.Serialization.JavaScriptSerializer(); 

    List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>(); 

    Dictionary<string, object> row; 
    foreach (DataRow dr in dt.Rows) 

    { 

    row = new Dictionary<string, object>(); 

    foreach (DataColumn col in dt.Columns) 

    { 

    row.Add(col.ColumnName, dr[col]); 

    } 

    rows.Add(row); 

    } 

    return serializer.Serialize(rows); 

     } 
     } 

    } 

.aspx的:

我用這個數據表創建市場上贏得。

http://pastebin.com/Uy88hVBJ

<body onload="initialize()"> 



    <script type="text/javascript"> 

var markers = JSON.parse('<%=Branches() %>'); 

    var mapOptions = { 


    center: new google.maps.LatLng(markers[0].lat, markers[0].lng), 
    zoom: 7, 
    mapTypeId: google.maps.MapTypeId.ROADMAP 

    }; 

    var infoWindow = new google.maps.InfoWindow(); 

    var map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions); 


     for (i = 0; i < markers.length; i++) { 
      var data = markers[i] 


      var image = '/Images/branches.png'; 
      var myLatlng = new google.maps.LatLng(data.lat, data.lng); 
      var marker = new google.maps.Marker({ 
       position: myLatlng, 
       map: map, 
       title: data.title + " : " + data.city, 

       icon: image, 

      } 


       ); 



(function (marker, data) 


{ 

    google.maps.event.addListener(marker, "click", function (e) { 

    infoWindow.setContent(data.description); 
    infoWindow.open(map, marker); 
    window.location = "Braches.aspx?DI=" + data.BranchID; 
    icon: InitIcon 

     }); 

     })(marker, data); 

     } 

我沒有任何問題,直到here.My地圖正在

enter image description here

但我想補充這方面的一些靜態標記地圖。

例如

標題= XYZ,LAT = 34.125444經度:42.122121,BranchID = 12345,圖像=圖像/ branchXYZ.png

標題= ABC,LAT = 32.125444經度:49.122121,BranchID = 67676,圖像=圖像/ branchABC.png

標題= DEF,LAT = 31.125444經度:47.122121,BranchID = 3434,圖像=圖像/ branchDEF.png

標題= GFH,LAT = 34.125444經度:42.122121,BranchID = 343434,image =圖片/ branchGFH.png

我該怎麼做?

回答

1

那麼你已經得到了你所需要的大部分。只需用這些標記的新數組重複循環。或者創建一個類似的第二個循環,但是基於這些數據的作用稍有不同。例如

var staticMarkers = [ 
    {title:"XYZ", lat:34.125444, lng:42.122121, BranchID:12345, image:"Images/branchXYZ.png"}, 
    {title:"ABC", lat:32.125444, lng:49.122121, BranchID:67676, image:"Images/branchABC.png"}, 
    {title:"DEF", lat:31.125444, lng:47.122121, BranchID:3434, image:"Images/branchDEF.png"}, 
    {title:"GFH", lat:34.125444, lng:42.122121, BranchID:343434, image:"Images/branchGFH.png"} 
]; 

for (i = 0; i < staticMarkers.length; i++) { 
    var data = staticMarkers[i]; 

    var image = data.image; 
    var myLatlng = new google.maps.LatLng(data.lat, data.lng); 
    var marker = new google.maps.Marker({ 
     position: myLatlng, 
     map: map, 
     title: data.title, 
     icon: image, 
    }); 

    (function (marker, data) 
     { 
      google.maps.event.addListener(marker, "click", function (e) { 
       // you don't have any 'description', so i'm assuming you don't need the infowindow 
       //infoWindow.setContent(data.description); 
       //infoWindow.open(map, marker); 
       window.location = "Braches.aspx?DI=" + data.BranchID; 
      }); 
     })(marker, data); 
} 
+0

這是你的answer.Thank你這麼多鄧肯 – Mhmt

+0

優秀,歡迎您的me.Solved出色答卷 – duncan