Retrieve details on Bus Line or Subway in Google Map API

若如初见. 提交于 2020-01-05 04:07:26

问题


I'm looking for doing something like this website : WEBSITE GMAPS

For the moment i can get everything around 500meters like if it's a bus stop or something else but i can't retrieve the Bus line.

var mapControl = $scope.map.control.getGMap();
        service = new google.maps.places.PlacesService(mapControl);
        var transitLayer = new google.maps.TransitLayer();
        transitLayer.setMap(mapControl);
        var directionsService = new google.maps.DirectionsService();

        service.search(request, callback);

        function callback(results, status) {
            if (status == google.maps.places.PlacesServiceStatus.OK) {
                $scope.placesTransit = [];
                $scope.timeTotal = [];
                angular.forEach(results, function(result, key) {
                    var timeTotal;
                    directionsService.route(
                    {
                        origin: new google.maps.LatLng($scope.map.center.latitude, $scope.map.center.longitude),
                        destination: result.geometry.location,
                        travelMode: google.maps.DirectionsTravelMode.WALKING,
                        unitSystem: google.maps.UnitSystem.METRIC
                    }, function (response, status)
                    {
                        if (status == google.maps.DirectionsStatus.OK)
                        {

                            result.timeTotal = response.routes[0].legs[0].duration.text;
                            if (result.types[0] === "subway_station") {
                                result.typeTransport = "Métro";
                            }
                            console.log(result);
                            $scope.placesTransit.push(result);
                        }
                    });
                });
            }
        }

If someone can helps me how to do that : Apparently i can retrieve this information on this page , but there's nothing to get this with Google MAP API v3 . Any Idea ? Thanks


回答1:


Yes, Google has a place where you can get the information about public transportation.

First, you need to check if your city is supported:

  • https://maps.google.com/landing/transit/cities/

Then, if it is supported you can get more information here:

  • https://maps.google.com/landing/transit/index.html

To access it programatically, you can both the following APIs:

  • https://developers.google.com/transit/
  • https://developers.google.com/maps/documentation/directions/

I would go with the second one.

With this in mind, if the company you are trying to check is registered, you will be able to easily access all of its bus routes (and more).

If the company is not listed however, it means that they coded the path on the map that you see, and you must do the same using overlays:

  • https://developers.google.com/maps/documentation/javascript/examples/overlay-simple

Hope it helps!



来源:https://stackoverflow.com/questions/37811930/retrieve-details-on-bus-line-or-subway-in-google-map-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!