2

これは capdragon からの更新による新しいコードですが、マーカーを作成しません。これを修正する方法がわかりません。コード スニペットは、マーカーを識別するという以前の問題を解決するための非常にスマートな方法であり、おそらく今私が見逃している非常に小さなものだと思います。もう一度助けていただければ幸いです。

<title>Open Street Map</title>
        <style type="text/css">
        html, body, #demo {
        position: absolute;
        left: 2%;
        width = 25%
        }
        html, body, #activePlanes {
        position: absolute;
        left: 2%;
        width = 25%
        }
        html, body, #mapBox {
        position: fixed;
        top: 0;
        right: 0;
        width: 75%;
        height: 100%;
        }
        </style>

        <script src="http://www.openlayers.org/api/OpenLayers.js"></script>
        <script type="text/javascript">

        var arrMarkers = [];
        function AddPlaneMarker(planeID, lonlat)
        {
            var markers = new OpenLayers.Layer.Markers("Markers");
            map.addLayer(markers);
            var marker = new OpenLayers.Marker(lonlat);
            marker.PlaneID = planeID;
            markers.addMarker(marker);
            arrMarkers.push(marker);
        }

        function RemovePlaneMarker(planeID)
        {
            for(var x in arrMarkers)
            {
                if(arrMarkers[x].PlaneID == planeID)
                {
                    arrMarkers.splice(x, 1);
                    markers.removeMarker(arrMarkers[x]); 
                    return;
                }       
            }
        }  

        function GetPlaneMarker(planeID)
        {
            for(var x in arrMarkers)
            {
                if(arrMarkers[x].PlaneID == planeID)
                {
                    return arrMarkers[x];
                }       
            }
        }

        function displayPlane()
        {
            var x = document.getElementById('planeList');
            var newPlane = document.createElement('option');
            newPlane.text = document.getElementById("plane_ID").value;
            if (newPlane.text == null || newPlane.text == "")
            {
                alert("Please specify an Aircraft ID");
            }
            else
            {
                try
                {
                    x.add(newPlane, x.newPlane[null]); // standards compliant; doesn't work in IE
                }
                catch(e)
                {
                    x.add(newPlane, null); // IE only
                }
                var lonLat = new OpenLayers.LonLat(document.getElementById("long").value, document.getElementById("lat").value).transform(
                new OpenLayers.Projection("EPSG:4326"),
                map.getProjectionObject());
                var planeID = document.getElementById("plane_ID");
                AddPlaneMarker(planeID,lonLat);
            }
        }

        function init()
        {
            map = new OpenLayers.Map("mapBox");
            map.addLayer(new OpenLayers.Layer.OSM());
            focusPoint = new OpenLayers.LonLat(-81.04818, 29.18710)
            focusPoint.transform(new OpenLayers.Projection("EPSG:4326"), map.getProjectionObject());
            map.setCenter(focusPoint, 14);
        }

        function newLonLat()
        {
            var newValues = prompt("Please specify new location of the Aircraft", "-81.07870 29.17210");
            var theValues = newValues.split(" ");
            var newPixel = map.getLayerPxFromViewPortPx(map.getPixelFromLonLat(new OpenLayers.LonLat(theValues[0], theValues[1]).transform(
            new OpenLayers.Projection("EPSG:4326"),
            map.getProjectionObject())));
            marker.moveTo(newPixel);
        }

        function removePlane()
        {
            var x=document.getElementById("planeList");
            x.remove(x.selectedIndex);
            var planeID = x.options[x.selectedIndex].text;
            RemovePlaneMarker(planeID);
        }
        </script>

        <body onload="init();">
        <div id="mapBox"></div>
        </body>
        <body>
        <br /><div id="demo">
        Aircraft ID: <input type="text" id="plane_ID" value="AA5024"/><br />
        Longitude: &nbsp;<input type="text" id="long" value="-81.04818" /><br />
        Latitude: &nbsp;&nbsp;&nbsp;&nbsp;<input type="text" id="lat" value="29.18710" /><br /><br />
        <dd>&nbsp;&nbsp;&nbsp;&nbsp;<button value="Add Aircraft" OnClick="displayPlane()">Add Aircraft</button></dd>
        <br /><br />
        <h3><dd>Active Planes</dd></h3>
        <select id="planeList" size="10" multiple="multiple" style="text-align:center" ondblclick="newLonLat()">
        <option>----------Plane ID----------</option>
        </select><br />
        <dd><button value="Remove Aircraft" OnClick="removePlane()">Remove Aircraft</button></dd>
        </div>
        </body>
4

1 に答える 1

3

markersmarker範囲で利用できるようには見えませんremovePlane()

それらをグローバル スコープで宣言し、varインスタンス化するときに を削除してみてください。

<script type="text/javascript">
...
var markers, marker;
...
        function displayPlane()
             ...
             markers = new OpenLayers.Layer.Markers(name);
             ...
             marker = new OpenLayers.Marker(lonLat);
...

コメントごとに更新:

私は実際には最後の選択肢が一番好きです

マーカーへの参照を含む独自の Plane オブジェクトの配列でそれらを追跡します。このようなもの:

​var arrPlaneObjects = []​;

function AddPlaneMarker(planeID, lonlat){

    var objPlane = {};
    objPlane.ID = planeID;
    objPlane.Marker = new OpenLayers.Marker(lonLat);
    //Add to layer.
    markers.addMarker(objPlane.Marker);
    //Add to array.
    arrPlaneObjects.push(objPlane);
}

