函数ol.proj.transform()根据用户输入返回错误的坐标。为了测试目的,我设置为50.7712078,9.6679688。
当它们通过值传递给函数(add_marker(y,x))时,结果为:

1076233.3638212564,-4754497.267923687

但是,当我创建代表与用户输入相同的纬度和经度的局部变量时
这给了我:

1076233.3638212564,6580922.381627579

ol3中的此功能是否有问题?

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; carset=utf-8">
        <link rel="stylesheet" href="./_css_II.css" type="text/css">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js" type="text/javascript"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/ol3/3.5.0/ol.js" type="text/javascript"></script>
        <script src="./_script.js" type="text/javascript"></script>
    </head>

    <body>
        <div id="map" class="map"></div>
        <div id="geo-marker" class="marker"></div>

        <div id="inputForm">
            Latitude:
        <input id="latitude" type="text" value="50.7712078"/>//default
            Longitude:
        <input id="longitude" type="text" value="9.6679688"/>//default
        <input id="btn_addmarker" type="button" value="Add Marker" />
        </div>
    </body>
</html>


_script.js

var map, marker;

$(document).ready(function(){

    init_map();
    $("#btn_addmarker").on("click",function(){

        var lat = $("#latitude").val();
        var lon = $("#longitude").val();

        add_marker(lon,lat);

    });
});

function init_map(){

    var OSM = new ol.layer.Tile({

        source: new ol.source.OSM()
    });

    var v_OSM = new ol.View({

        center:[2061969,7281059],
        zoom: 14
    });

    var map = new ol.Map({

        layers: [OSM],
        target: "map",
        renderer: "canvas",
        view: v_OSM
    });

    var markerHwnd = document.getElementById("geo-marker");

    marker = new ol.Overlay({

        position: [2061961,7281059],
        positioning: "bottom-center",
        offset: [0,0],
        element: markerHwnd,
        stopEvent: false
    });

    map.addOverlay(marker);
}

function add_marker(y,x){

    var goodLat = 50.7712078;
    var goodLon = 9.6679688;


    var newCoord = ol.proj.transform([y,x],"EPSG:4326","EPSG:3857");

    console.log(newCoord); //[1076233.3638212564, -4754497.267923687] <- Should be: [1076233.3638212564, 6580922.381627579]

    /*
        var newCoord = ol.proj.transform([goodLon,goodLat],"EPSG:4326","EPSG:3857");
        console.log(newCoord);//[1076233.3638212564, 6580922.381627579] <- OK!
    */

    marker.setPosition(newCoord);

}

最佳答案

您正在传递文本作为参数。
调用方法以使其正确时,显式解析参数:
    add_marker(parseFloat(lon),parseFloat(lat));

关于javascript - ol3 ol.proj.transform错误的转换,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37208969/

10-10 08:51