2016-12-07 32 views
0

在我的js腳本有:我如何一點從谷歌墨卡託轉換爲WSG84投影在JavaScript

<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 }})是在谷歌墨卡託投影。在我的地圖中,我需要使用WSG84座標系。我怎樣才能將我的觀點轉化爲所需的系統?

回答

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> 

無論如何謝謝!

相關問題