xamenrax
xamenrax

Reputation: 1744

Googlemaps for Rails search for place

Im using Gmaps4Rails gem in my rails app, so I just want to find out if it's possible to search for a place (I dont want to pass coordinates but name of country for example), so i need smth like on the pic below: enter image description here

Upvotes: 0

Views: 356

Answers (1)

manishie
manishie

Reputation: 5322

See the documentation: https://github.com/apneadiving/Google-Maps-for-Rails/wiki/Model-Customization

If you are doing a model based lookup, you would have your address in a field such as gmaps4rails_address. In your case, this would be simply the country.

You could have a boolean field called gmaps and set it to false, indicating that the address (country) has not yet been geocoded. Once it is geocoded, the lat & long will be stored in the appropriate model fields automatically, and the gmaps boolean will be automatically set to true so that the address is not geocoded again.

Update 1:

In response to your comment, your question is misleading. You said you wanted to pass the name of a country, which my solution does (e.g. the gmaps4rails_address field could contain 'Germany'). But it sounds like what you're looking for is to get a map outlining the boundaries of that country, which means you can't use the google maps api (it doesn't do that). So this gem (or any google maps gems) won't help you.

You need to use something like the Google Geomap javascript api. This can't be combined with a regular google map. The Geomap api will give you a flash map with the outlines of the countries.

From the documentation (https://developers.google.com/chart/interactive/docs/gallery/geomap):

<script type='text/javascript' src='https://www.google.com/jsapi'></script>
  <script type='text/javascript'>
   google.load('visualization', '1', {'packages': ['geomap']});
   google.setOnLoadCallback(drawMap);

    function drawMap() {
      var data = google.visualization.arrayToDataTable([
        ['Country', 'Popularity'],
        ['Germany', 200],
        ['United States', 300],
        ['Brazil', 400],
        ['Canada', 500],
        ['France', 600],
        ['RU', 700]
      ]);

      var options = {};
      options['dataMode'] = 'regions';

      var container = document.getElementById('map_canvas');
      var geomap = new google.visualization.GeoMap(container);
      geomap.draw(data, options);
  };
  </script>
</head>

<body>
  <div id='map_canvas'></div>
</body>

Upvotes: 1

Related Questions