如何在javascript中将点从Google Mercator转换为WSG84投影

时间:2016-12-07 10:02:45

标签: javascript

在我的js脚本中,我有:

<script type="text/javascript">
    document.addEventListener("DOMContentLoaded", function (event) {
        var overlay_layers = {};
        map = L.map('the_map').setView(new L.LatLng({{ resource.center_x }}, {{ resource.center_y }}), {{ resource.zoom }});

        L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpandmbXliNDBjZWd2M2x6bDk3c2ZtOTkifQ._QA7i5Mpkd_m30IGElHziw',{ 
            maxZoom: 18,
            attribution: 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, ' +
            '<a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, ' +
            'Imagery © <a href="http://mapbox.com">Mapbox</a>',
            id: 'mapbox.streets'
         }).addTo(map);

        [..]
</script>

({{ resource.center_x }}, {{ resource.center_y }})位于Google Mercator投影中。在我的地图中,我需要使用WSG84坐标系。如何将我的观点转换为所需的系统?

1 个答案:

答案 0 :(得分:0)

我找到了一个正在寻找魔法的图书馆:http://proj4js.org/

<script type="text/javascript">
    document.addEventListener("DOMContentLoaded", function (event) {
        var overlay_layers = {};

        var firstProjection = proj4('EPSG:3857');            
        var secondProjection = proj4('EPSG:4326');

        var point_wsg84 = proj4(firstProjection,secondProjection,[{{ resource.center_x }}, {{ resource.center_y }}]);
        var map = L.map('the_map').setView([point_wsg84[1], point_wsg84[0]],6);

    [..]
</script>

无论如何,谢谢!