Posted inTech Study Notes

How to Create and Manage Google Maps Events with JavaScript

= Google Maps is a web platform that  makes it possible to explore maps , locate points of interest and find directions to different parts of the world.

Its biggest advantage is that it allows three map view options: normal, satellite and terrain. Later in this post we will show you how to handle events in Google Maps, which is another little-known feature of this app.

JavaScript is a programming language, whose latest available version is known as: ECMAScript. Resulting in a standard dialect for developers or programmers. Said in programming language, it has a fairly extensive event library.

How to Manage Google Maps Events with JavaScript

If we take this language and apply the characteristics that it has. In addition to including it in a practical way in the program or application known worldwide as Google Maps .

It’s simple to work with Google Maps; It fulfills its function perfectly and does not confuse with unnecessary decorations, also from a PC, in which we can check if someone is using it remotely

Google Maps may be available for mobile devices through its application . It comes standard on Android smartphones and tablets, so you can use it as a portable GPS.

You can do a lot of different activities that are very useful for your users.

In this way and continuing with JavaScript, if you want to embark on the world of programming or code writing. Specifically those that are used in JavaScript and its application on Google Maps commands. So, this guide will teach you the basic principles you need.

Index( )

  1. Events that we can use in Google Maps
  2. Key features of Google Maps 
  3. Managing events on Google Maps, from theory to practice

Events that we can use in Google Maps

To begin, in the first instance, we have to know the events that we have at our disposal. The API we are going to work with uses the name: google.maps.event to allow us to use these events. In the same way, this name gives us static methods that will be in charge of “ Listen ”.

Yes, these methods will listen when a specific event occurs through a controller called: addListener(). As its name indicates: add = Add and Listener = Listener. So, with this in mind, let’s move on to define some of the more important events.

  • In the event that a change is made in the center of the map, the event that must be listened for is: center_changed. Which its name translates to: Center = Center and Changed = Change / Changed.
  • The event known as: click will be firedwhen the user clicks on the interface. It does not matter if it is on the map, on an information window or on a marker.
  • At the moment the user moves the map or drags itin any direction, our Listener will activate the event:
  • mousemove is the name given to the event that will be fired when the user moves their mouse. This will be done regardless of which direction the mouse is facing in the application.
  • Every time the user displays a context menu of the Document Object Model (DOM). The event known as: rigthclickwill be “listened to” . Allegory to the action of pressing the “right button” of a computer mouse.
  • zoom_changed or its translation into Spanish: Change of approach, it will be an activated element when the established number of the “Zoom” changes. Specifying that this zoom modification can only be used on the map.

All of the aforementioned are the most common and important events for the development of activities in Google Maps.

Even so, there are other events that can be added to the code that are much more specific. And of course, its writing or syntax requires a little more advanced knowledge.

Managing the tools expressed in the previous list, adding a constant practice in the development of this activity.

In a very short time you will discover the great amount of things that you can get to do with these lines of code. This is due to the valuable potential that Google Maps offers us, by extending its functionalities.

Key features of Google Maps 

Before learning how to handle events with Google Maps , we must take into account the functions of this app

  • Searching for addresses or places of interest.
  • Visualize the places of your interest as they really are through Street View.
  • Print maps, by clicking on print located in the toolbar of your browser.
  • Share the URL of a map with the people you want.
  • Activate the location of a GPS,from a mobile phone, entering the address where you are and the address you want to get to.
  • Select nearby places, write what you want to find, to locate a place to visit.
  • Any event can be published on Google Maps: games, markets, open parties, etc.

This last function of Google Maps is the least known and the one that should be detailed here. This function is achieved thanks to its respective API, which is what gives us the possibility of handling events with Google Maps. The API is based on Javascript, which is also an event-driven programming language.

It should be noted that the API incorporates some events that are not part of Javascript , it is also unique and works in any browser.

API, in Spanish, means Application Programming Interfaces, being a formal specification about how a software module manages to communicate and interact with another.

Managing events on Google Maps, from theory to practice

Creating passwords is an easy task for a WEB Developer , but the treatment of events is very simple , to extract the point where you have clicked on the map.

First you will need to create the map and add an Event listener to GEvent for when the map is clicked.

var map = new GMap2(document.getElementById(“map”));

map.setCenter(new GLatLng(33,0),3);

map.addControl(new GLargeMapControl());

map.setMapType(G_NORMAL_MAP);

 

GEvent.addListener(map, “click”, click_treatment);

 

When adding an event to be listened to, you must provide three parameters :

  • The API object on which to listen for the event
  • The name of the event that you want to detect and take action on when caught.
  • The name of the function that will handle the event.

Now you need to code the behavior of the map for when it is clicked i.e. code the function

treatment_click.

function click_treatment(overlay,point){

alert(“Hello friend! I see you’re there because you clicked!”);

alert (“The point where you clicked is: ” + point.toString());

}

The function receives a pair of parameters, which are the context in which the event was detected .

Here is another development example where you can  make a map that has a mark . By clicking anywhere on the map you will be able to reposition the mark to the position where you clicked.

The event that you will be able to see is the click on the map object, of the GMap2 class. In this case, the clicked point will be retrieved to place the mark at that point.

First you will see the code to create the map with a mark :

var map = new GMap2(document.getElementById(“map”));

map.setCenter(new GLatLng(40,-4),3);

map.addControl(new GLargeMapControl());

map.setMapType(G_NORMAL_MAP);

 

var point = new GPoint(-4.40);

var marker = new GMarker(point);

map.addOverlay(marker);

See the simple code to detect a click event on the map:

GEvent.addListener(map, “click”, function (overlay,point){

if(point){

marker. setPoint(point);

}

});

The event listener is created, for the map object and the click event type, by executing a function that receives two parameters (overlay,point). Remember that overlay is the clicked mark, and point is the clicked point on the map.

Now, Google Maps, has incorporated in its latest version an innovative function, which makes it easier for you to create public events .

Officially it has not been announced, it is in the experimentation stage, but it already has a page  in the help section of the application.

It is only available on Android devices , however it is also conditioned by your location and range.

Whose function can be located in the “Contribution” and “Your contributions” tabs within the application. Once you click on the “Create Public Event” option , the app will allow you to add the details.

You can place the name of the event, place, time, category, description, you can even add a link to a website or photos related to the event. As this feature is under development, it takes more than an hour to update.

 

I’m a teacher, researcher and writer. I write about study subjects to improve the learning of college and university students. I write top Quality study notes Mostly, Tech, Games, Education, And Solutions/Tips and Tricks. I am a person who helps students to acquire knowledge, competence or virtue.

Leave a Reply

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