How to Make a Map Using Google Maps JS API
Method 1 of 1:
Setting Up the Map
- Open or create your webpage. If you don't already have a webpage you want to insert the map into, you can use the following HTML5 template; save it as 'map_page.html' :
<html lang="en"> <head> <meta charset="utf-8"> <title>My Leaflet Maptitle> head> <body> body> html>
- Include the Google Maps JS file. To do this, paste the following line of code into your HTML file, inside the
area, on a new line, just before the closing
tag:
<script async defer src="https://maps.googleapis.com/maps/api/js?key=[YOUR_API_KEY]&callback=initMap"> script>
- Get an API key and insert it into the JS URL. To use Google Maps JS API - just like any other Google API - you'll need an API key, which is like a passcode. You then need to insert your new key into the URL from the step above where it says [YOUR_API_KEY] (remove the square brackets).To get your own key, see Get a Key/Authentication page.
- Add an HTML map container. Google Maps will display the map inside an HTML element, so you need to provide one for it. Paste the following line of markup inside the
:
<div id="map">div>
- Style the map container. You need to set how large the map will be when it is displayed, and you should use CSS to do this. You can add the following to the document
:
<style> #map { height: 500px; width: 700px; } style>
- Create the map object. To start writing the JavaScript code that sets up the map, you'll need to add a
area to the
after the map container
div
. Also inside of this, you can create the map object by calling theMap()
function of the google.maps object. You'll also need to pass the function the map container html element, like this:<script type='text/javascript'> var map; function initMap() { // Create the map object. map = new google.maps.Map(document.getElementById('map')); } </script>
- Set the map's zoom and center. The map's zoom and center properties determine the area that the map covers by default when the map is loaded. These are just the map's default values; the map's current zoom and center can be changed later if you enable those controls - we'll get to that later.
Note: You need to simply copy the following lines of code into yourelement (from the previous step), and make sure it is inside the initMap() function, before the closing '}':
map.setZoom(2); map.setCenter({lat: 0, lng: 0});
- Add a marker. Markers show the location of a point on a map. In Leaflet, markers are a type of 'overlay', so they can be directly added to maps. By default, a marker appears as a red pin, which is a standard image that you can change.
var marker = new google.maps.Marker(); var coordinates = {lat: 20, lng: 20}; marker.setPosition(coordinates); marker.setMap(map); marker.setTitle('Chad');
- Add polyline. Just like the marker, a polyline is a type of overlay, but it is used for a different purpose. A polyline is simply a line with multiple segments. You define it by providing the coordinates of the points that the line segments will be between. The coordinates needs to be an array of coordinate point objects. One of the great features of Google Maps' polylines is that they can be 'geodesic', which means they can curve to match the shape of the Earth! You can set that as an option later on.
var coordinates = [ {lat: 20, lng: 10}, {lat: 10, lng: 20}, {lat: 20, lng: 30} ]; var polyline = new google.maps.Polyline(); polyline.setPath(coordinates); // Using the array we defined above polyline.setMap(map);
- Set options. Each overlay in Google Maps has options that you can set, that determine what the overlay looks like. You could set the options of the marker (from the steps above) to make it look unique, but below are options to set for the polyline you just created:
polyline.setOptions({ strokeColor: '#FF0000', // Bright red strokeOpacity: 1.0, // Fully opaque (not translucent) strokeWeight: 2 // Thickness of 2 pixels });
- The finished map.
5 ★ | 1 Vote
You should read it
- How does Google Maps work?
- Tips for using Google Maps on Android
- How to preview images where needed on Google Maps
- How to find your way with Google Maps on your phone
- How to use Google Maps without wasting space
- Infinite zooming on Google Maps
- How to turn on Google Maps for CarPlay on iPhone
- Google My Maps updated: Development opportunities for local businesses
May be interested
- Google My Maps updated: Development opportunities for local businessesgoogle has announced upgrading of google my maps features, and later this year it will replace the classic google maps.
- Avoid tolls and save money with the Google Maps GPS featurethere must be road taxes, gasoline and insurance that will consume a lot of your monthly budget. therefore, google maps offers a feature that helps you cut a portion of that cost by guiding you to avoid toll roads with google maps gps.
- Useful features on the Google Maps app that you don't know yetgoogle maps is the most popular map application on smartphone devices. with the application, users can easily search for the location they need the fastest, the detailed route, or some of the features you don't know yet.
- How to change the route on Google Mapsgoogle maps is an important part of moving in modern life. here's how you can take care of routing and manually importing your own routes into google maps.
- How to use the new Google Maps shortcut for easy remote shoppinggoogle has just launched a series of new shortcuts on the maps application, making it easier for people to access essential locations such as fuel, medicine and especially food.
- Instructions for booking Grab car on Google Mapsinstead of booking grab on the grab app as usual, you can choose to make grab booking via the google maps map application. grab car booking function on google maps allows you to call a car simply and quickly right from within this app without having to access the original app.
- How to Get Photos from Google Mapsgoogle maps is an online map service developed by google. you can't directly upload user-uploaded photos to google maps, so you need to apply some technical tips. this article will help you download photos from google maps via the desktop version of chrome or by taking a screenshot on a mac or windows computer.
- Top 5 extensions to add more features to Google Mapsgoogle maps isn't just for directions — it's packed with cool features like fuel cost tracking, route optimization, and easy location sharing.
- How to create and share favorite places on Google Mapsin the new version of google maps application, users can create lists for favorite places then send to friends via messenger messages or applications, email, ... installed on the device .
- Apple added many new features to Apple Maps in iOS 13, resolving its market share from Google Mapsgoogle maps regularly tops the list of the most commonly used apps on the iphone for many years, although it was developed by google, and apple is certainly not happy about this situation.