JS-151-API
  • JavaScript Library
  • 02-DOM-Manipulation
    • Overview
    • DOM Explained
    • Example Setup
    • Selecting Elements
    • Events
    • Complete Example
  • 03-API Fundamentals
    • 0-Getting Started
      • Welcome
    • 1-Intro-To-APIs
      • Intro
      • Client
      • Requests
      • JSON
      • API Endpoints
      • Server
      • Response
      • Statelessness
      • REST
    • 2-Asynchronous-Programming
      • Intro
      • Callbacks
      • Promises
      • Promises Continued
      • Chaining Promises
    • 3-Fetch
      • Fetch Intro
      • Star Wars
        • Star Wars API
        • Star Wars Setup
        • Star Wars JS
      • Random Photo
        • Unsplash
        • Unsplash Setup
        • Unsplash JS
    • 4-Apps
      • 01-New York Times
        • 00-App Intro
        • 01-HTML/CSS/API Key
        • 02-Variables
        • 03-Event Listeners
        • 04-fetchResults
          • 01-fetchResults()
          • 02-preventDefault()
          • 03-fetch() method
          • 04-Dates
        • 05-displayResults
          • 01-Logging JSON
          • 02-Working with JSON
          • 03-Link Display
          • 04-Results Navigation
          • 05-Results Update
          • 06-Keywords
          • 07-Images
        • 06-Pagination
          • 01-Pagination Intro
          • 02-Pagination Functions
        • 07-Next Steps
      • 02-YouTube
        • html
        • youtube.css
        • youtube.js
      • 03-GoogleMaps
        • Setup
        • HTML and CSS files
        • API Key
        • JS Setup
        • Adding Your Location
        • Listeners
        • Custom Options
Powered by GitBook
On this page
  • Marker
  • Custom Marker
  1. 03-API Fundamentals
  2. 4-Apps
  3. 03-GoogleMaps

Adding Your Location

PreviousJS SetupNextListeners

Last updated 7 years ago

At the moment, we have a blank map of our current location. Let's add a marker to display where we are.

Marker

In the googlemaps.js file, add the following code inside the if statement underneath the map variable:

var marker = new google.maps.Marker({
    position: latlng,
    map: map
});

This marker object takes the latlng and map variables that were previously defined and adds them as properties. This will tell the program what map the marker needs to be placed on, as well as the coordinates for it to be placed. Check out more information on marker in .

Save your changes and refresh your browser, and the marker should appear. This the standard marker for GoogleMaps as you're used to seeing it! If we want to, we can customize our marker.

Custom Marker

Let's use a fun custom marker! Google image search anything you'd like to use as a marker. Smaller images will work better! In googlemaps.js, add the following line of code right above the marker object declaration. You can set iconBase equal to the image address of an image from Google. Make sure you do Copy Image Address when you right click on a google image to grab it. Replace your-img-url-here with the address of the image you want.

//Step 2. Add Custom Icon
var iconBase = 'your-img-url-here';

Also change the var marker code to

var marker = new google.maps.Marker({
      position: latlng,
      icon: iconBase,
      map: map
    });

You should now see your custom marker on the map at your current location!

If you've successfully changed the marker icon, commit your code and move on to the next module.

Google's API Docs