Google Maps array + GLatLng + GPolyline

I use the following code to group and display a list of ship positions on a map:

function configure_ship_polylines() {

    var shipPoints = new Array();

    // Group positions by ship
    for (var ScenarioPositionID in __data['scenarioPositions']) {

        var scenarioPosition = __data['scenarioPositions'][ScenarioPositionID];
        var key = "ShipID_" + scenarioPosition.ShipID;

        // Initialize if necessary
        if (shipPoints[key] == null) {
            shipPoints[key] = new Array();
        }

        var latLong = new GLatLng(scenarioPosition.Latitude, scenarioPosition.Longitude);

        // Append coordinates
        shipPoints[key].push(latLong);
    }


    // Loop through the grouped coordinates and add to map
    for (var key in shipPoints) {

        var points = shipPoints[key];
        var ShipID = key.substring(7);

        // Only add polygons with points.length > 1
        if (points.length > 1) {

            var shipPolyline = new GPolyline(points);

            //alert("Adding polyline: " + shipPolyline + " for ship: " + ShipID + " " + points + " " + typeof points);

            __mapItems['shipPolylines'][key] = shipPolyline;

            __map.addOverlay(shipPolyline);

        }
    }
}

It happens that only one of the polylines is displayed on the map. The rest are invisible or not added at all (I'm not quite sure how to debug Google maps to find out). I used firebug many times to debug this, but everything seems fine.

If I create polylines manually, I can add them to the map, and everything will be fine. If I create markers at each point instead of polylines, then the markers appear in the right places.

I'm mad at this because I almost spent a full day trying to figure out what the hell was going on. Any ideas?

+3
source share
3

, , GLatLng shipPoints [key], . :

shipPoints[key].push(latLong);

// points is a latlong
var points = shipPoints[key];
// create a polyline using one latlng
var shipPolyline = new GPolyline(points);
// add polyline to map
__map.addOverlay(shipPolyline);

, , GLatLngs, GPolyline . .

, .

+1

javascript (ctrl + shift + j Firefox)? , , , for-in , javascript. javascript, Array.prototype, for-in ( ).

polyline.show() ?

0

This may be due to the fact that all polylines are based on the shipPolyline variable, and for some reason, all relate to the same source data. Try this for the second loop:

// Loop through the grouped coordinates and add to map
for (var key in shipPoints) {

    var points = shipPoints[key];
    var ShipID = key.substring(7);

    // Only add polygons with points.length > 1
    if (points.length > 1) {

        //alert("Adding polyline: " + shipPolyline + " for ship: " + ShipID + " " + points + " " + typeof points);

        __mapItems['shipPolylines'][key] = new GPolyline(points);

        __map.addOverlay(__mapItems['shipPolylines'][key]);

    }
0
source

Source: https://habr.com/ru/post/1721992/