谷歌地图获取坐标中心(在多边形中心放置标签) [英] Google Maps get the center of coordinates (place label at center of polygon)

查看:51
本文介绍了谷歌地图获取坐标中心(在多边形中心放置标签)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用谷歌地图,我在地图上像这样标记区域:

I use Google maps, I mark areas on the maps like that:

 var areaCoords2 = [
        new google.maps.LatLng(32.819649, 35.073102),
        new google.maps.LatLng(32.819604, 35.073026),
        new google.maps.LatLng(32.817169, 35.071321),
        new google.maps.LatLng(32.817097, 35.071353),
        new google.maps.LatLng(32.816042, 35.073391),
        new google.maps.LatLng(32.818513, 35.075119),
        new google.maps.LatLng(32.818612, 35.075054)
    ];

我想实现:将标签标记放在标记红色区域的中间(大约).

I want to achieve: place the label and the marker in the middle(approximately) of the marked red area.

而不是用静态的纬度和经度放置它(我有很多区域)

instead of place it with static Latitude and Longitude(I have a lot of areas)

有办法以编程方式这样做吗?

There is a way of doing so programmatically?

推荐答案

以下代码构建了一个 google.maps.Polygon 并在其边界的中心放置了一个 MapLabel.

The following code constructs a google.maps.Polygon and places a MapLabel at the center of its bounds.

        // Construct the polygon.
        var mypolygon2 = new google.maps.Polygon({
            paths: polyCoords,
            strokeColor: '#FF0000',
            strokeOpacity: 0.8,
            strokeWeight: 3,
            fillColor: '#FF0000',
            fillOpacity: 0.35
        });

        mypolygon2.setMap(map);

        //Define position of label
        var bounds = new google.maps.LatLngBounds();
        for (var i=0; i< polyCoords.length; i++) {
          bounds.extend(polyCoords[i]);
        }

        var myLatlng = bounds.getCenter();

        var mapLabel2 = new MapLabel({
            text: '2',
            position: myLatlng,
            map: map,
            fontSize: 20,
            align: 'left'
        });
        mapLabel2.set('position', myLatlng);
        var obj = {};
        obj.poly = mypolygon2;
        obj.label = mapLabel2;

代码片段:




这篇关于谷歌地图获取坐标中心(在多边形中心放置标签)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