function RemovePlaneMarker(planeID){
    for(var x in arrPlaneObjects){
        if(arrPlaneObjects[x].ID == planeID){
            //Remove from array.
            arrPlaneObjects.splice(x, 1);
            //Remove from layer.
            markers.removeMarker(arrPlaneObjects[x].Marker); 
            return;
        }       
    }
}

次に、これを使用してマーカーを簡単に追加および削除できます。

AddPlaneMarker("AA5024", lonLat);

RemovePlaneMarker("AA2222");

別のオプションは、配列内のマーカーを追跡し、次のようにそのマーカー内に平面 ID を格納することです。

​var arrMarkers = []​;

function AddPlaneMarker(planeID, lonlat){

    var marker = new OpenLayers.Marker(lonLat);
    marker.PlaneID = planeID;
    //Add to layer.
    markers.addMarker(marker);
    //Add to array.
    arrMarkers.push(marker);
}

function RemovePlaneMarker(planeID){
    for(var x in arrMarkers){
        if(arrMarkers[x].PlaneID == planeID){

            //Remove from layer.
            markers.removeMarker(arrMarkers[x]); 
            //Remove from array.
            arrMarkers.splice(x, 1);
            return;
        }       
    }
}

後で呼び出すには:

function GetPlaneMarker(planeID){
    for(var x in arrMarkers){
        if(arrMarkers[x].PlaneID == planeID){
            return arrMarkers[x];
        }       
    }
}

var myPlaneMarker = GetPlaneMarker("AA2222");

ソリューションの更新

<title>Open Street Map</title>
        <style type="text/css">
        html, body, #demo {
        position: absolute;
        left: 2%;
        width = 25%
        }
        html, body, #activePlanes {
        position: absolute;
        left: 2%;
        width = 25%
        }
        html, body, #mapBox {
        position: fixed;
        top: 0;
        right: 0;
        width: 75%;
        height: 100%;
        }
        </style>

        <script src="http://www.openlayers.org/api/OpenLayers.js"></script>
        <script type="text/javascript">

            var arrMarkers = [];
            var markers;

            function AddPlaneMarker(planeID, lonlat) {

                var marker = new OpenLayers.Marker(lonlat);
                marker.PlaneID = planeID;
                markers.addMarker(marker);
                arrMarkers.push(marker);

            }

            function RemovePlaneMarker(planeID) {
                for (var x in arrMarkers) {
                    if (arrMarkers[x].PlaneID == planeID) {

                        markers.removeMarker(arrMarkers[x]);
                        arrMarkers.splice(x, 1);

                        return;
                    }
                }
            }

            function GetPlaneMarker(planeID) {
                for (var x in arrMarkers) {
                    if (arrMarkers[x].PlaneID == planeID) {
                        return arrMarkers[x];
                    }
                }
            }

            function displayPlane() {
                var x = document.getElementById('planeList');
                var newPlane = document.createElement('option');
                newPlane.text = document.getElementById("plane_ID").value;
                if (newPlane.text == null || newPlane.text == "") {
                    alert("Please specify an Aircraft ID");
                }
                else {
                    try {
                        x.add(newPlane, x.newPlane[null]); // standards compliant; doesn't work in IE
                    }
                    catch (e) {
                        x.add(newPlane, null); // IE only
                    }
                    var lonLat = new OpenLayers.LonLat(document.getElementById("long").value, document.getElementById("lat").value).transform(
                new OpenLayers.Projection("EPSG:4326"),
                map.getProjectionObject());
                    var planeID = document.getElementById("plane_ID").value;
                    AddPlaneMarker(planeID, lonLat);
                }
            }

            function init() {
                map = new OpenLayers.Map("mapBox");
                map.addLayer(new OpenLayers.Layer.OSM());
                focusPoint = new OpenLayers.LonLat(-81.04818, 29.18710)
                focusPoint.transform(new OpenLayers.Projection("EPSG:4326"), map.getProjectionObject());
                map.setCenter(focusPoint, 14);

                markers = new OpenLayers.Layer.Markers("Markers");
                map.addLayer(markers);
            }

            function newLonLat() {
                var newValues = prompt("Please specify new location of the Aircraft", "-81.07870 29.17210");
                var theValues = newValues.split(" ");
                var newPixel = map.getLayerPxFromViewPortPx(map.getPixelFromLonLat(new OpenLayers.LonLat(theValues[0], theValues[1]).transform(
            new OpenLayers.Projection("EPSG:4326"),
            map.getProjectionObject())));
                marker.moveTo(newPixel);
            }

            function removePlane() {
                var x = document.getElementById("planeList");
                var id = x.options[x.selectedIndex].value;
                x.remove(x.selectedIndex);
                RemovePlaneMarker(id);
            }
        </script>

        <body onload="init();">
        <div id="mapBox"></div>

        <br /><div id="demo">
        Aircraft ID: <input type="text" id="plane_ID" value="AA5024"/><br />
        Longitude: &nbsp;<input type="text" id="long" value="-81.04818" /><br />
        Latitude: &nbsp;&nbsp;&nbsp;&nbsp;<input type="text" id="lat" value="29.18710" /><br /><br />
        <dd>&nbsp;&nbsp;&nbsp;&nbsp;<button value="Add Aircraft" OnClick="displayPlane()">Add Aircraft</button></dd>
        <br /><br />
        <h3><dd>Active Planes</dd></h3>
        <select id="planeList" size="10" multiple="multiple" style="text-align:center" ondblclick="newLonLat()">
        <option>----------Plane ID----------</option>
        </select><br />
        <dd><button value="Remove Aircraft" OnClick="removePlane()">Remove Aircraft</button></dd>
        </div>
        </body>
于 2012-06-14T20:29:01.983 に答える