OnMapClickListener for a website?

1

I have researched a little about this topic and I am wondering if there is a way to do that by doing click on a Google map on my website I can get the LatLng values (latitude and longitude) of the point where you clicked.

I know that this can be done on android using OnMapClickListener that returns me that data. But I can not find a way to do something similar but on my website.

These values I intend to use to create static maps, in which the route is drawn between two points, one already predetermined and another one obtained by means of a written address, something like "New york united states". But in the country where I live these addresses fail a lot so I need to sometimes manually place that data LatLng so to make the process easier for my client that just enough to do click in place and automatically fill in the latlng information

EDIT: The site is basically made in php, using laravel I have researched in the google maps API documentation but I only see that command and it is for Android

    
asked by Luis Romero 03.05.2017 в 03:14
source

1 answer

2

It is very possible that when looking for documentation you have not gone to look for the right place. From what I can glimpse you've been looking at the Android API instead of the Web API of JavaScript that I seem to understand what you need.

The map object has a click event that you can use and in the event information, we have the LatLng object of the position where we have marked. All you need to do is subscribe to this event:

google.maps.event.addListener(map, 'click', function(event) {
   alert(event.latLng);
});

In the documentation of class Map we see a list of events to which we can associate a listener among which is the event click :

Official documentation:

  

This event is fired when the user clicks on the map. An ApiMouseEvent   with properties for the clicked location is returned unless a place   icon was clicked, in which case an IconMouseEvent with a placeid is   returned. IconMouseEvent and ApiMouseEvent are identical, except that   IconMouseEvent has the placeid field. The event can always be treated   as an ApiMouseEvent when the place is not important. The click event   is not fired if a marker or infowindow was clicked.

Home translation:

  

This event is launched when the user clicks on the map. He   returns a ApiMouseEvent with the properties for the location   where you clicked unless you clicked on an icon   of "place", in that case, a IconMouseEvent is returned with a    placeid IconMouseEvent and ApiMouseEvent are identical, a   exception the IconMouseEvent contains the field placeid . The   event can always be treated as if it were a ApiMouseEvent in   case that placeid is not important. The click event does not   launch if you click on a marker or a infowindow .

    
answered by 04.05.2017 / 08:46
source