mapbox / mapbox-gl-geocoder

Geocoder control for mapbox-gl-js using Mapbox Geocoding API
https://mapbox.com/mapbox-gl-js/example/mapbox-gl-geocoder/
ISC License
362 stars 181 forks source link

422 Error When Using External Geocoder and Mapbox GL JS #496

Open mcollins-GPI opened 1 year ago

mcollins-GPI commented 1 year ago
  1. I am using an external geocoder with the Mapbox geocoder widget.
  2. The results return and are concatenated to the standard geocoder results correctly.
  3. When clicking on a result in the geocoder tool menu, it navigates correctly to the point identified in the list.
  4. It also throws a 422 error, and I am not sure why.

This is the code where I implement it:

externalGeocoder: async (queryString) => {
    const filters = {
        geom_column: 'geom',
        filter: `cast(busstopid as text) like '${queryString}%' or address like upper('${queryString}%')`,
        precision: 9,
        columns: 'address as place_name, address as text, busstopid',
        id_column: 'internal_id',
        limit: 5,
    };
    // get data from both the GTFS feed and MIIPS if possible
    const generalData = await utilities.getData(`${URL.base}/v1/geojson/gtfs_data?`, filters);
    const miipsData = await utilities.getData(
        `${URL.base}/mama/miips/read_serial_number_geocoder/${queryString}`
    );

    if (miipsData.features) {
        return generalData.features.concat(miipsData.features);
    } else {
        return generalData.features;
    }
}

This is the object the external geocoder is creating:

{
    "type": "FeatureCollection",
    "features": [
        {
            "id": 13419,
            "type": "Feature",
            "center": [
                -73.94012,
                40.765427
            ],
            "geometry": {
                "type": "Point",
                "coordinates": [
                    -73.94012,
                    40.765427
                ]
            },
            "place_name": "34 AV/VERNON BL",
            "properties": {
                "text": "34 AV/VERNON BL",
                "busstopid": 552207,
                "place_name": "34 AV/VERNON BL"
            }
        },
        {
            "id": 14304,
            "type": "Feature",
            "center": [
                -74.107189,
                40.671472
            ],
            "geometry": {
                "type": "Point",
                "coordinates": [
                    -74.107189,
                    40.671472
                ]
            },
            "place_name": "34 ST/HOBOKEN LIGHT RAIL STATION",
            "properties": {
                "text": "34 ST/HOBOKEN LIGHT RAIL STATION",
                "busstopid": 805054,
                "place_name": "34 ST/HOBOKEN LIGHT RAIL STATION"
            }
        },
        {
            "id": 14305,
            "type": "Feature",
            "center": [
                -74.10723,
                40.671416
            ],
            "geometry": {
                "type": "Point",
                "coordinates": [
                    -74.10723,
                    40.671416
                ]
            },
            "place_name": "34 ST/HOBOKEN LIGHT RAIL STATION",
            "properties": {
                "text": "34 ST/HOBOKEN LIGHT RAIL STATION",
                "busstopid": 805056,
                "place_name": "34 ST/HOBOKEN LIGHT RAIL STATION"
            }
        }
    ]
}

I looked into the repo and tried to discern from the error and payload what I was doing wrong. The results seem to work correctly, but it also seems like it is trying to use the external result with the standard geocoder somehow, which seems wrong. I am not sure how to drill down into this further to better diagnose the behavior, but any help would be most appreciated.