0

I am trying to get the area of a polygon using JavaScript. To do this I need to convert the longitude and latitude points in my JSON array over to Cartesian x and y coordinates. This loop and function converts the longitude value to x for each longitude value in the array. I now need to make a new array with all of these values. At the moment it is making a new array for every value in the JSON array. Here is the result in the console:

[445.555480625]                                mapChoropleth.html:280
[1, 445.55897]                                 mapChoropleth.html:280
[2, 2: 445.62943062500005]                     mapChoropleth.html:280
[3, 3: 445.63478375]                           mapChoropleth.html:280
[4, 4: 445.64964499999996]                     mapChoropleth.html:280
[5, 5: 445.6483775]                            mapChoropleth.html:280
[6, 6: 445.61210562499997]                     mapChoropleth.html:280
[7, 7: 445.612394375]                          mapChoropleth.html:280
[8, 8: 445.6023443750001]                      mapChoropleth.html:280
[9, 9: 445.604339375]                          mapChoropleth.html:280
[10, 10: 445.571159375]                        mapChoropleth.html:280

This is how I am trying to fill the new array at the moment:

for (var i=0;i<areaData.coordinates[0].length;i++){
        var lng = areaData.coordinates[0][i][1];
        var x = XLngToPixel(lng, '#map');
            function XLngToPixel(lng,elem){

                var array = new Array();
                var containerWidth=($(elem).width());
                    lng=lng+180;
                    array[i] = $(elem).offset().left+((lng*containerWidth)/360);
                return array;
            }
            console.log(YLatToPixel(lng, '#map'));
    }

Any help would be much appreciated!

7
  • 1
    declare array outside for loop and just initialize it inside for loop. Commented Oct 26, 2013 at 19:15
  • @codeSpy how would that make a difference? Commented Oct 26, 2013 at 19:15
  • @BenjaminGruenbaum i corrected that, it's the array that should be declared outside the loop . Commented Oct 26, 2013 at 19:20
  • @codeSpy I still don't understand why that would make a difference. Mind elaborating? Commented Oct 26, 2013 at 19:22
  • 1
    var MyArray = new Array(); this line creates an instance. Everytime the loop is iterating having that line, it's creating a new instance of array(according to this code).So everytime the corresponding value is being stored in a new array rather a single array. But if the array is created outside the loop and just initialized inside with corresponding values like this MyArray[index] = value; then all the values will be inserted into a single array rather different instances/objects of arrays. Commented Oct 26, 2013 at 19:28

1 Answer 1

1

To create a single array containing your results, the array needs to be created outside of your loop:

var array = new Array();
for (var i=0;i<areaData.coordinates[0].length;i++){
    var lng = areaData.coordinates[0][i][1];
    var x = XLngToPixel(lng, '#map');
        function XLngToPixel(lng,elem) {
            var containerWidth=($(elem).width());
            lng=lng+180;
            array[i] = $(elem).offset().left+((lng*containerWidth)/360);
            return array[i];
        }
        console.log(YLatToPixel(lng, '#map'));
}

To create arrays of lat and lng:

var lats = new Array();
var lngs = new Array();
for (var i=0;i<areaData.coordinates[0].length;i++){
    var lng = areaData.coordinates[0][i][1];
    var x = XLngToPixel(lng, '#map');
        function XLngToPixel(lng,elem) {
            var containerWidth=($(elem).width());
            lng=lng+180;
            lngs[i] = lng;
            lats[i] = $(elem).offset().left+((lng*containerWidth)/360);

            return lats[i];
        }
        console.log(YLatToPixel(lng, '#map'));
}

Is this something like what you are looking for?

Sign up to request clarification or add additional context in comments.

3 Comments

Consider using array literals [] and don't make functions within loops.
Both valid points, I was trying to help Milliam iterate the code forward rather than give them a text book answer, however!
Thank you, moving the array outside the loop did the job!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.