2

I'm building a web app that calls the mapquest Geocoding API to grab the users lat-long position, and then make a second call to another API to return another set of data. The main issue I am having is when I call the mapquest Geocode API the user can enter any string of characters or made up locations and it returns JSON with data. I need to find a way to prevent this by ensuring the user enters an actual location. Here is the mapquest Geocode API https://developer.mapquest.com/documentation/geocoding-api/address/get/

and I believe my answer lies in here but I am not sure how I would use these to validate the location https://developer.mapquest.com/documentation/geocoding-api/quality-codes/

I've tried setting up a few if statements looking for an X in any of the values for responseJson.results[0].locations[0].geocodeQualityCode but that still kicked back valid results

this is an example of an acceptable entry that the API returns

{
    "info": {
        "statuscode": 0,
        "copyright": {
            "text": "© 2019 MapQuest, Inc.",
            "imageUrl": "http://api.mqcdn.com/res/mqlogo.gif",
            "imageAltText": "© 2019 MapQuest, Inc."
        },
        "messages": []
    },
    "options": {
        "maxResults": -1,
        "thumbMaps": true,
        "ignoreLatLngInput": false
    },
    "results": [
        {
            "providedLocation": {
                "location": "Lawton, ok"
            },
            "locations": [
                {
                    "street": "",
                    "adminArea6": "",
                    "adminArea6Type": "Neighborhood",
                    "adminArea5": "Lawton",
                    "adminArea5Type": "City",
                    "adminArea4": "Comanche County",
                    "adminArea4Type": "County",
                    "adminArea3": "OK",
                    "adminArea3Type": "State",
                    "adminArea1": "US",
                    "adminArea1Type": "Country",
                    "postalCode": "",
                    "geocodeQualityCode": "A5XAX",
                    "geocodeQuality": "CITY",
                    "dragPoint": false,
                    "sideOfStreet": "N",
                    "linkId": "282026294",
                    "unknownInput": "",
                    "type": "s",
                    "latLng": {
                        "lat": 34.606378,
                        "lng": -98.396817
                    },
                    "displayLatLng": {
                        "lat": 34.606378,
                        "lng": -98.396817
                    },
                    "mapUrl": "http://www.mapquestapi.com/staticmap/v5/map?key=zkn6RGyTDlLGsN8i8RfEmURf2GozTAkL&type=map&size=225,160&locations=34.606378,-98.396817|marker-sm-50318A-1&scalebar=true&zoom=12&rand=197404762"
                }
            ]
        }
    ]
}
A. Meshu
  • 4,053
  • 2
  • 20
  • 34

1 Answers1

0

Given the Lawton, ok input, the geocoder returns an A5XAX which means that it found a city (A5) with no address match (the first X) an exact city/state match (A) and not postal code match (the second X).

You can plug the returned quality code into the documentation page for a full explanation with some more detail.

The geocode api is not intended to prevent users from entering any string of characters or made up locations by ensuring the user enters an actual location. If the input isn't random keystrokes, there is probably a city or street name that will be a close match and return something other than an X. So the application will need to decide what position it will accept a no-match in and how many no-matches it will accept. For instance, if you need an exact address match for a delivery, you'll want no X and probably all A or B confidences. If a city is ok, then XAX may be fine. As always, it depends.

MQBrian
  • 452
  • 1
  • 3
  • 7