尝试使用表单将javascript输入到gmaps4rails中

时间:2014-03-21 15:22:45

标签: javascript ruby-on-rails ruby gmaps4rails

我一直在Uncaught TypeError: Cannot call method 'geocode' of undefined。地址显示我在表单上输入的内容,但geocodeAddress功能并未添加我想要的新标记。

这是表格

%form.center{:action => "", :method => "GET", :name => "myform"}
    .large-4.columns
      %label.right.inline{:for => "right-label"} Enter your Location
    .large-8.columns
      .row.collapse  
        .small-10.columns
          %input#right-label{:placeholder => "e.g 131 Bobby St Orlando Fl", :name => "inputbox", :type => "text", :value => ""}
        .small-2.columns
          %input{ :name => "button1", :class => "button postfix", :value => "Find", :onClick => "geocodeAddress(this.form)"}

的Javascript

var handler = Gmaps.build('Google', { markers: { maxRandomDistance: null }, builders: { Marker: InfoBoxBuilder} });
  handler.buildMap({ internal: {id: 'geolocation'} }, function(){
    if(navigator.geolocation)
      navigator.geolocation.getCurrentPosition(displayOnMap);
  });

  function displayOnMap(position){
    var marker = handler.addMarker({
      lat: position.coords.latitude,
      lng: position.coords.longitude,
      infowindow: 'You!',
      picture: {
                    "url": "https://addons.cdn.mozilla.net/img/uploads/addon_icons/13/13028-64.png",
                    "width":  36,
                    "height": 36
                },
    });
    handler.map.centerOn(marker);
    handler.getMap().setZoom(17);
    markers = handler.addMarkers(#{raw @hash.to_json});
  };

var geocoder;

function initialize()
{
    geocoder = new google.maps.Geocoder();
}

function geocodeAddress(form){
    address =form.inputbox.value;

    geocoder.geocode( {'address': address}, function(results, status) //this is where it is showing the error
    { 
      if ( status == google.maps.GeocoderStatus.OK)
      {
        handler.map.centerOn(results[0].geometry.location);
        marker = handler.addMarker(results[0].geometry.location);
      }
      else
      {
        alert("Geocode was not successful for the following reason: " + status);
      }
    });
}

任何帮助,为什么会发生这种情况将不胜感激。

1 个答案:

答案 0 :(得分:1)

您有一个initialize功能设置geocoder,但是......您永远不会调用它。

所以geocoderundefined,繁荣。