Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7807597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T02:54:01+00:00 2026-06-02T02:54:01+00:00

Rails, Google maps, Javascript and JSON all at once. So I apologize if this

  • 0

Rails, Google maps, Javascript and JSON all at once. So I apologize if this is a simple question.

I have a rails app with a database of listings that each have a latitude and longitude. What I want to do is when the page loads, I would like to show all the listings on the map.

So I have the listings_controller.rb, application.js, index.html.erb as follows. Im using this book as a reference for my code, but the problem is its examples use Google maps version thats deprecated.

The steps I need to execute with my code are as follows:

  1. index.html.erb executes code in application.js
  2. application.js calls a function in listings_controller.rb which takes all the listings in the Listing model and converts them into a giant JSON object.
  3. application.js now has the JSON array of listings and parses it within a loop and extracts the name of the listing, its latitude and longitude and some other fields and passes this information to the javascript method “addMarker(latitude, longitude, description, map)”

The problem is I only know how to do parts of these steps as listed below. I was wondering if someone can fill in the holes and help me.

index.html.erb

<!DOCTYPE html>  
<html>  
  <head>  
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />  
    <style type="text/css">  
      html { height: 100% }  
      body { height: 100%; margin: 0; padding: 0 }  
      #map_canvas { height: 100% }  
    </style>  
    <script type="text/javascript"  
      src="http://maps.googleapis.com/maps/api/js?key=AIzaSyATZJn-p3mhbyk9JmR8mevKAbtrx4ZzPiQ&sensor=false">  
    </script>  
    <%=javascript_include_tag 'application' %>  

  </head>  
  <body onload="initialize()">  
    <div id="map_canvas" style="width:80%; height:80%"></div>  
  </body>  
</html>  

listings_controller.rb

def index   
    @listings = Listing.order(:name)  
    self.all_listings_json      
end  

def all_listings_json  
   render :text=>(@listings).to_json  
end  

application.js

function initialize() 
  {  
    options =   
    {   
      center: new google.maps.LatLng(vlat,vlong),  
      zoom: 15,  
      mapTypeId: google.maps.MapTypeId.ROADMAP  
    };  

    map = new google.maps.Map(document.getElementById("map_canvas"), options);  

    //get JSON object         
    var markersArray = getMarkersJSON();

    //pass JSON object to method and add listings in array to map
    addMarkerArray( markersArray , map);
   }

function getMarkersJSON()  
{  
    //To do  

}  

function addMarkerArray( markersArray, map)  
   {
    var i;
    for(i = 0; i < markersArray.length ; i++)
    {
        //To DO

    }

   }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T02:54:02+00:00Added an answer on June 2, 2026 at 2:54 am

    So you already have the JSON object by the time you build your map?

    Then all you need to do is to run trough your object:

    var myJSON = [
    {lat : 10.010232,long: 34.123232},
    {lat : 23.122323,long: 76.343434},
    {lat : 43.123434,long: 74.12343}
    ]; //Test data
    
        function addMarkerArray( markersArray, map)
        { 
            for(var i = 0; i < markersArray.length ; i++) 
            {
              var latitude = markersArray[i].lat;
              var longitude = markersArray[i].long;
              var myLatlng = map.LatLng(latitude,longitude);
              var marker = map.Marker({
                  position: myLatlng,
                  map: map,
                  title:"One title"
              });
            }
        }
        addMarkerArray(myJSON,map);
    

    You can check the Google Maps documentation here:
    https://developers.google.com/maps/documentation/javascript/overlays

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been working on a Google Maps based Rails app and it was working
I'm currently writing a Rails 3 app that uses the google maps API (v3)
I'm using the Google Maps Javascript API so that I can have someone put
In my Rails app, the background consists of a fullscreen div with Google Maps
I'm trying to transfer Rails-generated json into Google Maps markers on a map, but
I have Google this question for almost a whole day, but still not answer.
My question is about thinking through Rails app. I have a rich ajax-application which
I was playing with https://github.com/apneadiving/Google-Maps-for-Rails and i'd like to clear all the marker on
I have installed the sweet Google-Maps-for-Rails plugin, which seems to be under active development.
I am using Omniauth 1.0.1 in my Rails app for authentication. I have omniauth-google-oauth2

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.