简体   繁体   中英

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. I have been using gmaps.js, with this script in the head:

<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.

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. You could try out the address using the GMaps playground here . When I used my long address, I discovered that it wasn't geocoding but it found a shorter address pretty quickly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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