从当前缩放谷歌地图获取边界经度和纬度

发布于 2024-11-27 14:30:16 字数 183 浏览 1 评论 0原文

我需要知道当前区域四个角的经度和纬度 如这张图片

在此处输入图像描述

我已经尝试过此操作,但没有运气:

map.getBounds();

有任何帮助吗?

i need to know long and lat of my four corners of current area
as in this image

enter image description here

i have tried this but with no luck :

map.getBounds();

any help ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

红尘作伴 2024-12-04 14:30:16

你已经成功了一半。您需要做的就是获取地图边界,然后提取(并正确使用)角点的坐标。

var bounds = map.getBounds();
var ne = bounds.getNorthEast(); // LatLng of the north-east corner
var sw = bounds.getSouthWest(); // LatLng of the south-west corder

您可以从上面的两个角中获得西北角和东南角:

var nw = new google.maps.LatLng(ne.lat(), sw.lng());
var se = new google.maps.LatLng(sw.lat(), ne.lng());

请记住,地图必须已经初始化,否则地图边界为空或未定义。

如果您想更新视口的每次更改,请使用 idle 事件侦听器:

google.maps.event.addListener(map, 'idle', function(ev){
    // update the coordinates here
});

You are half way there. All you need to do is to get the map bounds and then extract (and properly use) the coordinates of the corners.

var bounds = map.getBounds();
var ne = bounds.getNorthEast(); // LatLng of the north-east corner
var sw = bounds.getSouthWest(); // LatLng of the south-west corder

You get north-west and south-east corners from the two above:

var nw = new google.maps.LatLng(ne.lat(), sw.lng());
var se = new google.maps.LatLng(sw.lat(), ne.lng());

Just keep in mind that the map has to be already initialized, otherwise the map bounds are null or undefined.

If you want to be updated about every change of the viewport, use idle event listener:

google.maps.event.addListener(map, 'idle', function(ev){
    // update the coordinates here
});
放我走吧 2024-12-04 14:30:16

在Android中,你可以通过这种方式找到角点LatLng(东北、西南),
在地图中,每次相机监听(意味着手势检测)时,它们都会被调用,

private var mapView: GoogleMap? = null

............

    mapView?.setOnCameraMoveStartedListener { reasonCode ->
        if (reasonCode == GoogleMap.OnCameraMoveStartedListener.REASON_GESTURE) {
            val screenView: LatLngBounds =  mapView.getProjection().getVisibleRegion().latLngBounds
            var northeast=screenView.northeast
            var southwest=screenView.southwest
            var center=screenView.center
            
            
            Log.v("northeast LatLng","-:"+northeast)// LatLng of the north-east Screen
            Log.v("southwest LatLng","-:"+southwest)// LatLng of the south-west Screen
            Log.v("center LatLng","-:"+center)// LatLng of the center Screen
        }
    }

In Android, you can find out corner LatLng (northeast, southwest) by this way,
in the map, every time the camera listen(means gesture detect) they are called,

private var mapView: GoogleMap? = null

............

    mapView?.setOnCameraMoveStartedListener { reasonCode ->
        if (reasonCode == GoogleMap.OnCameraMoveStartedListener.REASON_GESTURE) {
            val screenView: LatLngBounds =  mapView.getProjection().getVisibleRegion().latLngBounds
            var northeast=screenView.northeast
            var southwest=screenView.southwest
            var center=screenView.center
            
            
            Log.v("northeast LatLng","-:"+northeast)// LatLng of the north-east Screen
            Log.v("southwest LatLng","-:"+southwest)// LatLng of the south-west Screen
            Log.v("center LatLng","-:"+center)// LatLng of the center Screen
        }
    }
塔塔猫 2024-12-04 14:30:16
**this is mapbox zoom example**
 map.on('zoom', (el) => {
        map.getBounds();
    });
**this is mapbox zoom example**
 map.on('zoom', (el) => {
        map.getBounds();
    });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文