Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
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

Getting mouse coordinates


One of the most important aspects of using maps is user interaction. Interacting with the map can be done in various ways, which also depends on the device used; for example, you can hover over a location using the mouse, but you can't do this using a touch-based device, such as a smartphone. In smartphones, you can use multitap or perform gestures, and these user interactions cannot be performed using the mouse.

In this recipe, you will learn how to use event listeners to trigger actions in order to implement user interaction in your map.

How to do it…

The condensed steps to be performed are as follows:

  1. Create a map with baseLayer.

  2. Then create an event listener using map.on('mousemove', mouseMovedCallback).

  3. In the callback method, get the latitude and longitude.

  4. Now, construct an HTML file that shows the latitude and longitude and inject the HTML file into a div to display the mouse coordinates.

Let's perform these steps now:

  1. Start by opening the starter project directory...

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