不理解传单中 latLngBounds 的语法


传单latLngBounds()函数有一个getCenter()方法,但我不明白返回该值的语法。


var mk5 = L.latLngBounds.getCenter(); returns an error

var mk5 = L.latLngBounds().getCenter(); returns an error

var mk5 = L.latLngBounds({getCenter()}); returns an error

找到其使用的例子是不可能的。有人请告诉我使用此方法的语法。


绝地无双
浏览 204回答 1
1回答

三国纷争

如文档所示:创建第一个角:var corner1 = L.latLng(40.712, -74.227);创建第二个角:var corner2 = L.latLng(40.774, -74.125);创建边界:bounds = L.latLngBounds(corner1, corner2);从实例调用方法:getCenterboundsbounds.getCenter()下面是一个示例:<!DOCTYPE html><html>&nbsp; <head>&nbsp; &nbsp; <title>Quick Start - Leaflet</title>&nbsp; &nbsp; <meta charset="utf-8" />&nbsp; &nbsp; <meta name="viewport" content="width=device-width, initial-scale=1.0">&nbsp; &nbsp; <link rel="shortcut icon" type="image/x-icon" href="docs/images/favicon.ico" />&nbsp; &nbsp; <link rel="stylesheet" href="https://unpkg.com/leaflet@1.6.0/dist/leaflet.css" integrity="sha512-xwE/Az9zrjBIphAcBb3F6JVqxf46+CDLwfLMHloNu6KEQCAWi6HcDUbeOfBIptF7tcCzusKFjFw2yuvEpDL9wQ==" crossorigin="" />&nbsp; &nbsp; <script src="https://unpkg.com/leaflet@1.6.0/dist/leaflet.js" integrity="sha512-gZwIG9x3wUXg2hdXF6+rVkLF/0Vi9U8D2Ntg4Ga5I5BZpVkVxlJWbSQtXPSiUTtC0TjtGOmxa1AJPuV0CPthew==" crossorigin=""></script>&nbsp; </head>&nbsp; <body>&nbsp; &nbsp; <div id="mapid" style="width: 600px; height: 400px;"></div>&nbsp; &nbsp; <script>&nbsp; &nbsp; &nbsp; var mymap = L.map('mapid').setView([51.505, -0.09], 2);&nbsp; &nbsp; &nbsp; L.tileLayer('https://api.mapbox.com/styles/v1/{id}/tiles/{z}/{x}/{y}?access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpejY4NXVycTA2emYycXBndHRqcmZ3N3gifQ.rJcFIG214AriISLbB6B5aw', {&nbsp; &nbsp; &nbsp; &nbsp; maxZoom: 18,&nbsp; &nbsp; &nbsp; &nbsp; attribution: 'Map data &copy; <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors, ' +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '<a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, ' +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'Imagery © <a href="https://www.mapbox.com/">Mapbox</a>',&nbsp; &nbsp; &nbsp; &nbsp; id: 'mapbox/streets-v11',&nbsp; &nbsp; &nbsp; &nbsp; tileSize: 512,&nbsp; &nbsp; &nbsp; &nbsp; zoomOffset: -1&nbsp; &nbsp; &nbsp; }).addTo(mymap);&nbsp; &nbsp; &nbsp; var corner1 = L.latLng(40.712, -74.227),&nbsp; &nbsp; &nbsp; &nbsp; corner2 = L.latLng(40.774, -74.125),&nbsp; &nbsp; &nbsp; &nbsp; bounds = L.latLngBounds(corner1, corner2)&nbsp; &nbsp; &nbsp; var rect = L.rectangle(bounds, {&nbsp; &nbsp; &nbsp; &nbsp; color: 'blue',&nbsp; &nbsp; &nbsp; &nbsp; weight: 1&nbsp; &nbsp; &nbsp; }).addTo(mymap);&nbsp; &nbsp; &nbsp; mymap.fitBounds(bounds)&nbsp; &nbsp; &nbsp; const markerCoords = bounds.getCenter()&nbsp; &nbsp; &nbsp; L.marker(markerCoords).addTo(mymap)&nbsp; &nbsp; </script>&nbsp; </body></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript