Enable bus stop icons clickable in Google Maps

只愿长相守 提交于 2019-12-04 07:47:05

For now you can't get it to work. It's an 'acknowledged' bug in gmap's api: https://code.google.com/p/gmaps-api-issues/issues/detail?id=145

You'll notice that in their official transit-layer code demo on the api site, there is also no interactivity: https://developers.google.com/maps/documentation/javascript/examples/layer-transit.

You can get bus station name, ID and coordinates, and then get information about bus stop with any other API. Here is code:

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

$('#map-canvas').click(function(event){
    var point = new google.maps.Point(event.pageX,event.pageY);
    var location = overlay.getProjection().fromContainerPixelToLatLng(point); //get map coordinates by click

    var request = {
      location: location,
      types: ['bus_station','subway_station'], //get bus stops and metro stations
      radius: 10,
    };
    placesService = new google.maps.places.PlacesService(map);
    placesService.search(request, function(result, status, pagination){
      station = result[0];
      if(typeof station != 'undefined'){
        pos = station.geometry['location']; //position
        bus_no = station.name.match(/\[([0-9]+)\]/i)[1]; //get ID by name
        alert(bus_no); // ID
      }
    });
  });
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!