When using the Geolocation API to obtain coordinates for an if statement in JavaScript, a variable cannot be found to trigger an event

I've been utilizing the Geolocation API to track a user's coordinates and then redirect them to the appropriate webpage based on their location. However, I'm having trouble setting up the if statement that will trigger the redirection. What specific variable should I use in the if statement to successfully direct the user to the correct page?

<button onclick="getLocation()">Get Location</button>
  <p id="demo"></p>

  <script>
  var x = document.getElementById("demo");

  function getLocation() {
    if (navigator.geolocation) {
      navigator.geolocation.getCurrentPosition(showPosition);
    } else {
      x.innerHTML = "Geolocation is not supported by this browser.";
    }
  }

  function showPosition(position) {
    x.innerHTML = "Latitude: " + position.coords.latitude +
  "<br>Longitude: " + position.coords.longitude;

  if (showPosition(latitude) > 34) {
      console.log("It worked");
  }
}
</script>

Answer №1

The issue lies in your usage of showPosition(latitude) as the function name instead of utilizing the parameter position, which contains the coordinate data.

if (position.coords.latitude > 34) {
   console.log("Success");
}

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

Transferring an array from PHP to Javascript using GET method

Can someone help me figure out how to pass an array to Javascript after it sends a GET request to PHP? I've got the data sending and retrieving down pat, but now I'm stuck on how to send the data back as an array. Here's what I have so far: ...

Utilizing Kendo for Dynamic HTML Element Connection

Currently, I am facing a situation where I have three HTML elements. The first is a textbox labeled shipToAddress, the second is another textbox called deliverToAddress, and finally, there is a checkbox named sameAsShipToAddress. In the background, there ...

Obtain information on the Node.js server transmitted from the Ajax jQuery on a client side

Currently, I am utilizing ajax jquery in order to transmit an image to the nodejs server. However, I have hit a roadblock when it comes to receiving the image on the nodejs server from the client. Here is the code snippet from the client: jQuery.noConfli ...

Obtain the distinct highest value for every vertical axis on a bar graph

I'm facing an issue with my code where I am appending multiple SVG elements and generating 3 different charts. The problem is that the maximum value evaluated from the y.domain() for each y-axis is taken as the maximum value from all of my data. Is t ...

Tips for efficiently importing modules from the mocha.opts configuration file

In my mocha unit tests, I am currently using the expect.js library by requiring it on the first line of each file. Here is an example: var expect = require('expect.js'); describe('something', function () { it('should pass&apo ...

Error: PHP is showing an undefined index error when trying to decode JSON, even though the value

I am feeling quite puzzled. I transferred a key value pair object from jQuery to PHP and successfully alerted it back out. However, when I visit the PHP page, it indicates that the data is either null or an undefined index. Here is my jQuery code: $(&ap ...

The issue with setting data in a Firestore collection using the collection.set method

I am facing an issue with my Google button, it allows users to sign in with their Google account. Everything seems to be working fine, except for the fact that I am unable to write user data to the Firestore database. When I check the Firebase auth page on ...

Clicking to reveal a v-date-picker v-menu and automatically focusing on a v-text-field within it?

I implemented a date-picker component in my app following the instructions from Vuetify. To enhance usability for desktop users, I removed the readonly attribute to allow manual input. Now, desktop users can easily navigate through form fields using th ...

Downloading EJS File instead of Displaying on Express.js Router

As I embark on the journey of developing a live video sharing/watching feature using Pure Node.js and without relying on any frameworks, an unexpected issue arose in the middle of the process. The problem occurred when Express started downloading an EJS fi ...

Apple on High Sierra page demonstrated an innovative use of grid parallax

I am attempting to create a unique parallax effect within a two-sided grid design. Apple's High Sierra page features an interesting parallax effect. ( ) Take a look at this image showcasing the parallax effect in action In this setup, the right sid ...

HTMLunit - error: [MobX] Unable to access proxy

Currently, I am faced with a challenge in extracting data from a JavaScript-based website using htmlunit. My approach involves calling the site through webClient.getPage, but this method is triggering an exception. public static void main(String[] args ...

Guidelines for combining and refreshing JSON documents in Node.js

In my current situation, I am dealing with two JSON documents that are retrieved from the backend. One document contains the grid definition, while the other contains a customized grid file for the user. The user may have modifications to the default value ...

Setting Javascript variables - listing elements

I have created a menu where I want to assign a variable to each li item and display an alert with its text when clicked. I managed to achieve this using the script provided below. $("ul#menudropd .animal li a").click(function() { var whatever = $(this ...

Understanding the Difference Between WARN and ERR in npm Peer Dependency Resolution

I encountered a puzzling situation where two projects faced the same issue, yet npm resolved them differently: https://github.com/Sairyss/domain-driven-hexagon npm ERR! code ERESOLVE npm ERR! ERESOLVE unable to resolve dependency tree npm ERR! npm ERR! W ...

Combining multiple objects in an array to create a single object with the aggregated sum value can be achieved using JavaScript

I am working with an array that contains numbers of array objects, and I need to merge these arrays into a single array with unique values for content and the sum of values for total as shown in the desired result below. Any assistance would be greatly app ...

Parallax scrolling in all directions

Is there a resource available for learning how to program a website similar to the one at ? I am familiar with parallax but can't seem to find any examples that resemble what they have done on that site. ...

Choose selections from a single item and utilize jQuery to alter each one

Currently, I am working on creating a quick buy feature for products that come with various variants within my product list. Each product has a specific set of available options which are selected from a dropdown menu using jQuery, passed to an input field ...

How can you resize a circle in Three.js without resizing its outline?

I'm currently using a THREE.Path to generate a Circular path and then utilizing a TubeGeometry to form a circle with transparent fill and an adjustable stroke thickness. My main query revolves around the process of scaling up the Circular path dynamic ...

Having trouble loading Angular JS json file with filters using $http

Currently, I am able to load a collection of JSON data using the code below: $scope.properties = [{ title: "Tai's Bull Country Inn", property_type: ['pub','hotel','bandb','restaurant','selfcatering ...

Set markers at specific locations like "breadcrumbs" and generate a route using Google Maps API v3.exp

I'm developing a new iOS application for scouting out locations while on the move. I want users to be able to mark each location by simply clicking a button, which will drop a marker based on their current location. The ultimate goal is to connect all ...