简体   繁体   English

如何使用 Leaflet.js 获得所需的 lat、long 值以仅显示 map 的所需部分?

[英]How to get the desired value for lat,long to display only the required portion of the map with Leaflet.js?

I am trying to display a portion of a map with this amount of code:-我正在尝试使用此代码量显示 map 的一部分:-

<div id="mapid" style="height: 280px; width: 1143px;">
    <script src="http://cdn.leafletjs.com/leaflet/v0.7.7/leaflet.js"></script>

    <script>
        var mymap = L.map("mapid").setView([5, 120], 13);

        L.tileLayer("http://{s}.tile.osm.org/{z}/{x}/{y}.png", {
            attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors',
        }).addTo(mymap);
    </script>
</div>

But that displays this portion of the map:-但这显示了 map 的这一部分:- 在此处输入图像描述

But I am trying to get this portion of the map:-但我正在尝试获取 map 的这一部分:- 在此处输入图像描述 is there any tool I can optimize my code to do so:-有什么工具可以优化我的代码:-

You can set the view of the map with: map.setView([lat,lng],zoom)您可以使用以下命令设置 map 的视图: map.setView([lat,lng],zoom)

For your example: mymap.setView([-3.687845, 113.776067], 4);对于您的示例: mymap.setView([-3.687845, 113.776067], 4);

Or you can set the bounds (top left corner and right bottom corner) of the map, so the map is calculated what is the best zoom to contain the bounds.或者您可以设置 map 的边界(左上角和右下角),因此计算 map 包含边界的最佳缩放比例。

var bounds = L.latLngBounds([6.884470, 94.897447],[-13.549102, 154.879214])
mymap.fitBounds(bounds)

But to answer your question, you can get the latlng when you click on the map with:但是要回答您的问题,您可以在单击 map 时获得 latlng:

map.on('click',function(e){
    console.log(e.latlng);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM