A
A
Andrzej Wielski2015-05-01 06:14:03
Google
Andrzej Wielski, 2015-05-01 06:14:03

How to track click coordinates using standard Google Maps v3 markers?

Good day.
The problem is the following - when using the "click" event on the map embedded on the site (Google Maps API v3), nothing happens when you try to click on the standard marker of a metro station, bus stop, etc.
That is, the event is not executed, and, accordingly, it is impossible to track the coordinates of the click.
How to solve this problem other than Places API?
Now, when drag'n'drop maps, I execute a request, getting all the nearest stops, and on top of them I create an invisible marker to track the click. But it turns out too much code, and, accordingly, requests to Google servers.
Is there any way to make it more elegant?
Thanks in advance for your help.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrzej Wielski, 2015-05-06
@wielski

I found an elegant solution to the problem myself:
Create an overlay layer on top of the map (we declare the variable in global visibility)

overlay = new google.maps.OverlayView();
  overlay.draw = function() {};
  overlay.setMap(map);

We hang a click trigger on the map object:
$('#map-canvas').click(function(event){
    var point = new google.maps.Point(event.pageX,event.pageY);
    var location = overlay.getProjection().fromContainerPixelToLatLng(point); //получаем координаты по значениям X,Y клика

    var request = {
      location: location,
      types: ['bus_station','subway_station'], //нам нужны только автобусные остановки, и метро
      radius: 10,
    };
    placesService = new google.maps.places.PlacesService(map);
    placesService.search(request, function(result, status, pagination){ //ищем по близости объект по координатам, в 10 метрах от клика
      station = result[0];
      if(typeof station != 'undefined'){
        pos = station.geometry['location'];
        bus_no = station.name.match(/\[([0-9]+)\]/i)[1]; //получаем ID автобусной остановки, он обычно среди []
        alert(bus_no); // А вот и наша остановка
      }
    });
  });

Good luck, I hope it helps someone!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question