Google Maps JavaScript API中MART公交系统未在公交图层显示的问题求助
It’s super frustrating when transit data shows up perfectly on the Google Maps web app but refuses to load in your custom JavaScript API implementation—let’s break down the most likely fixes for getting MART transit visible on your map.
1. Confirm MART’s Agency Compatibility with the Default Transit Layer
The default TransitLayer in the JavaScript API doesn’t always include every small regional transit agency that the web version does. First, you’ll want to confirm MART’s official agency ID (used in Google’s transit dataset):
- You can retrieve this using the Places API: search for "MART transit" and check the
agency_idin the response. - Alternatively, look up MART’s public GTFS feed (most transit agencies publish this on their website) and note the
agency_idfield in theagency.txtfile.
Unfortunately, the standard TransitLayer doesn’t support filtering by agency, so if MART isn’t included in the default layer, you’ll need a custom approach.
2. Load MART’s GTFS Data as a Custom Layer
Since MART likely publishes its own GTFS data (check their official site for a download link), you can parse this data and display it on your map using the Google Maps Data layer. Here’s a simplified example:
function initMap() { // Center map on MART's primary service area (Lowell, MA) const map = new google.maps.Map(document.getElementById('map'), { center: { lat: 42.6075, lng: -71.3825 }, zoom: 12 }); // Load converted GTFS routes (convert GTFS shapes to GeoJSON first) const transitDataLayer = new google.maps.Data(); transitDataLayer.loadGeoJson('path-to-your-mart-routes.geojson'); // Style the routes to match Google's transit look transitDataLayer.setStyle({ strokeColor: '#0066cc', strokeWeight: 3, strokeOpacity: 0.7 }); transitDataLayer.setMap(map); }
Note: GTFS data isn’t GeoJSON by default. You can use open-source scripts (Python or Node.js tools work well) to convert the shapes.txt file from MART’s GTFS feed into GeoJSON for easy loading into the map.
3. Double-Check API Key Setup
Even if other cities work, verify your API key has:
- The Maps JavaScript API enabled in the Google Cloud Console.
- Active billing (free tier limitations rarely block regional transit data, but it’s worth confirming).
- No regional restrictions that might block access to MART’s service area.
4. Ensure Correct Map Viewport and Zoom
MART operates in a relatively small area. Make sure your map is centered on its service region (like Lowell, MA) and zoomed to at least level 12—smaller transit routes often don’t appear at lower zoom levels in the default layer.
5. Report the Issue to Google
If none of the above fixes work, it’s possible MART’s transit data isn’t properly integrated into the JavaScript API’s transit layer. File a bug report through the Google Maps Platform issue tracker, including details that the data works on the web app but not in your API implementation.
内容的提问来源于stack exchange,提问作者Dale Watt

