How to pass a string of C # (aspx.cs) to a javascript fix for google maps?

0

I have a static variable in my webform class, which are coordinates for a Google map

public static string Query = "19.475 -91.97500000 19.475 -91.98333333 19.4833333 -91.98333333 19.4833333 -92.01666666 19.5 -92.01666666 19.5 -92.025 19.5333333 -92.025 19.5333333 -91.99166666 19.5166667 -91.99166666 19.525 -91.98333333 19.5 -91.98333333 19.5 -91.97500000 19.475 -91.97500000";

In my Javascript block I need an arrangement like this:

var cuadrado = [
          { lat: 25.774, lng: -80.190 },
          { lat: 18.466, lng: -66.118 },
          { lat: 32.321, lng: -64.757 },
          { lat: 25.774, lng: -80.190 }
           ];

In my javascript code send that static variable

var coords = "<%=Query %>";

To my var called coords I do split to replace the spaces with commas ",". and I send it to print on my browser (Google Chrome)

var myArray = coords.split(" ");
alert(myArray);

I put the coordinates in an array to go through the array and concatenate the words "lat:" latitu ", lng:" length ""

for(var d=0; d<myArray.length; d++)
          {
              myArray[i] = +myArray[i]; 
          }

I go through the arrangement and concatenate the words

for(var i=0; i<myArray.length; i++)
          {

              for(var j=0; j<myArray.length;j++)
              {

                  var triangleCoords=[
                 {lat: myArray[i], lng:myArray[j]},
                  ];
              }
          }

And I need to form a block like that, but it's not printed by the nested cycle, Help please

var cuadrado = [
          { lat: 25.774, lng: -80.190 },
          { lat: 18.466, lng: -66.118 },
          { lat: 32.321, lng: -64.757 },
          { lat: 25.774, lng: -80.190 }
           ];
    
asked by Luis David De La Cruz Bautista 18.07.2017 в 01:33
source

1 answer

0

You do not need nested cycles, only one that traverses the two-by-two arrangement

var query = "19.475 -91.97500000 19.475 -91.98333333 19.4833333 -91.98333333 19.4833333 -92.01666666 19.5 -92.01666666 19.5 -92.025 19.5333333 -92.025 19.5333333 -91.99166666 19.5166667 -91.99166666 19.525 -91.98333333 19.5 -91.98333333 19.5 -91.97500000 19.475 -91.97500000";

var numeros = query.split(' ');
var cuadrado;

cuadrado= "[{lat:" + numeros[0] + ", lng:" + numeros[1] + "}";

for(var i=2; i<numeros.length; i+=2){
  
  cuadrado+= ",{lat:" + numeros[i] + ", lng:" + numeros[i+1] + "}";
}
cuadrado +="]";
console.log(cuadrado);
    
answered by 18.07.2017 в 01:54