The Google Maps API v3 in Django fails to function properly on Chrome browsers when incorporating template variables

I need to pass latitude and longitude variables to my template in order to display a location on Google Maps. To do this, I am using template variables within JavaScript:

<script type="text/javascript">
  var map;
  function initMap() {
      map = new google.maps.Map(document.getElementById("map"), {
      center: {lat: {{ m_city.location.y }}, lng: {{ m_city.location.x }}},
      zoom: 10
      });
  }
</script>

<script src="https://maps.googleapis.com/maps/api/js?key=KEY&callback=initMap"
async defer></script>

While everything works fine in Firefox, Chrome (v.42) does not display the map at all. Strangely enough, when I hard code the generated latitude and longitude values into the template, Chrome shows the map as expected.

Can anyone help me understand why this discrepancy is occurring?

UPDATE

After Selcuk pointed out in the comments, it became clear that the issue was with the decimal separator being a comma instead of a period. This discrepancy arose due to different language settings in different browsers. By setting USE_L10N to False, the problem was resolved.

Answer №1

To ensure seamless format localization without disrupting your javascript code, I recommend utilizing the localize templatetag.

{% localize off %}
    <script type="text/javascript">
      var map;
      function initMap() {
          map = new google.maps.Map(document.getElementById("map"), {
          center: {lat: {{ m_city.location.y }}, lng: {{ m_city.location.x }}},
          zoom: 10
          });
      }
    </script>
{% endlocalize %}
<script src="https://maps.googleapis.com/maps/api/js?key=KEY&callback=initMap"
async defer></script>

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

What is the best way to display two timers on one page?

I'm having an issue with displaying two timers for a 3-minute countdown. The problem is that the second timer works perfectly, but the first timer doesn't. Even if I remove the second timer, there is still some problem with the first one. The cod ...

Building a loading spinner component in a react-native project

I have successfully implemented a loading spinner that is currently being used in various components of my React project. However, as I am now working on a react-native version of the application, I am faced with the challenge of recreating this loading sp ...

Alter the font color upon clicking the menu using jQuery

When I click on the menu and sub-menu items, I want to change their colors along with their parent. You can see an example of how I want it to work here. However, currently, when I click on a sub-menu item, the color of the parent menu item gets removed. ...

Is there a way to adjust the size of an iframe that includes an external source while also shifting the contents?

How can I achieve the following tasks: Delay loading of an external iFrame Adjust the dimensions of an externally sourced iFrame (e.g., 100px x 40px) Position an externally sourced iFrame off-center (e.g., 25px x 50px) Here's a code snippet example ...

Transforming a JSON object into a list in C#

After exploring similar posts without success, I am reaching out here for help. I have a Json data stored in a hidden field that I am trying to access in the code behind file of my markup page. My goal is to convert this Json into a List and bind it to a ...

How can you incorporate AJAX to add a paragraph element to your HTML document?

I have encountered an issue with two files in my project. The first file is an HTML document, while the second file is a PHP file called ajax_access_database.php. Originally, I intended for the PHP file to access a database, but complications arose, leadin ...

Running a designated AJAX function from a variable by utilizing Applescript

Struggling to execute a JavaScript code within a page using Applescript. In the JavaScript, an AJAX function was defined like so: var myFunction = function () { // Here be the code... } I attempted this in Applescript: do JavaScript "document.myFunct ...

What are the steps to pause and restart my countdown timer?

I'm attempting to create a countdown timer with the following 4 functionalities: Change button to 'stop' when 'start' is clicked Stop the timer when 'stop' is clicked Show the 'start' button when the timer is ...

Focus on selecting just one button within Angular

By retrieving values from a database and displaying them using ng-repeat within a div: <div ng-controller = "myTest"> <div ng-repeat="name in names"> <h4>{{name.name}}</h4> <button ng-class="{'active ...

I am facing an issue with my sequelize model where the "before create" functionality is not

Having issues with my before create hook in Sequelize model I've set up a template, but my before create hook doesn't seem to be working and I'm struggling to figure out why. async createUser(req,res){ const { name,email,login,password ...

Sending the axios fetched property from the parent component to the child component results in the error message "TypeError: Cannot read property 'x' of undefined"

I've noticed that this question has been asked before, but none of the solutions provided seem to work for my situation. Parent component import axios from "axios"; import { useEffect, useState } from "react"; import Child from &q ...

Initiating an AJAX request to communicate with a Node.js server

Recently, I started exploring NodeJS and decided to utilize the npm spotcrime package for retrieving crime data based on user input location through an ajax call triggered by a button click. The npm documentation provides the following code snippet for usi ...

Create your masterpiece on a rotated canvas

My goal is to draw on a canvas using the mouse, even after rotating and scaling the canvas container. The issue I am facing is that the mouse coordinates get affected by the rotation and scaling, making it difficult to draw correctly. I have tried switch ...

Add a close button to an Angular directive

I'm currently working on creating an alert message with the following layout: <div class="alert"> <p>Some alert content!</p> </div> Everything is looking good so far. Now, I want to enhance this alert by adding a close bu ...

Retrieve text content from a specified element on a different page, not the one currently loaded, by

After implementing this dynamic content script, I realized that it is working well for the current page. However, since I cannot access the code-behind in the asp.net project, I am facing an issue when trying to fetch the text from "div.sampleheading" on t ...

Encountering an issue while attempting to transmit Electron window data

Hey there! I'm currently in the process of transitioning my project from a web browser to a GUI-based app using electron. I came across this video which was really helpful in getting started. The tutorial uses yarn, but I prefer npm 9.2.0. Initially, ...

Adding rows to a Datatable using an object array in rows.add()

Attempting to refresh my current Datatable by fetching new data using an Ajax function. Despite trying various solutions from other sources, the table does not update as expected. The function being used is shown below: $.ajax({ url: "<?php echo s ...

Encountered an issue retrieving audio during recording with Recorder.js

I've come across a scenario where I'm utilizing the Recorder.js Demo for recording audio. Interestingly, it works perfectly on Linux but encounters an issue when used on Windows. A pop-up alert stating "Error getting audio" shows up. Here's ...

Is it appropriate to delete the comma in the Ghost Handlebars Template?

While navigating through the tags, I unexpectedly encountered a comma. This could potentially have an unwanted impact. I attempted to remove the comma, but is there a specific method to eliminate it completely? I referred to the Ghost blog Document for gui ...

Updating the value of a specific property within an object in an array

I'm in the process of developing a stock trading application, and I have an empty array to store the stocks that are bought. Each time a stock is purchased, it adds an object to the array. If the same stock is bought again, I want to check for that sp ...