addGeoJson函数反转Lat和Lng?

时间:2016-02-01 00:59:07

标签: google-maps-api-3 geojson

我正在开发一个谷歌地图功能,以便在地图中显示GEOJson数据。

根据网络,以下几点是蒙特利尔:

  • Point1:lat = 45.555和lng = -73.555
  • Point2:lat = 45.666和lng = -73.666

http://www.latlong.net/c/?lat=45.666&long=-73.666

所以我构建了我的GEOJson数据如下(在蒙特利尔显示两个标记):

var data_default = {
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [45.555, -73.555]
            }
        },
        {
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [45.666, -73.666]
            }
        }
    ] };

但是,对于此数据,具有以下功能:

map.data.addGeoJson(data);

蒙特利尔没​​有表明要点蒙特利尔我应该反转"坐标"的纬度和长度。 ..所以我的两点应该有以下坐标:[ - 73.555,45.555] [-73.666,45.666]而不是[45.555,-73.555] [45.666,-73.666]

有人可以向我解释为什么Google map api的addGeoJson正在以相反的方式读取这些点吗?

谢谢

1 个答案:

答案 0 :(得分:1)

GeoJSON将坐标指定为[经度,纬度]。你有倒退的坐标。

来自the spec

  

点坐标为x,y顺序(东向,北向投影坐标,经度,纬度为地理坐标):

代码段

function initialize() {
  var map = new google.maps.Map(
    document.getElementById("map_canvas"), {
      center: new google.maps.LatLng(45.555, -73.555),
      zoom: 10,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });
  var data_default = {
    "type": "FeatureCollection",
    "features": [{
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [-73.555, 45.555]
      }
    }, {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [-73.666, 45.666]
      }
    }]
  };
  map.data.addGeoJson(data_default);
}
google.maps.event.addDomListener(window, "load", initialize);
html,
body,
#map_canvas {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map_canvas"></div>