javascript 将地理 WGS 84 转换为 Web Mercator 102100

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11957538/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 14:52:50  来源:igfitidea点击:

Converting geographic WGS 84 to Web Mercator 102100

javascriptarcgisesri

提问by

I am trying to convert geographic coordinate system to Esri Webmercator, but when I do the conversion the resulted x and y have values of 0000003232112222… and 00000012665321…. This is very odd since coordinates as those do not exist.

我正在尝试将地理坐标系转换为 Esri Webmercator,但是当我进行转换时,结果 x 和 y 的值为 0000003232112222... 和 00000012665321...。这很奇怪,因为坐标不存在。

var positions = [];
positions.push(x, y);

var g = new esri.geometry.Point(positions);
g = esri.geometry.geographicToWebMercator(g);
x = g.x;
y = g.y;

回答by YinchaoOnline

You should try geographicToWebMercatormethod in esri/geometry/webMercatorUtilsmodule.see the detailed documentation.

您应该尝试模块中的geographicToWebMercator方法esri/geometry/webMercatorUtils。请参阅详细文档

        //a point in GCS_WGS_1984(wkid is 4326)
        var point = new Point(-118.15, 33.80, new SpatialReference({
            wkid: 4326
        }));

        var pointWebMercator = webMercatorUtils.geographicToWebMercator(point);

        alert("the point in 102100 is ( " + pointWebMercator.x + "," + pointWebMercator.y + " )");

a live demo:

现场演示:

<!DOCTYPE html>
<html>

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no" />
    <title>Converting geographic WGS 84 to Web Mercator 102100</title>
    <link rel="stylesheet" href="https://js.arcgis.com/3.20/esri/css/esri.css">
    <style>
        html,
        body,
        #map {
            height: 100%;
            margin: 0;
            padding: 0;
        }
    </style>
    <script src="https://js.arcgis.com/3.20/"></script>
    <script>
        var map;

        require(["esri/map", "esri/geometry/Point", "esri/SpatialReference", "esri/geometry/webMercatorUtils", "dojo/domReady!"], function (Map, Point, SpatialReference, webMercatorUtils) {
            map = new Map("map", {
                basemap: "topo", //For full list of pre-defined basemaps, navigate to http://arcg.is/1JVo6Wd
                center: [-122.45, 37.75], // longitude, latitude
                zoom: 13
            });

            //a point in GCS_WGS_1984(wkid is 4326)
            var point = new Point(-118.15, 33.80, new SpatialReference({
                wkid: 4326
            }));

            var pointWebMercator = webMercatorUtils.geographicToWebMercator(point);

            alert("the point in 102100 is ( " + pointWebMercator.x + "," + pointWebMercator.y + " )");
        });
    </script>
</head>

<body>
    <div id="map"></div>
</body>

</html>

Hope it could help you.

希望能帮到你。

回答by Bjorn Svensson

You don't actually have to convert the latitide/longitude to add points to a basemap that's in webmercator.

您实际上不必转换纬度/经度以将点添加到 webmercator 中的底图。

You can create the Point directly using latitude/longitude (and the API will internally do the conversion from geographic to webmercator) in a few different ways. This is available since version 3.3 (January 2013).

您可以通过几种不同的方式直接使用纬度/经度创建点(API 将在内部进行从地理到 webmercator 的转换)。自 3.3 版(2013 年 1 月)起可用。

var point = new Point(-98, 38); // note that longitude(x) comes before the latitude(y).

// or as an array
var point = new Point([-98, 38]);

// or as an object
var point = new Point({latitude: 38, longitude: -98});

https://developers.arcgis.com/javascript/3/jsapi/point-amd.html#point4

https://developers.arcgis.com/javascript/3/jsapi/point-amd.html#point4