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

Finding Points on a Map

Finding Points on a Map in React

In this lesson, you will learn how to capture user click events on a map and retrieve their coordinates. This involves leveraging React’s event handling, ES6’s let, and debugging techniques to ensure accurate results.


Step 1: Adding an Event Listener to the Map

  1. Open your Map.js file and update the render method to include an onClick event:
render() {
  return (
    <img 
      src="https://upload.wikimedia.org/wikipedia/commons/e/e3/World_map_blank_without_borders.svg" 
      alt="Map" 
      style={{ width: '100%' }} 
      onClick={this.onClick.bind(this)} 
    />
  );
}
  1. Create the onClick method within your Map class to handle the click event:
onClick(e) {
  console.log('Map clicked at:', e.nativeEvent.offsetX, e.nativeEvent.offsetY);
}

Step 2: Understanding ES6 let

  • Use let for variable declarations to limit their scope to the block they are defined in.
  • Example:
onClick(e) {
  let x = e.nativeEvent.offsetX;
  let y = e.nativeEvent.offsetY;
  console.log(`X: ${x}, Y: ${y}`);
}

This avoids variable leaks beyond their intended scope, ensuring cleaner and more predictable code.


Step 3: Handling Older Browser Support

  1. Check for browser compatibility with offsetX and offsetY.
  2. If unsupported, calculate coordinates manually using pageX, pageY, and offsets:
let point = { x: e.nativeEvent.offsetX, y: e.nativeEvent.offsetY };

if (typeof point.x === 'undefined') {
  point.x = e.pageX - e.target.offsetLeft;
  point.y = e.pageY - e.target.offsetTop;
}

console.log('Calculated coordinates:', point);

Step 4: Testing the Implementation

  1. Save the file and reload your development server.
  2. Open the browser and click anywhere on the map.
  3. Confirm the console logs the correct x and y coordinates.

Key Takeaways

  • React Event Handling: Use onClick to capture user interactions.
  • ES6 let Benefits: Scoped variables reduce errors in block-level code.
  • Cross-Browser Compatibility: Fallback logic ensures robust functionality.

By the end of this lesson, you have a functional map component capable of capturing click positions. In the next lesson, we’ll calculate latitude and longitude based on these coordinates.

Explore the full course here: Mastering ReactJS: Data Visualization

Video Transcript

