This example demonstrates how to look up a latitude and longitude to a geographical location.
Step 1: Create a Resource Key
First, obtain a Resource Key by following the Configurator explanation. For this example, ensure that the Resource Key is authorized for at least the Country field provided by 51Degrees.
Step 2: Install the relevant library
Step 3: Create a Pipeline and authorize it with the Resource Key
The pipeline, as explained in the basic pipeline description and Pipeline, is the way to consume 51Degrees APIs. Set the Resource Key to be the one you obtained above in step 1.
Step 4: Create a FlowData on the Pipeline and add evidence to it
For reverse geocoding, two pieces of query data (called evidence) are required: latitude and longitude. Each piece of data is passed to the APIs with a key naming the type of data that it is. The relevant evidence keys for reverse geocoding are:
- query.51D_pos_latitude
- query.51D_pos_longitude
Keys are not case-sensitive. Add each piece of evidence with its key.
Step 5: Start a query with process()
This actually initiates the query and contacts your data source, whether the 51Degrees cloud API or a local deployment.
Step 6: Read the results
Results are available in the existing flowData
object (note: they are not returned from the process()
call) in an aspect data dictionary with relevant properties. Each property has a hasValue
boolean; if the property is populated, flowData.property.hasValue
will be true and flowData.property.value
will be the relevant value. If the property is not populated, flowData.property.hasValue
will be false and flowData.propertynullreason
will be populated.
You can get all this code from the examples attached to each library on GitHub, and other examples as well. See below for the link to the appropriate getting-started example and other examples for each language.
Troubleshooting
If you are finding unexpected results from querying the APIs, this section may give some indication of where to start debugging.
Invalid Resource Key
If your Resource Key is invalid or unrecognized by the server, you should receive a server error reading "Resource key not found", but you may also receive an error like "An invalid IP address was specified. Parameter name: address". In both of these cases, generate a new Resource Key with access to the properties you require and use that.
Unauthorized Resource Key
If you find that properties are unexpectedly undefined on your flowData
object (for example, if flowData.location.country
is not present at all, rather than being present with hasValue
false) then your Resource Key may not be authorized to access any location properties at all. Try generating a new Resource Key and using it, or visiting https://configure.51degrees.com/YOUR_RESOURCE_KEY to see the properties which that Resource Key can access. Note that a Resource Key's properties cannot be changed once created; create a new key instead of trying to edit an existing one.
(If you can inspect the JSON response from the HTTP APIs, you may find that device.geolocationnullreason
is set, which is another indicator of this problem.)
Incorrect evidence keys
The list of evidence keys which are used to add evidence must be adhered to. If you provide evidence with an invalid key, it will be ignored. If you do not have any valid keys (or have insufficient valid evidence), then an error like this will be returned:
This property requires evidence values from JavaScript running on the client. It cannot be populated until a future request is made that contains this additional data.
If this happens, check your calls to flowData.evidence.add
and ensure that you are using valid evidence keys. There is deliberately no comprehensive list of evidence keys, because they vary depending on source and API queried. For geocoding, the evidence keys that are likely to be useful are:
- query.51d_pos_latitude
- query.51d_pos_longitude