Hi Peeps...

I'm a little stuck on this one, I'm sure it can be done, but just can not get my head around it all.

Here is the problem...

I have Lat & Lng stored in a database like this... which should draw a polygon on a map.

PHP

$polyPoints="50.37349614430351, -2.0654296875000106,51.56341232867588, 2.4609375,53.514184520771124, -3.427734375000007,50.37349614430351, -2.0654296875000106";

OR

$polyPoints=$row[polyPoints];

I need to get $polyPoints in to a JavaScript Array so I can use it like this...

JavaScript

var polygon = new VEShape(VEShapeType.Polygon, polyPoints);
polygon.HideIcon();
map.AddShape(polygon);

Can anyone show me the light here please! 😕

:o

    Assuming that it's a comma separated string, then I'd do something like this:

    $polyPoints = $row['polyPoints'];
    // $polyPoints now has this value:
    // 50.37349614430351, -2.0654296875000106,51.56341232867588, 2.4609375,53.514184520771124, -3.427734375000007,50.37349614430351, -2.0654296875000106
    
    $polyPoints = explode(',', $polyPoints);
    // $polyPoints now has this value:
    // Array (
    //     [0] => 50.37349614430351
    //     [1] => -2.0654296875000106
    //     [2] => 51.56341232867588
    //     [3] => 2.4609375
    //     [4] => 53.514184520771124
    //     [5] => -3.427734375000007
    //     [6] => 50.37349614430351
    //     [7] => -2.0654296875000106
    
    
    $JSON = json_encode($polyPoints);
    // $JSON now has this value:
    // ["50.37349614430351"," -2.0654296875000106","51.56341232867588"," 2.4609375","53.514184520771124"," -3.427734375000007","50.37349614430351"," -2.0654296875000106"]
    
    // Now just echo the value of $JSON into a new Javascript variable:
    echo '<script language="Javascript"><!-- // --><![CDATA[
        var polyPoints = ' . $JSON . '
    
    var polygon = new VEShape(VEShapeType.Polygon, polyPoints);
    polygon.HideIcon();
    map.AddShape(polygon);
    // ]]></script>';
    

    And there you have it. The javascript variable "polypoints" will be a javascript array which you can use to get the items like:
    polyPoints[?] where ? will be a number between 0 and 7.

      Hi

      Thanks for quick response...

      I'm using PHP 4, can it still be done another way?

      🙂

        Hi

        In fact, to make things work [ I've just found out ]

        The Var must be returned like this in Javascript.

        var points = [
        new VELatLong(50.37349614430351, -2.0654296875000106),
        new VELatLong(51.56341232867588, 2.4609375),
        new VELatLong(53.514184520771124, -3.427734375000007),
        new VELatLong(50.37349614430351, -2.0654296875000106)
        ];

        Things I notice are...

        The first and last lines are always the same [ to close the polygon ]
        The last 'new VELatLong() does not have a , to continue...

        So I need to turn

        $polyPoints="50.37349614430351, -2.0654296875000106,51.56341232867588, 2.4609375,53.514184520771124, -3.427734375000007,50.37349614430351, -2.0654296875000106";

        in to...

        var points = [
        new VELatLong(50.37349614430351, -2.0654296875000106),
        new VELatLong(51.56341232867588, 2.4609375),
        new VELatLong(53.514184520771124, -3.427734375000007),
        new VELatLong(50.37349614430351, -2.0654296875000106)
        ];

        remembering there could be any number of points held in $row[polyPoints];

        😕

          9 days later
          for($i=0; $i<count($polyPoints); $i++)
          {
              $js .= 'new VELatLong('.$polyPoints[$i].', '.$polyPoints[++$i].'),';
          }
          
          $js = substr($js, 0, -1); // remove the last ","
          
          $output = 'var points = [' . $js . '];';
          
          echo $output;

          Something like that?

            Write a Reply...