gol*_*oli 5 javascript google-maps
我有 getBounds() 谷歌地图 API 返回的绑定值,如下所示 -
\n\n**_.pe {pa: oe, ka: ke} \nka: ke {g: -180, h: 180} \npa: oe {g: -26.222936261748305, h: 72.98776122961861} \n__proto__: Object** \nRun Code Online (Sandbox Code Playgroud)\n\n在谷歌搜索中,我发现我们可以使用 getSouthWest() 和 getNorthEast() api 来解码
上面的信息以获得所需的坐标,但不幸的是它对我不起作用。
\n我得到以下输出 -
**console.log(vr.getNorthEast()) \nVM312861:1 \n_.L {lat: \xc6\x92, lng: \xc6\x92} \nlat: \xc6\x92 () \nlng: \xc6\x92 () \n__proto__: Object** \nRun Code Online (Sandbox Code Playgroud)\n\n任何想法如何解决这个问题或任何其他我可以用来从边界获取坐标的方法。
\n\n谢谢
\ngetNorthEast并getSouthWest返回LatLng对象。要从这些对象获取原始值,您可以在对象上使用以下函数LatLng来获取坐标:.lat()纬度、.lng()经度或.toString()坐标的字符串表示形式。
另请注意,必须初始化地图,否则边界将不确定。
这是一个工作示例。您可以忽略由于未使用 API 密钥而导致的初始脚本错误。只需拖动地图即可查看控制台中显示的所有四个角的坐标:
let map;
function initialize() {
let mapOptions = {
zoom: 8,
center: new google.maps.LatLng(-34.397, 150.644)
};
map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);
map.addListener("dragend", function() {
let bounds = map.getBounds();
let ne = bounds.getNorthEast(); // Coords of the northeast corner
let sw = bounds.getSouthWest(); // Coords of the southwest corner
let nw = new google.maps.LatLng(ne.lat(), sw.lng()); // Coords of the NW corner
let se = new google.maps.LatLng(sw.lat(), ne.lng()); // Coords of the SE corner
console.log(ne.toString(), sw.toString(), nw.toString(), se.toString());
})
}
google.maps.event.addDomListener(window, 'load', initialize);Run Code Online (Sandbox Code Playgroud)
#map-canvas {
height: 500px;
margin: 0px;
padding: 0px;
width: 500px;
}Run Code Online (Sandbox Code Playgroud)
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<div id="map-canvas"></div>Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2710 次 |
| 最近记录: |