Using both the variable and the JSON format from the Google Maps API

I am trying to display the coordinates from the input fields on a map using Google Maps API. However, I am facing an issue with passing the values to the script.

Below is the code snippet:

<input type="text" id="latitude" class="form-control" />        
<input type="text" id="longitude" class="form-control"/>
<div id="myMap"></div>
<script>

var cx = document.getElementById("latitude").value;
var cy = document.getElementById("longitude").value;
  function initMap() {
    var location={lat : cy , lng : cx};

    var map = new google.maps.Map(document.getElementById('myMap'), {
      zoom: 4,
      center: location
    });
    var marker = new google.maps.Marker({
      position: location,
      map: map
    });
  }
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?
key=MY_API_KEY&callback=initMap">
</script>

The issue lies in

var location={lat : cy , lng : cx};

The API key is correct, but has been omitted for security reasons.

Answer №1

After some work, I managed to figure it out by using the parseFloat() function on the string.

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

Failure to retrieve JSON using $.ajax in JSP

I am attempting to retrieve a JSON Array from a Servlet and populate data into a table. However, the JSON or text is not being received in the JSP despite trying various methods. Below is my JSP code: <%@ page contentType="text/html;charset=UTF-8" lan ...

:Incorporating active hyperlinks through javascript

Hey there, I've encountered a little conundrum. I have a header.php file that contains all the header information - navigation and logo. It's super convenient because I can include this file on all my pages where needed, making editing a breeze. ...

Developing a Node.js and Express REST API for generating tailored routes for custom fields

I'm currently using node.js and express framework to build my REST API server. One of the features I want to implement is similar to the Facebook graph API, where I can pass specific fields in my API routes like: /me?fields=address,birthday,email,do ...

Transforming the appearance of the menu element in Vue using transitions

In my Vue app, I have this SCSS code that I'm using to create a smooth transition effect from the left for a menu when the isVisible property is set to true. However, I am encountering an issue where the transition defined does not apply and the menu ...

Creating a dynamic JSTree that loads data on demand using Stored Procedures

Currently in my SQL Server database, I have two Stored Procedures that are responsible for extracting data from a tree structure: One procedure retrieves all nodes at a specific level based on the provided level number. The other procedure retrieves th ...

Solving complex promises in both serial and parallel fashion

My current function performs four tasks that must be executed in a specific sequence: - promise1 - promiseAfter1 // In parallel - promise2 - promiseAfter2 To ensure the promises are handled sequentially, I have structured two separate functions as follows ...

Issues with ng-click functionality in MVC partial view

I am currently working on a single page application that utilizes angular.js and MVC. Within the application, there are two partial views being called: Menu Accounts The Menu partial view loads successfully. However, I am encountering an issue with the ...

Analyzing the presence of the term "rain" in a JSON reply with Python

Striving to identify the presence of the word rain in a JSON response, I have devised a python script along with the respective JSON data and encountered an error message in the console. The objective is to analyze the content of 'main' and &apo ...

Refreshing a page occurs every 30 seconds or upon the user submitting a form

My PHP page consists of various includes for different sections of a video website. One of these sections is a comments area where users can submit their feedback to a database successfully. However, I want to ensure that only the specific included page/di ...

Error: Definitions missing during conversion from JSON to XML

I've scoured the internet extensively, but I still can't figure out why... The header file Newtonsoft.Json.Jsonconvert is missing the definition for DeserializeXmlNode(), which I need to convert Json to Xml. Additionally, the load() function do ...

What causes the server to give an incorrect response despite receiving a correctly read request?

After setting up a new project folder and initializing NPM in the Node.js repl, I proceeded to install the Express package. In my JavaScript file, I included the following code: const express = require('express'); const app = express(); ...

Displaying a dynamic splash screen during the resource-loading process on an Android application

I would like to implement an animated image (currently using a set of PNGs) as a splash screen while my resources are loading. I have successfully displayed the splash screen using this method. However, the issue I am facing is that the splash screen appe ...

What causes the variance in AJAX errors between IE and Chrome browsers?

Consider the code example provided below: <script> function xyz() { $.ajax({ type: "GET", url: "https://zx/xyz/uvw", timeout: 6000, dataType: "jsonp", ...

avoid loading javascript in ajax layer

After struggling for days to find answers online and unsuccessfully trying different codes, I discovered a way to use ajax to dynamically change the contents of a div element and display different sliders. Here are the files if you want to take a look: Fi ...

Clicking on a Vuetify v-btn with the :href attribute set to download will open the XML file

I'm having trouble getting the v-btn to download an XML file instead of opening it in the browser. <v-btn :disabled="!exportUrl" block x-large height="100" color="primary" :href="exportUrl" download> ...

Limiting the number of checkboxes selected in a Checkbox Group based on

I am working on a checkboxGroupInput that has 4 options (denoted as A, B, C, D). My goal is to restrict the selection to only 2 choices. The user should be able to pick a 3rd option. In this scenario, only the newly selected (3rd) and previously selec ...

CSS - Achieving full width on hover even when the element has a specified width setting

I am encountering an issue with my CSS. Although I have successfully centered everything, I am facing a problem with the hover effect in CSS. Whenever I apply the active class or hover over an li element, the background color expands to 100%. Additionally, ...

Integration of PHP and MySQL with PayPal can be challenging, especially when the PayPal button fails to function unless the amount is

When I initially set the amount value, clicking the "Pay with PayPal" button does not trigger any action. However, if I manually adjust the amount, the PayPal button starts functioning properly. This is how the input field appears: <input name="am ...

Encountering a hydration issue in Next.js when attempting to refresh the page after switching languages (excluding English) with next-translate/useTranslation

I've encountered an issue with the useTranslation hook from the next-translate package in my Next.js project. Although all languages are being recognized, I'm facing a hydration error whenever I change the language and refresh the page. Below is ...

Issues with injection of angularjs, sockjs, and angular-sockjs are causing functionality to not

As a newcomer to technologies like angular, sockjs-client, and cyclone, I've encountered an injection issue while attempting to utilize a component created by bendrucker. The component in question can be found at this link: https://github.com/bendruck ...