PressTigers

How To Create Google Direction Map Shortcode In WordPress

This article would explain how to create a short-code to display Google Direction Map and to use shortcode anywhere in posts/pages or in text widget with zero effort. A WordPress-specific code that lets you do nifty things with very little effort is “shortcode“. Shortcodes can embed files or create objects that would normally require lots of complicated, ugly code in just one line. Shortcode = shortcut (source). Shortcodes can be used in page/post content or in text widget. When non-technical user is managing a site, then shortcodes are the ultimate help. Non technical user can add/remove page front-end look with the help of shortcodes.

Point to be noted here is that this article would not encompass an in-depth discussion on Google Direction Map code and how Google Direction Map API works. It will tell you about the use of Google Direction Map code that you can find easily on internet or from Google Map API. If you have any query regarding Google Direction Map API, don’t hesitate to contact us. For Google Direction Map API help, follow this link.

First we will look into the Google Direction Map code to display Google Direction Map on HTML pages. Below is its code for Google Map API JS file:

To display Map on HTML pages, its Map Script is as follows:

HTML for Google Map and inputs for Google Direction Map are as follows:

Now, review the code and find the line listed below in Google Script code:

var default_location = new google.maps.LatLng(‘.$lat.’, ‘.$long.’);

Here $lat> and $long> are the two variables that are used to display user current position on map ($lat for latitude and $long> for longitude).

The function calcRoute()> is used to calculate the route distance or direction from your address to new address given by the user.

This input is used to get the Ending Address (Destination Address) from user.

Conversion Of Code To WordPress ‘shortcode’:

add_shortcode is a WordPress function used to create a shortcode. The function gets two arguments, first argument is name of the shortcode that we will be using and second argument is the function that will be called when this shortcode is used. The name of the shortcode should be unique. In our case, our shortcode name will be pt_google_map>. The pt> (PressTigers) concatenates with shortcode name to verify that its name is unique.

The function pt_google_map_function() has one argument name $atts which contains the extra information given when the shortcode is being used. In our case, we will pass latitude and longitude. Our final code will be like this:

How To Use ‘shortcode’ In Content Area or Posts/Pages:

Calculate your address latitude and longitude from this http://www.mapcoordinates.net/en. You need to enter your address only and it will give you the latitude and longitude values that you can then use in your shortcode:

[pt_google_map lat=”40.7143528,” long=”-74.0059731”>]>

You can style the inputs and Google Map div with custom CSS as you want. Below is the output of this shortcode. In our case, we have added some CSS for its Styling. See the screenshot below for the output of this shortcode:

google-map-shortcode-output

User Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Get in Touch