02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Calculating Latitude and Longitude from Map Points

Calculating Latitude and Longitude from Map Points in React

In this lesson, we’ll transform click points from a map into latitude and longitude. This foundational knowledge is critical for integrating geographic data with web services.


Step 1: Understanding Latitudes and Longitudes

  • Latitude: Measures north-south position (±90°).
  • Longitude: Measures east-west position (±180°).
  • Map coordinates are centered at (0, 0) with positive and negative ranges to the edges.

Step 2: Setting Up Latitude and Longitude Calculations

  1. Open your Map.js file and locate the onClick method.
  2. Add calculations for latitude (lat) and longitude (lon):
onClick(e) {
  let img = e.target; 
  let point = {
    x: e.nativeEvent.offsetX,
    y: e.nativeEvent.offsetY
  };

  let latSize = img.height / 180; // Height per degree
  let lonSize = img.width / 360; // Width per degree

  let lat = -(point.y - img.height / 2) / latSize; // Flip y-axis
  let lon = (point.x - img.width / 2) / lonSize;

  console.log(`Latitude: ${lat}, Longitude: ${lon}`);
}

Step 3: Debugging and Testing Coordinates

  • Debugging: Use console.log to confirm the accuracy of your latitude and longitude values.
  • Test the outputs by clicking various areas of the map.
    • Top of the map: Positive latitude.
    • Bottom of the map: Negative latitude.
    • Right edge: Positive longitude.
    • Left edge: Negative longitude.

Step 4: Preparing for Web Services

  • These calculated values are essential for querying APIs or web services that rely on geolocation.
  • Accuracy isn’t critical for this exercise, but ensure logical consistency when transforming coordinates.

Key Takeaways

  • Learn to divide map dimensions into geographic coordinates.
  • Debug effectively to ensure accurate latitude and longitude values.
  • Build foundational skills for integrating map data into dynamic applications.

In the next lesson, we’ll explore ES6 string templates to simplify the creation of dynamic URLs for web service requests.

Explore the full course here: Mastering ReactJS: Data Visualization

Video Transcript

00:01In the last lecture, we fetch the point on the map,
00:04the XY coordinates of the map,
00:06but we need to transcribe that into latitude
00:09and longitude, which is exactly
00:10what we're gonna do in this lecture inside
00:13of our react component.
00:15Alright, so the next step now
00:17that we have points on our map is
00:19to actually convert them into latitudes and longitudes.
00:24Now generally speaking, latitudes
00:26and longitudes are literally just taking the world map
00:30and splitting it in half.
00:32And the center point when we go up
00:35or down would affect the latitude if we go left
00:39or right, would affect the longitude.
00:42Now the latitude is really divided into 90 degrees
00:47up or 90 degrees down,
00:49while the longitude is 180 degrees right,
00:53or 180 degrees left.
00:56With that said, our starting point is at zero zero in the
01:00center of our map.
01:01This is a accurately set up
01:03or relatively accurately set up map.
01:07So let's go ahead and
01:08what I'm gonna do is I'm gonna go into our let
01:10and add a few more properties.
01:12One is I want to define the actual size of a latitude,
01:16so I'm gonna call that lat size.
01:18Now our lat size is gonna basically be our image it.
01:23Our image is height divided by 180,
01:26because we said it's 90 degrees to either point.
01:29So I need to figure out how much is actual each step in
01:32latitudes for longitudes, same story applies only this time.
01:36We need to take our image width
01:39and divide it by 360 to give us a full circle really
01:43and give us the full longitude.
01:46Okay? So we figure it out our latitudes size,
01:49you could say each latitude.
01:51Each single latitude.
01:52And so we've done Otto for our longitude.
01:56So the next step for us is to try
01:57to figure out the actual longitude and latitude.
02:01Now, I can't do this up here
02:02because our point might not be configured yet,
02:04so I'm just gonna go ahead and declare a la an Otto along,
02:08but not actually convert them and build them out.
02:12Once we're done setting up our point, it's time for us to go
02:15to that lat and try to figure it out.
02:17So first of all, the first point that I'm gonna need
02:20to do is I'm gonna take the actual point
02:22that I've fetched from the point creation,
02:25and let's start from the latitude,
02:26which would accept the why.
02:28Now for the latitude, I'm gonna take that Y
02:30and I'm gonna subtract out of it basically the point
02:34of the image, which is basically right slap in
02:36the middle of the image.
02:38So what I want to do is I'm gonna take my image dot height
02:42and I'm gonna divide it by two.
02:44Now that I've divided our height by two
02:46and I got my actual point distance from
02:51that actual point to that point, it's time for me
02:54to take all of that
02:55and divide it by my lat size. Now I need to do one
02:58More thing because I need to flip the direction.
03:02Currently, the direction is gonna be basically negative
03:07to positive when I want it to be the exact opposite
03:10because we're going through height
03:12and in our situation, this would be in the negative space
03:15and this would be in that positive state.
03:17But I really want to flip the script here.
03:19So I just need to put your A minus somewhere in the,
03:22in the process, I could put it right here.
03:24That works for me as well.
03:26So I'm basically switching the direction, making sure
03:29that a positive turns into a negative
03:31and a negative into a positive.
03:33And if I go ahead and test this out, just our latitude,
03:36and we just go ahead and click somewhere here, we'll see
03:38that as we are in the upper areas,
03:40the number is gonna be positive.
03:42And as we go down, the number will be negative all the way
03:45to 90 degrees.
03:46It's gonna be hard for us to get exactly 90,
03:48but we're gonna get very close to it.
03:50And again, our accuracy is not the most important thing
03:53right now, although it would be in a real world
03:55scientific application.
03:58Okay, so really, uh, we're gonna use,
04:01we're gonna use some sort of, we're gonna have to give, uh,
04:04we'll get to that when we're in the web service to try
04:06to give it a value that will be arranged
04:07to give us more possibilities of getting our value.
04:10So again, I'm gonna go ahead
04:11and also do the same thing for our longitude.
04:14So I'm gonna go ahead here and set that long.
04:16This time around, I'm gonna take the X position instead
04:19of the Y and subtract the width of our element.
04:23And last but not least, I don't need that minus
04:25because it's gonna go in the right direction naturally
04:28and use our lawn size instead.
04:32So if we go ahead here and we check our latitude
04:34and longitude, and I go ahead here
04:37and click on a far distance here, it should be negative
04:40for our almost 180 degrees exactly.
04:43And if we're in the center, it'd be close to zero.
04:45And if we're all all the way on the other edge,
04:47it should be roughly 180 degrees.
04:51So we got all our numbers combined and we got our latitude
04:54and longitude, and we're just about ready
04:57to call a web service
04:58and get our data to find out
05:01what is the actual content going on in here.
05:06In this lecture, we saw how to create our
05:08or con, convert our points into latitude, longitudes,
05:12and it's time for us to turn our attention
05:15to our web service.
05:16But just before we do that,
05:18let's explore some e ES six string templates
05:21to simplify the process of creating the URL
05:24for our web service.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!