在Google地图中找到多个地点的中心 [英] Find center of multiple locations in Google Maps

查看:94
本文介绍了在Google地图中找到多个地点的中心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚在这个问题上复制了代码

a>并应用我的经纬度。然而,经度和纬度都是动态的,地图的 center 会根据位置的纬度和经度而变化。

以下是来自其他问题的代码
$ b

 <!DOCTYPE HTML> 
< html>

< head>
< meta http-equiv =content-typecontent =text / html; charset = UTF-8/>
< title> Google地图多个标记< / title>
< script src =http://maps.google.com/maps/api/js?sensor=falsetype =text / javascript>< / script>
< / head>

< body>
< div id =mapstyle =width:500px; height:400px;>< / div>
< script type =text / javascript>
var locations = [
['Bondi Beach',-33.890542,151.274856,4],
['Coogee Beach',-33.923036,151.259052,5],
[' Cronulla Beach',-34.028249,151.157507,3],
['Manly Beach',-33.80010128657071,151.28747820854187,2],
['Maroubra Beach',-33.950198,151.259302,1]
];

var map = new google.maps.Map(document.getElementById('map'),{
zoom:10,
center:new google.maps.LatLng( - 33.92,151.25),
mapTypeId:google.maps.MapTypeId.ROADMAP
});

var infowindow = new google.maps.InfoWindow();

var marker,i;

(i = 0; i< locations.length; i ++){
marker = new google.maps.Marker({
position:new google.maps.LatLng (位置[i] [1],位置[i] [2]),
map:map
});

google.maps.event.addListener(marker,'click',(function(marker,i){
return function(){
infowindow.setContent(locations [i ] [b]);
infowindow.open(map,marker);
}
})(marker,i));
}
< / script>
< / body>

< / html>

所以我的问题是,如何知道具有动态位置的地图中心。我已经试过把中心留空,但是地图没有加载。

解决方案

首先,您可以创建 LatLngBounds 对象,方法是将所有动态生成的位置都包含在内。使用扩展方法来包含点。然后,您可以使用getCenter方法获取绑定的中心。



更新:

代码:

  var bound = new google.maps.LatLngBounds(); 

for(i = 0; i< locations.length; i ++){
bound.extend(new google.maps.LatLng(locations [i] [2],locations [i ] [3]));

//其他代码
}

console.log(bound.getCenter());

插图


I have just copied the code on this question and applied my latitude and longitudes. However, the latitudes and longitudes will be dynamic, and the center of the map will change depending on the latitudes and longitudes of the locations.

The following is the code from the other question

<!DOCTYPE html>
<html>

    <head>
        <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
        <title>Google Maps Multiple Markers</title>
        <script src="http://maps.google.com/maps/api/js?sensor=false" type="text/javascript"></script>
    </head>

    <body>
        <div id="map" style="width: 500px; height: 400px;"></div>
        <script type="text/javascript">
            var locations = [
                ['Bondi Beach', -33.890542, 151.274856, 4],
                ['Coogee Beach', -33.923036, 151.259052, 5],
                ['Cronulla Beach', -34.028249, 151.157507, 3],
                ['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
                ['Maroubra Beach', -33.950198, 151.259302, 1]
            ];

            var map = new google.maps.Map(document.getElementById('map'), {
                zoom: 10,
                center: new google.maps.LatLng(-33.92, 151.25),
                mapTypeId: google.maps.MapTypeId.ROADMAP
            });

            var infowindow = new google.maps.InfoWindow();

            var marker, i;

            for (i = 0; i < locations.length; i++) {
                marker = new google.maps.Marker({
                    position: new google.maps.LatLng(locations[i][1], locations[i][2]),
                    map: map
                });

                google.maps.event.addListener(marker, 'click', (function(marker, i) {
                    return function() {
                        infowindow.setContent(locations[i][0]);
                        infowindow.open(map, marker);
                    }
                })(marker, i));
            }
        </script>
    </body>

</html>

So my question is, how could I know the center of the map when having dynamic locations. I've tried leaving the center blank but the map didn't load.

解决方案

First you can create a LatLngBounds object by including all the dynamically generated locations. Use the extend method to include the points. Then you can get the center of the bound using the getCenter method.

UPDATE:

Code:

var bound = new google.maps.LatLngBounds();

for (i = 0; i < locations.length; i++) {
  bound.extend( new google.maps.LatLng(locations[i][2], locations[i][3]) );

  // OTHER CODE
}

console.log( bound.getCenter() );

Illustration:

这篇关于在Google地图中找到多个地点的中心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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