【问题标题】:Find the midpoint of several locations using google map [duplicate]使用谷歌地图找到几个位置的中点[重复]
【发布时间】:2014-08-12 04:18:59
【问题描述】:

我为我的 Rails 应用程序编写了一个代码,使用以下代码在谷歌地图上显示一些位置:

var myOptions = {
        zoom: 2,
        center: new google.maps.LatLng(71.1333,27.7000, 13), // **I need to set the center from the locations in here.**
        mapTypeId: 'terrain'
    };
    map = new google.maps.Map($('#search_map_canvas')[0], myOptions);

    var addresses = <%=raw search_offering_addressess.to_json %>;
    for (var x = 0; x < addresses.length; x++) {
        $.getJSON('http://maps.googleapis.com/maps/api/geocode/json?address='+addresses[x]+'&sensor=false', null, function (data) {
            var p = data.results[0].geometry.location
            var latlng = new google.maps.LatLng(p.lat, p.lng);
            new google.maps.Marker({
                position: latlng,
                map: map
            });

        });
    }

这里,search_offering_addressess 包含一个位置数组。例如。 [“柏林,德国”,“苏黎世,瑞士”,....]

如何找到这些位置的中点?我的地图错过了一些位置标记。

【问题讨论】:

    标签: ruby-on-rails google-maps


    【解决方案1】:

    使用 LatLngBounds 试试这个

          //newly added
            var bounds = new google.maps.LatLngBounds();
            var myOptions = {
                    zoom: 2,
                    center: new google.maps.LatLng(71.1333,27.7000, 13), // **I need to set the center from the locations in here.**
                    mapTypeId: 'terrain'
                };
    
    
               map = new google.maps.Map($('#search_map_canvas')[0], myOptions);
    
                var addresses = <%=raw search_offering_addressess.to_json %>;
                for (var x = 0; x < addresses.length; x++) {
                    $.getJSON('http://maps.googleapis.com/maps/api/geocode/json?address='+addresses[x]+'&sensor=false', null, function (data) {
                        var p = data.results[0].geometry.location
                        var latlng = new google.maps.LatLng(p.lat, p.lng);
                        //newly added
                        bounds.extend(latlng);
                        new google.maps.Marker({
                            position: latlng,
                            map: map
                        });
    
                    });
                }
      // Automatically center the map fitting all markers on the screen 
      map.fitBounds(bounds); 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-27
      • 1970-01-01
      • 1970-01-01
      • 2010-12-08
      • 1970-01-01
      • 2012-12-22
      • 1970-01-01
      相关资源
      最近更新 更多