00:00In the last lecture, we've created a map,
00:03but for us to make it interactive, we need to figure out
00:06where are the points the user is clicking on the map.
00:10In the process. We're also gonna explore L, which is new
00:13to E es six, and we're gonna talk about what it actually is
00:16and how it actually works.
00:17But I just wanna also say,
00:19although we're not gonna talk specifically about cons,
00:22the same rules apply to cons as they do to let,
00:24which we'll explore in this section.
00:27The only difference is, is when you define a variable,
00:29instead of using var
00:31and you're using cons instead it is a constant,
00:34which means once you set the value in,
00:36you cannot change it again, which is really,
00:38really good for debugging.
00:40But in this lecture, we'll take a look at
00:42Let and how it works.
00:44We're gonna debug things
00:45and we're also gonna look at how to work with events within
00:48React for us to get that points that we need for our map.
00:53Alright, let's continue.
00:55So the real, the the,
00:56the real goal really is if you take a look at this map,
00:59my goal is to be able to click on different regions
01:01and to be able to get the, I would say cities
01:06or locations in our nearby.
01:07And we're gonna use web services for that.
01:10But before we could do that, we need to figure out
01:12what the longitude and ude is.
01:16So let's go ahead and start the process.
01:18So inside of my map itself,
01:20what I'm gonna do is I'm gonna go to my map
01:22and what I want to do is I want to add
01:24to the map itself a click event.
01:27So let's go ahead and let's add here an on click.
01:31And I'm gonna tell it that when it clicks, we want
01:33to call this dot on click
01:36and obviously we would have to make sure
01:37that we create that method.
01:39So let's go ahead and create on click.
01:43And on click has an event.
01:45So I'm just gonna put here an E for event. Beautiful.
01:49Now we're already in a really good shape,
01:51but what we wanna do is we want to capture
01:55the actual XY positions that are,
01:57or the coordinates that our i i
02:01that our image is capturing.
02:03So to do that I'm gonna create a new let
02:06and let's our just like variables.
02:09The only difference is that a let contrary
02:11to a variable which is new to E ES six, um,
02:14is lives within the bounds of, I would say an enclosure.
02:19It's not in the bounds of an object, it's not in the bounds
02:22of a function, it's in the bounds of a object, so
02:26of, of a wrapping.
02:27So in, in other words, if I had here these open
02:30and closing curly brackets and I set here ette
02:33and let's say I call this name equals
02:35or you know, let's set it even to what we need it to be.
02:37For example, image. Now in the case of image, our image is
02:41inside of our IDA target.
02:44Now what will happen is if I do go to my console log
02:48and I log out that image right away in this spot,
02:52I'm gonna get that image even though we're sitting in
02:55supposedly an object or it's an organizer
02:57for that say it's not really an
02:58An object. For
02:59example, it could be here a case of a if statement.
03:02So if true, which will always happen,
03:05then I'm gonna set up a variable and set its target
03:08and log out our image.
03:10Now if we go into our browser, we'll see
03:12and if I open up our uh, inspector console, I will see
03:17that I'm gonna get, basically when I click on something,
03:21I'm gonna get my image beautiful exactly
03:23as we wanted it to be.
03:24But notice what's gonna happen if I take that image out
03:27and I call it outside of that if statement, if I do that
03:32and I try to run it again, we're gonna see
03:33that I'm not gonna actually get
03:35that value when I click on it, it's gonna tell me that Ima,
03:38that image reference is not defined.
03:40And that is really useful, especially in situations
03:43where you're only using something on a block level.
03:47Generally speaking, I would say use let all the time
03:50unless you need a variable
03:51because you need a scope that is larger
03:53from where you're at.
03:54But generally speaking, a let will definitely do.
03:57Okay, so now that we set our let
03:59and you know the difference between alet
04:01and Avar, I want go ahead and set our point.
04:05Now our point is really the X
04:06and Y coordinates of our mouse.
04:08So I'm gonna go ahead and use uh, new to uh, CS three type
04:13of property that is called offset X.
04:16And I'm gonna set oso our offset Y.
04:22Now our offset X
04:23and our offset Y should give us the values
04:25that are coming back from our X and Y coordinates,
04:28although the support is not fully integrated yet.
04:32And our react packages
04:33and our packages that basically included our, our
04:38poly fills don't really include that as far as I know.
04:43But even if they did, assuming let's say this is something
04:45that is not supported yet or we didn't have a shim for it
04:48and we didn't add that extra capability to make sure
04:51that this always works, I could just go ahead
04:54and ask literally if my point has an X point
04:58so I know it has a X and a Y.
05:01If it doesn't, I'll just go ahead
05:02and I'll update that point to use old school way
05:05of doing it, which would be to ask for the L, the pages
05:09X coordinate and subtract out of it, the target do offset
05:14left and I would do the exact same thing for the Y
05:17to get the basic, basically to get the same thing.
05:20So I'm gonna take my page Y,
05:22which will gimme the actual coordinate
05:23of our element within the page
05:25and I'm gonna subtract out of it the actual targets offset
05:29top, making sure that I'm actually getting the full point.
05:33So if I go ahead here and I save this
05:36and do here a little console log for our point,
05:41we should be getting our point every time we click onto it
05:45to run it.
05:46So if I go ahead here
05:48and I try to click somewhere, we're gonna see that first
05:51of all, I'm getting undefined, which is already a bad thing.
05:53So let's see what's going on here. So first of all, let me
05:56Go ahead and put here another console log just
06:01before to just get what is inside of my current currently
06:04and the point, even before we change it,
06:06is it going to be changed.
06:07So let's go ahead and take a look.
06:08So if I click here, I'm getting first of all undefined
06:11and then I'm getting undefined again.
06:13Okay, so it's telling me that the value that I've put here,
06:16and let me just comment this whole thing out.
06:18Let's debug this out completely.
06:21So for point X I'm not getting anything,
06:24so let's double check that I'm really not getting anything.
06:26And lemme also double check for our offset X to try
06:29to isolate and understand what am I doing wrong.
06:34So if I go ahead here and save this
06:35and try to run it one more time,
06:37let's see if we're just getting that X.
06:38So we're definitely not getting that X at all,
06:41but yet it's passing through our if condition.
06:45So let's double check. Maybe there isn't support in the
06:48browser for offset X in the current
06:50browser that I'm running.
06:51So let's go ahead and do a console
06:56log and see we are here.
06:59And again, old school debugging.
07:01I'm basically checking to see if I'm in that if statement
07:04and you can see that I'm not even getting into
07:06that if statement, which is basically telling me
07:08that this value that is coming through is not equal
07:12to undefined even though it's coming back
07:14as a value of undefined.
07:16So my if condition is not going through
07:21and when I'm looking at this, I'm realizing
07:23what my mistake was because the point X really doesn't exist
07:26because this property doesn't exist in my browser currently.
07:30So I need to say if it doesn't exist,
07:32because if a value is there, we don't need to run it again.
07:34And if the value is not there only then we're gonna go
07:37inside of here we are, here are we to more accurately.
07:42And now I could go ahead
07:43and do that console to the log to check it out.
07:46So I'm more or less being a little bit future ready
07:48for a point when offset X
07:50and offset Y will actually be available.
07:53And this is what you would do with kind of like properties
07:56that you know are not fully supported
07:58and you could create these first, setting them up
08:00or asking if they exist and then configuring them.
08:03And now if I go ahead and I check this again,
08:05I should get my X and Y coordinates
08:08whenever I click on anywhere on the screen.
08:12In this lecture we explored a lot of things,
08:14but in the end of it we got our points within the map,
08:18it's time for us to take those points
08:19and try to figure out out
08:21of them the latitude and longitude.
08:23And that's exactly what we're gonna do in the next lecture.

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!