You can combine Google Map API and HTML 5 to calculate the distance between two points. Do you know how to use the Google Map API to retrieve a current usage location? If so, can you also draw a direction from a current user's location to another location? In case you would like to know how to achieve this, this small tutorial tends to help you achieve it. After reading this article, you will be able to create and obtain a Google API key. Also, you will understand how to use it in your project to bring latitude and longitude. I will provide you with the JavaScript code to acquire the user's current position and draw the direction.
To acquire the Google Map API key, you need a Gmail or G-Suite account. If you do not have an account, you can create one; it's free. Once you've created your Gmail or G-Suite account, follow these steps to get your API key:
Note: The Google API is not a free service, but in my experience, unless you make too many API requests or calls, you may never have to pay for it. the service. You can read Google's terms and conditions for more information.
If you are successful in activating your account, you will then be granted access to the Google Cloud Platform Dashboard and then you will do the following:
There are more than 18 APIs, but we only need the direction API, because we will use HTML 5 features to detect the current location.
If your API is for mobile apps, there is an option to restrict Android or iOS apps. In the API Restriction option, you can either leave it unrestricted or restrict the key. The "Do not restrict" option will allow you, in some cases, to use the same API on different types of applications. The "Restriction" option only allows you to choose APIs for the type of application you are building. For example, in our case, we need the API keys for Google Map geocoding and direction. Therefore, in the "restrict key" option, we will select.
Replace your API key in line of code 2. Line 27: Use a form to add destination location and usage (See: value="120 Rue Louise, Longueuil, QC J4J 2S9"). You can change the address as you wish. Lines 35 to 41: We get the current usage information — Latitude and longitude. We use the form id on line 27 to acquire the form value on line 73.
Thank you for reading, you may also be interested in the article “Edit Add to Cart Button with Custom WooCommerce Single Product Page Link”.