2017-09-20 1 views
0

J'appelle une fonction JavaScript dans une boucle en C#. Chaque appel envoie la description, la longitude et la latitude à la fonction. La fonction crée un marqueur individuel dans une carte. Ce que j'aimerais voir, ce sont tous les marqueurs dont les informations correspondantes sont envoyées dans la boucle. Ce que je vois est le marqueur du dernier objet qui a été envoyé.Création de marqueurs dans Google Maps, mais affichage uniquement du dernier marqueur

code C#

 protected void MapButton_Click(object sender, EventArgs e) 
    { 
     /////////////////////////////////////////////// 
     // Get all Games that are being played today // 
     // then send HomeTeam, AwayTeam and   // 
     // Coordinates to MapIt.      // 
     /////////////////////////////////////////////// 

     /////////////////////////////////////////////// 
     // Step 1: Call Stored Procedure that will // 
     //   return the TeamNames and   // 
     //   Coordinates where the games will // 
     //   be played today.     // 
     /////////////////////////////////////////////// 
     string strcon = WebConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString; 
     using (SqlConnection conn = new SqlConnection(strcon)) 
     { 
      SqlCommand cmd = new SqlCommand("GameDayCoordinates", conn); 
      cmd.CommandType = CommandType.StoredProcedure; 
      conn.Open(); 
      DataTable MapItTbl = new DataTable(); 
      SqlDataAdapter da = new SqlDataAdapter(cmd); 
      da.Fill(MapItTbl); 
      /////////////////////////////////////////////// 
      // Step 2: Send each set of Teams and  // 
      //   Coordinates to MapIt. Within a // 
      //   loop.        // 
      // Send the Google Map Coordinates, and this // 
      // function will mark the places on Google // 
      // Maps via a JavaScript function.   // 
      /////////////////////////////////////////////// 

      int count = MapItTbl.Rows.Count; 
      string[] splitSeparator = new string[] { ", ",", "}; 
      LocObject[] LocationInfo = new LocObject[count]; 

      for (int i = 0; i < MapItTbl.Rows.Count; i++) 
      { 
       DataRow row = MapItTbl.Rows[i]; 
       string Coordinates = row["Coordinate"].ToString(); 
       if (Coordinates != "NULL") { 
        string[] strArr = Coordinates.Split(splitSeparator, StringSplitOptions.None); 
        ScriptManager.RegisterStartupScript((Page)this, base.GetType(), i + "GMaps" + DateTime.UtcNow, string.Format("GameMap('{0}','{1}','{2}','{3}');", row.ItemArray.GetValue(0).ToString(), strArr[0], strArr[1], i), true); 
       } 
      } 
     } 
    } 

La fonction JavaScript appelée

  function GameMap(description, lat, lng, index) { 

      var marker = new Array(); 
      var i; 
      var max = 100; 
      var myLatLng = { lat: 32.787407, lng: -82.269194 }; 
      var MapObj; 
      MapObj = document.getElementById('map1'); 

      var map1 = new google.maps.Map(MapObj, { 
       zoom: 7, 
       center: myLatLng, 
       mapTypeId: google.maps.MapTypeId.HYBRID 
      }); 

      var GameLatlng = new google.maps.LatLng(parseFloat(lat), parseFloat(lng)); 

      document.write(description); 
      document.write(index); 

      marker = new google.maps.Marker({ 
       position: GameLatlng, 
       title: description 
      }); 

      //for (i = 0; i < 8; i++) { 
      marker.setMap(map1); 
      //} 
     } 

Comment puis-je obtenir tous les marqueurs (par opposition à juste le dernier marqueur) pour apparaître?

+1

marqueur 'var = new Array();' ',,, puis marqueur = new google.maps.Marker ({' ... donc, marqué est jamais utilisé comme un tableau, alors pourquoi trouvez-vous initialisez –

+0

Vous avez tenté de l'utiliser comme un tableau à un moment donné dans l'espoir que le marqueur soit identifié de manière unique, ce qui s'est avéré non pertinent – MDDeVane

+0

Y at-il un moyen de s'assurer que le marqueur restera affiché une fois que je quitterai la fonction JavaScript? le marqueur est créé, est-il remplacé par un nouvel appel à la fonction JavaScript? – MDDeVane

Répondre

0

Voir les commentaires après "Bon poing .... Merci!"

<script type="text/javascript"> 

    var map; 
     function initMap() { 

      var myLatLng = { lat: 32.787407, lng: -83.269194 }; 

      map = new google.maps.Map(document.getElementById('map1'), { 
       zoom: 7, 
       center: myLatLng, 
       mapTypeId: google.maps.MapTypeId.HYBRID 
      }); 

      //var marker = new google.maps.Marker({ 
      // position: myLatLng, 
      // map: map 
      //}); 
     } 



     function GameMap(description, Lat, Lng) { 

      var GameLatlng = new google.maps.LatLng(Lat, Lng); 

      var marker = new google.maps.Marker({ 
       position: GameLatlng, 
       title: description, 
       map: map 
      }); 

      document.write(description); 
      document.write(" * "); 
      document.write(Lat); 
      document.write(" * "); 
      document.write(Lng); 
      document.write("<br>"); 
     } 

</script>