简体   繁体   English

gmaps.js地理编码器无法正常工作?

[英]gmaps.js geocoder not working?

I am trying to building a Rails blog that displays a location on a google map using an address belonging to the object in the database. 我正在尝试建立一个Rails博客,使用属于数据库中对象的地址在Google地图上显示位置。 I have been using gmaps.js, with this script in the head: 我一直在使用gmaps.js,该脚本位于头部:

<head> <script src='https://maps.google.com/maps/api/js?sensor=true'></script> </head>

And the partial that I am rendering looks like this: 我渲染的部分看起来像这样:

 <script>
  var map = new GMaps({
  div: '#map',
  lat: -12.043333,
  lng: -77.028333,
  scrollwheel:  false,
  draggable: false,
  clickable: true
});

  GMaps.geocode({
  address: "<%= @post.address %>",
  callback: function(results, status) {
    if (status == 'OK') {
      var latlng = results[0].geometry.location;
      map.setCenter(latlng.lat(), latlng.lng());
      map.addMarker({
        lat: latlng.lat(),
        lng: latlng.lng()
      });
    }
  }
});
   </script> 

Right now the partial displays a map, but it does not display the location that is associated with the @post, even though <%= @post.address %> does display the address outside of the map context. 现在,partial显示地图,但即使<%= @post.address %>确实在地图上下文之外显示地址,它也不显示与@post关联的位置。

Can anyone help me with this? 谁能帮我这个? I'm sure it must be a relatively simple problem, but I can't figure it out. 我敢肯定这一定是一个相对简单的问题,但我无法弄清楚。 Any help would be very much appreciated. 任何帮助将不胜感激。

Thanks in advance. 提前致谢。

I think you should check that the address could be geocoded with Google's API. 我认为您应该检查该地址是否可以使用Google的API进行地理编码。 You could try out the address using the GMaps playground here . 您可以在此处使用GMaps游乐场尝试地址。 When I used my long address, I discovered that it wasn't geocoding but it found a shorter address pretty quickly. 使用长地址时,我发现它不是地理编码,但是很快就找到了一个较短的地址。

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

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