简体   繁体   English

从坐标数组中,我如何决定以哪个坐标为中心?

[英]From an array of coordinates, how could I decide which to center the map on?

Assuming I am working with the following JSON of locations:假设我正在使用以下位置的 JSON:

var beaches = [
  ['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]
];

I am plotting these on a Google Map and I'd like to center on roughly the one in the middle.我在谷歌地图上绘制这些,我想大致集中在中间的那个。

Is there a function in the Google maps api for doing this?谷歌地图 api 中是否有用于执行此操作的函数? Or must I work out the average myself?还是我必须自己算出平均值?

I have written a function which I can pass the JSON and a zoom level to:我编写了一个函数,可以将 JSON 和缩放级别传递给:

function initialize_map(places, zoom) {

    // use first coordinates in the json to initiate the map
    var place_lat_lng = new google.maps.LatLng(places[0][1],places[0][2]);

    var mapOptions = {
          zoom: zoom,
          center: place_lat_lng,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        };

    var map = new google.maps.Map(document.getElementById('map_canvas'),
            mapOptions);

    var marker, i; 

    // loop through all the places to get markers
    for (var i=0;i<places.length;i++)
    { 
        var place = places[i];

         marker = new google.maps.Marker({
            position: new google.maps.LatLng(place[1], place[2]),
            map: map,
            animation: google.maps.Animation.DROP
        });
        // add the marker
        marker.setMap(map);            
    }   
}   

This function works, but it doesn't center, and markers will be out of view.此功能有效,但不会居中,并且标记将不在视野范围内。

How would you go about centering the view, and having it so all the markers are visible?您将如何使视图居中并使其所有标记都可见?

The Google Maps API v3 contains a method for displaying a set of markers : Google Maps API v3 包含显示一组标记的方法

fitBounds(bounds:LatLngBounds) - None - Sets the viewport to contain the given bounds. fitBounds(bounds:LatLngBounds) - None - 设置视口以包含给定的边界。

Add all your markers to a google.maps.LatLngBounds object, then call that method on your map object.将所有标记添加到 google.maps.LatLngBounds 对象,然后在地图对象上调用该方法。 Like this:像这样:

function initialize_map(places, zoom) {
     var bounds = new google.maps.LatLngBounds(); // empty bounds object

    // use first coordinates in the json to initiate the map
    var place_lat_lng = new google.maps.LatLng(places[0][1],places[0][2]);

    var mapOptions = {
          zoom: zoom,
          center: place_lat_lng,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        };

    var map = new google.maps.Map(document.getElementById('map_canvas'),
            mapOptions);

    var marker, i; 

    // loop through all the places to get markers
    for (var i=0;i<places.length;i++)
    { 
        var place = places[i];

         marker = new google.maps.Marker({
            position: new google.maps.LatLng(place[1], place[2]),
            map: map,
            animation: google.maps.Animation.DROP
        });
        // add the marker
        marker.setMap(map);            
        bounds.extend(marker.getPosition()); // add the marker to the bounds

    }   

    map.fitBounds(bounds); // show all the markers.
}

Here's a quick way to add the markers to a LatLngBounds and grab the center of the box.这是将标记添加到 LatLngBounds 并抓住框中心的快速方法。 This will at the very least show you all the markers you've got.这至少会向您显示您拥有的所有标记。 The rectangle and 'C' marker are to show you what the bounding box and center look like.矩形和“C”标记用于显示边界框和中心的外观。

var bounds = new google.maps.LatLngBounds();
for (x in beaches) {

    var data = beaches[x];       
    var lat = beaches[x][1];
    var lng = beaches[x][2]
    var latLng = new google.maps.LatLng(lat, lng);
    var marker = new google.maps.Marker({
        map: map,
        position: latLng
    });
    markers.push(marker);
    bounds.extend(latLng);
}

map.fitBounds(bounds)

new google.maps.Rectangle({
    bounds: bounds,
    fillColor: '#000000',
    map: map
})

var centerBounds = new google.maps.Marker({
    map: map,
    position: bounds.getCenter(),
    icon: 'http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=C|FF0000|000000'
});

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

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