Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Mapbox Cookbook

You're reading from   Mapbox Cookbook Over 35 recipes to design and implement uniquely styled maps using the Mapbox platform

Arrow left icon
Product type Paperback
Published in Mar 2016
Publisher
ISBN-13 9781784397357
Length 264 pages
Edition 1st Edition
Tools
Arrow right icon
Toc

Table of Contents (8) Chapters Close

Preface 1. Introduction to Mapbox FREE CHAPTER 2. Mapbox Services 3. TileMill and Mapbox Studio 4. Mapbox.js 5. Mapbox.js Advanced 6. Mapbox GL Index

Adding markers and popups


In this recipe, you will learn how to add markers with popups dynamically. We will enhance the previous recipe, which displays a map, and we will add a button that will open a popup in which we can type coordinates.

When we click on the OK button, a new marker will be created, and the map will zoom and pan to the location of this marker; if there are multiple markers, it will zoom to fit them all.

How to do it…

Perform the following steps:

  1. Create a map and add a base layer.

  2. Then add new markers to the map using L.marker([latitude, longitude]).addTo(map);.

  3. Use bindPopup('enter html content here') to bind a popup to the marker.

Adding a basic marker with a popup

Here are the steps to be performed:

  1. Open the chapter-4-example4-starter project folder in your favorite text editor. The project contains the map we used in other recipes with a New Marker button. When this button is clicked on, it will trigger a new method that will open a prompt to type the coordinates. Let's create...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image