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

Creating a Google Map

Its time for us to actually create a map. So far we covered all the details and steps we needed to take before starting to program. In this title we will build the map from start to scratch. We go step by step explaining the step involved with creating a google map. Starting with talking about Latitude and longitude and how we can figure out the values that would go into it. We discover some Google lab options for google maps that will enable us to pin point a location and get the lat/lng values for it. Once we know where we want our map to be centered it will be time for us to create a new google.maps.LatLng object. We continue going through all the steps to create the map. In this session we talk about what we are creating and hint into what is happening behind the hood (we will learn how to create this things later in these titles beyond using them).

Video Transcript

00:00Alrighty, so let's start building our map, and by the way, one of the stuff that we're
00:03going to be doing, and we're inside of our star.js file, one of the stuff that we're
00:08going to be doing besides building a Google map is kind of revisit things that you've
00:12probably learned while you were learning the basic constructions of JavaScript.
00:17Now, again, if you haven't, please watch earlier courses in our catalog, such as the
00:22JavaScript five-hour session that kind of teaches you everything you need to know
00:26about JavaScript to get started to be able to work in JavaScript. Now, in this
00:32title, and in this video, what I want to do is I want to kind of revisit some of
00:35those topics really quickly in the process of actually creating our Google
00:40map. Now, I'm going to start off with a very basic variable, which I'm going to
00:44call its center. Now, the center variable is going to be defined based on an
00:49object or a class that the Google API provides. That class is called google.maps.com.
00:56Which enables us to set a latitude longitude and basically tell the
01:01Google map at what location it's going to be. Now, as any other object you've
01:06created in the past, if it was an array or an object or any other type of object,
01:10you always start creating it with the words new space and then whatever you're
01:14going to be creating. In our case, we're going to be creating a google.maps.lat.lng
01:23uppercase L, uppercase L. Well, twice. Both L's are uppercase. Now, the next step is
01:30we're actually going to put inside of a number and literally the
01:34number is an actual latitude longitude. Ooh, that kind of went
01:40crazy. That was weird. Anyway, so I'm just going to paste the value inside.
01:45It doesn't really matter what was that craziness that just happened. I want to
01:49show you also how could you create your own latitude longitude before I
01:53explain what we actually did. So if you go into your browser into Safari or any
01:58other browser and this time around let's open up the maps.google.com. What you
02:05could do very very easily is you could actually right-click or if you go
02:10you'll see app labs on the bottom of your experience. In the map lab
02:15itself you could scroll around and search for something that will give
02:21you the latitude longitude tooltip and you can see you have one here that
02:26displays the tooltip next to the mouse cursor showing or adds an option in
02:30the context menu that lets you drop in a mini marker showing you the latitude
02:34longitude. This one looks good to me and I'm going to save these changes. Now,
02:37if I wanted to center my map let's say at this point I could now right-click
02:42then select drop a lat long marker we could close that baby up and we'll have
02:46the actual value that we could then copy paste and put it inside of our
02:50application. In our case though I had a pre-configured longitude which
02:55is going to be more or less roughly in the center of Europe so we could see
02:58Europe in our map. Now one of the stuff that we've seen in this experience in
03:04this in this part so far is we've actually created a new and a very
03:09mystical magical thing we were creating a new something but we don't really know
03:13what it is this is something that's defined somewhere else we're not really
03:16building and constructing it here this is actually happening somewhere else which
03:20is actually happening is is we're actually calling a constructor that is
03:24called google.maps.latlong. Once that constructor creates that object we're
03:28then placing that new object which is going to be a lat long object inside of
03:33our center variable. Now, so far you don't really know how to create your
03:37own object of this type but that's exactly the point of this course and
03:40we're going to be learning how to create those later on in our development. Now
03:45the second thing that we're going to do is we're going to create the standard
03:49object type that we've created in the past before so I'm going to create a
03:53new object and what I'm saying before I mean in our first four hours that teach
03:57you the basics of JavaScript. Now again I want to create a new object and this
04:01object this time around is actually going to be the Google map that we want
04:04to use. To do that I'm going to type a new and I'm going to set that new
04:08object so a new space and it's going to be a new google.maps.map
04:12surprisingly. Now the map the Google Maps needs to take in two parameters the
04:18first parameter is going to be the div itself that we want to be placed that we
04:21want to place. Our div is going to be that map so we're going to fetch that
04:25using one of HTML5's new and there's a couple of bugs and Dreamweaver 6
04:31maybe I don't have the latest version because I'm one of the alpha testers so
04:37I might have just had gotten one a funky variation of the software so I
04:44apologize for that let me try to save maybe saving will fix that no oh very
04:51very weird stuff all right so our first parameter is going to be the the new
04:56Google maps.map we're going to put in using the new HTML5 method which is
05:02called get element by ID so we don't need to use any open source variations or
05:09dig too deep to be able to get our get our elements with that said if you're
05:12trying to target older browsers you're gonna have to do some customization
05:16we're not going to be worrying about older browsers so we're going to be
05:19typing document dot get element by ID and then we're just going to send in our ID
05:26which is going to be map beautiful so our first parameter is all set configured
05:31and done our next parameter is going to be a literal object and the way we create
05:35a literal object as again we've worked on in our earlier title of JavaScript is by
05:40opening and closing a curly bracket and with that we created a new object so so
05:46again we see here in just these two lines of code that we've done so far is
05:50that we have in our first line we're creating an object based on a
05:53constructor which we don't really know what that is we haven't seen it in
05:56action yet but we know how to create with it we've created arrays with it
05:59and objects with it and other types of elements on the other line we see that
06:04we're creating a new object in the exact same type with a constructor which is the
06:08map object but with it we're sending two things we're sending an object using a
06:13property a built-in property and we're sending in the map div layer through the
06:18JavaScript API the get elements by ID which is of the document and the next
06:24thing is we're sending in a literal object and that literal object is an
06:27object we're creating from scratch on our own and with this literal object we
06:31could actually define a few things to help our map get a little bit of
06:35personality so let's start giving our map personality so I'm just gonna go right
06:39into that object and I'm gonna create a new property and by the way these
06:43properties are also in the Google API documentation there are a few properties
06:47that you could actually add to customize the map that you're creating
06:51we're just gonna look at a couple of them so we're gonna send in our center
06:54point which is gonna make sure that our map is gonna be centered in a
06:57specific spot we're gonna define our zoom level in our case we want it to be
07:01six but you could play with the zoom levels and see what zoom level you're
07:04happy with we're also going to define define a map type ID and that is gonna
07:10help us make sure that we're gonna set up a map that's gonna be literally the
07:15type that we want it to be and there are a few types we want it to be a
07:18road map so I'm gonna set here using a constant Google Maps Google maps get
07:25map type ID dot road map and we already set our first three parameters in our
07:34Google API and our last one there I'm gonna set up is gonna be disable default
07:39UI and I'm gonna set that to be true all right so I could go on and on and we
07:46could work on a lot of other the custom built-in Google map features but you
07:51could just look at their documentation and figure out which one you want I just
07:55put here the most common ones which is I want to center my map to a specific
07:59point I want to zoom in to a specific level I want to choose a specific type
08:03map that I want I could have chose road map I could have chose satellite and
08:06there's a few other options as well and you could look them up in the Google
08:10API documentation which will leave a link up to them in the course page in
08:15case you need or want to refer deep more deeply into that topic and last
08:19but not least we set up our default API to be disabled by default and that's all
08:25we had to do to actually get our map to work and if I jump right into our Safari
08:30and I go into our file itself let me close out because we don't need that
08:35anymore if I just open up our file and just refresh it I will find that it's
08:40not working oh and by the way I'm just clicking on this tab to kind of put my
08:44logger a bit out of the way so I could actually see it maybe a little bit
08:48more in large and move it around when I need to around to it one of the things
08:53obviously that I'm seeing here I've did something wrong and unfortunately with
08:56JavaScript the error messages that we're getting are basically nothing so
09:00the first thing that you want to do is you want to really take your code and
09:03look at it step by step and try to isolate and locate where do you have
09:07your problem now the best thing you could do initially before you even go
09:10too deep is to check to see that your center object is returning the way it
09:14should come true and that your map object is coming back as well so to test
09:18that out I'm just gonna create here a new console dot log and I'm gonna log
09:22out the center point and I'm gonna log out that map those two variables that
09:26we've created I'm gonna save that go right back into my browser click on
09:30refresh and see if I'm getting in my counsel those two objects and I am I'm
09:35getting two objects which are exactly what I was hoping to get that means
09:39it's not a problem in the creation of the objects so going back right into our
09:44code will continue on so we know it's not a problem with our center because
09:48we're getting an object back and we know that these are completely valid
09:51very parameters so we're good there we know that we're calling the document
09:56element by ID calling map we probably want to check maybe in the HTML make
10:00sure it's called map and it definitely is called map so far so good we don't
10:04see any issues until line 4 line 5 we're adding a new center variable that looks
10:10good no spelling errors zoom level looks good no spelling errors and here I see
10:15ready the problem line 7 you can see map type ID where I'm setting it ID with
10:20all upper case JavaScript is a case sensitive language and actually the
10:24parameter in the documentation of Google is an ID with a lower case now you might
10:29be thinking you know oh my god that's a really hard thing to discover and
10:33you're right it's a really hard thing to discover and lucky for us we're
10:38building it layer by layer so all we have to do is test out only 12 lines of code
10:43now that we know these 12 lines of code should be okay and we shouldn't have
10:47issues with them we won't have to worry about them anymore and if we encounter an
10:50issue we always want to isolate it to the minimum amount of lines we could so
10:55in this scenario just by by tracing out the center and the map we kind of
11:00made sure that center it cannot be center now with the map itself it could
11:04be that the map loaded but it didn't do what we wanted to so then we went in
11:07deeper into the map itself in the case of our center point we didn't have to
11:10worry about that so I'm gonna save my file and go back to the browser and
11:14hopefully this time around when I click on refresh we'll see that we're
11:17centered in the middle of Europe and we got our map to load fantastic that is
11:23really really great news all right

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!