0

我使用 gmaps4rails gem 在我的 rails 应用程序中显示地图。有些部分用户可以输入位置、范围和半径类型。

例子:

location => Chicago, IL,
within => 10, 
radius type => km(kilometer).

当用户单击“更新评论”链接时,我想更新圆的标记和半径。我已经使用以下语法替换了标记:

function add_marker(location_served) {
    var location = $(location_served).val();
    var geocoder;

    geocoder = new google.maps.Geocoder();
    geocoder.geocode( {
        'address': location
    }, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            Gmaps4Rails.replace_markers([{
                "description": "",
                "title": "",
                "longitude": results[0].geometry.location.lng(),
                "latitude": results[0].geometry.location.lat(),
                "picture": "",
                "width": "",
                "height": ""
            }]);
        } else {
            alert("The location you've entered is invalid");
        }
    });
}

但我对如何更新圆半径一无所知。我在 中看到create_circle了方法gmaps4rails.js,但我不知道在使用该方法时应该传递的参数。

有人知道如何动态更新圈子吗?

谢谢

4

2 回答 2

2

下面的答案中没有显示圆圈的原因是 lat/lng 的标签:

//brute way to do
...
Gmaps4Rails.circles = [{"latitude":  , "longitude": , "radius":}];

应该

Gmaps4Rails.circles = [{"lat":  , "lng": , "radius":}];

根据 gmaps4rails.base.js 第 165 行

于 2011-08-28T07:15:58.907 回答
0
function add_marker(location_served) {
  var location = $(location_served).val();
  var geocoder;

  geocoder = new google.maps.Geocoder();
  geocoder.geocode( {
    'address': location
  }, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
     Gmaps4Rails.replace_markers([{
        "longitude": results[0].geometry.location.lng(),
        "latitude":  results[0].geometry.location.lat()
     }]);

     //brute way to do
     Gmaps4Rails.clear_circles();
     Gmaps4Rails.circles = [{"latitude":  , "longitude": , "radius":}];
     Gmaps4Rails.create_circles();

     //soft way (if you only want to update radius)
     Gmaps4Rails.circles[0].google_object.setRadius(number_here); //beware in the latest version of the gem I replaced google_object with serviceObject 

    } else {
      alert("The location you've entered is invalid");
    }
  });
}
于 2011-07-03T14:24:09.707 回答