How can I integrate a Google Maps instance into my Rails views?

I have a file named my_app.js.coffee and it includes the following Google Maps setup:


map = undefined
initialize = (map) ->
    myOptions =
      center: new google.maps.LatLng 39.729001, -94.902342
      zoom: 3,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    map = new google.maps.Map $('.map')[0], myOptions

  $ ->
    initialize(map)

  return

When I use a DIV with a class of map in a view, the map is displayed. Pretty cool.

However, there are times when I need to change the center of the map. In the corresponding view, I attempted the following:

<script type="text/javascript"> 
  map.setCenter(43.652976, -79.390409);
</script>

Unfortunately, this does not update the center of the map. Instead, an error message appears in the console:

Uncaught ReferenceError: map is not defined

What am I overlooking here?

Thank you in advance.

EDIT:

buildMap = (callback = ->)->
    console.log('xxx')
    myOptions =
      center: new google.maps.LatLng 39.729001, -94.902342
      zoom: 3,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    console.log('b1')
    Gmaps.map = new google.maps.Map $('.map')[0], myOptions
    console.log('b2')
    #addMarkers(Gmaps.map)
    callback()

console.log('ccc')
Gmaps =
  buildMap: buildMap

window.Gmaps = Gmaps

Answer №1

map is designated as a local variable within your file.

An alternative solution would be to substitute it with window.map

Consider the scope of your variables and dependencies. It's a complex subject.

Firstly, I would approach it like this:

Gmaps = 
  buildMap: (callback = ->)->
    myOptions =
      center: new google.maps.LatLng 39.729001, -94.902342
      zoom: 3,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    Gmaps.map = new google.maps.Map $('.map')[0], myOptions
    callback()
    
window.Gmaps = Gmaps

then to initialize the map in your views:

Gmaps.buildMap() 

to create and adjust center:

Gmaps.buildMap(function(){
  Gmaps.map.setCenter(43.652976, -79.390409);
}) 

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there a way to verify if the password entered by the user matches the input provided in the old password field?

I am trying to compare the user's password with the one entered in the "oldPassword" input field. The challenge is hashing the input from the "oldPassword" field for comparison. How can I achieve this? Please review my ejs file and suggest improvement ...

creating an identical copy of a dynamic element's size

I am currently working on a project using react js where I need to incorporate multiple images. My goal is to have standard background divs / rectangles that match the exact size of each image, allowing for animations to be performed on top of them. The sn ...

Conceal an item if the span element encompasses a div with a specific class

I am struggling with a problem where I need to hide an "i" element only if a "span" contains a "div" element. The content inside the "span" is loaded via AJAX and rendered as HTML, so it may contain the "div class=difference" or not. The code snippet in q ...

AngularJS functionality ceases to function once additional HTML content is injected using AJAX

Upon loading the page for the first time, my angular functions perfectly. However, when I attempt to use the same functionality after loading my html via ajax, it does not work at all. There are no relevant errors visible in the console. Javascript: var ...

Having Trouble with Form Submission Button Across Different Web Browsers

Having some trouble with my form - all fields are properly closed with tags, but when I click the submit button, nothing happens. The page is loaded with code, so here's the link for you to check it out. Unfortunately, right-click is disabled, so ple ...

Error encountered in vue.js due to a ReferenceError when the resize event is used

I'm having trouble obtaining the window height when resizing, and I keep encountering the error ReferenceError: calcOfSliderHeight is not defined. Can someone explain what's happening? Here is my code: let example = new Vue({ el: '#exam ...

How is it possible that I am receiving two different outputs, with one of them even being undefined

I created a button using Jquery to submit POST data. It sends the value "name" : "kevin" in JSON format. $(document).ready(function() { $('#clickMe').on('click', function() { var data = {}; data.name="kevin"; ...

Displaying Vue.js tooltips in a table when the text gets clipped

I'm currently facing an issue with creating a tooltip in my vue.js document. I attempted to follow this guide from https://www.w3schools.com/howto/tryit.asp?filename=tryhow_css_tooltip in order to create one, but it's not working as expected. Her ...

Enhance local rotation of objects in Three.js

I am working on creating a spaceship-like object with controls, and I have learned that I need to use quaternions to achieve this. To start, I am attempting to make a cube rotate to the left when the A key is pressed. Here is the code I have written for th ...

Having trouble performing an Image (base64) update in Next.js

Hi everyone, I'm facing a challenge with updating the image data in my code. The base64 string data updates correctly and the new image is displayed before submitting the data. However, once I submit the data, the image doesn't update. Below is ...

How can I check if response.Text matches a specific String in an ajax call?

I am facing an issue where I am receiving data from a servlet in an AJAX function. Within this function, I am attempting to compare the response.Text with a certain String value, for example 'x'. However, the comparison is not working as expected ...

Avoid reactivating focus when dismissing a pop-up menu triggered by hovering over it

I am currently utilizing @material-ui in conjunction with React. I have encountered the following issue: In my setup, there is an input component accompanied by a popup menu that triggers on mouseover. Whenever the menu pops up, the focus shifts away from ...

Issue: Unable to locate the module 'babel-code-frame' in VUEJS (ESLINT)

Here are the current versions: -npm: 6.14.4 -node: v10.19.0 -eslint: v5.0.1 -linux: ubuntu 20.04 This is my script: vue create vue1 cd vue1 npm run serve This is my package.json: { "name": "vue1", "version": "0. ...

What methods are available to incorporate arithmetic when defining a style attribute?

Is there a way to achieve arithmetic operations in CSS, like calculating margin-left: -60px + 50% of the width of the parent div? I'm eager to find a solution, whether it involves JavaScript or any other method. Any help would be greatly appreciated. ...

Spring Boot is having trouble identifying the JavaScript files

I've incorporated Spring Boot in my project and I'm working with a JSP file that looks like this: <%@ include file="common/header.jsp" %> <%@ include file="common/navigation.jsp" %> <div class="container"> ...

Easily transform checkboxes into images using jQuery with no need for external plugins

Is it possible to replace checkboxes with images without using jQuery plugins? I'm hoping to achieve this in just a few lines of code. Thank you. ...

Performing Batch Writes in Firestore using the Admin SDK

I have a massive ASCII flat file containing 1.5 million lines, which is essentially a list of parts from a manufacturer. I want to store this data in Firestore. Originally saved as a .csv file, the size was 250GB. After converting it to a JSON file using ...

The $.ajax request encounters an error when trying to parse the data as JSON

I am encountering an issue where my ajax call to a JSON file fails when using dataType: "json". However, changing it to "text" allows the ajax call to succeed. See the code snippet below: $.ajax({ type: "POST", url: url, dataType: "json", ...

Mocha test failing to trigger function execution

I've been developing an Express.js application that includes a feature for creating appointments with a post request. This feature involves retrieving and saving data from a third-party API, followed by sending updated API data in the subsequent reque ...

Obtain a filtering dropdown list directly from the database within Ag-grid

Currently in my interface, I am attempting to implement a filter for the FOLDER column. This filter is supposed to retrieve data from the database and present it in a dropdown checkbox within that column. The filtering should be based on the selected data. ...