This example shows how a simple location pipeline can be built that looks up an address from longitude and latitude.
This example shows how a simple location pipeline can be built that looks up an address from longitude and latitude This example is available in full on GitHub.
This example uses the 'Country' property, which is pre-populated when creating a key using the link above.
<?php
require(__DIR__ . "/../../vendor/autoload.php");
$resourceKey = EnvironmentVariable::read(
Constants::RESOURCE_KEY_VARIABLE,
Constants::RESOURCE_KEY_ENV_VAR,
"!!YOUR_RESOURCE_KEY!!"
);
if ($resourceKey === '') {
$resourceKey = "!!YOUR_RESOURCE_KEY!!";
}
if (substr($resourceKey, 0, 2) === "!!") {
$message = 'No resource key specified in the environment variable ';
$message .= '"' . Constants::RESOURCE_KEY_VARIABLE . '"' . '<br/>';
$message .= 'Create a resource key with the properties required by this example';
$message .= 'at https://configure.51degrees.com/gGc3T5pT?utm_source=code&utm_medium=example&utm_campaign=51degrees-php&utm_content=examples-cloud-gettingstarted.php&utm_term=resource-key-required' . '<br/>';
$message .= 'Once complete, populate the environment variable ';
$message .= 'mentioned at the start of this message with the key.' . '<br/>';
echo $message;
return;
}
$settings = array(
"resourceKey" => $resourceKey,
"locationProvider" => "fiftyonedegrees"
);
$builder = new GeoLocationPipelineBuilder($settings);
$pipeline = $builder->build();
$flowData = $pipeline->createFlowData();
$latitude = "51.458048";
$longitude = "-0.9822207999999999";
$flowData->evidence->set('query.51D_Pos_latitude', $latitude);
$flowData->evidence->set('query.51D_Pos_longitude', $longitude);
$result = $flowData->process();
$country = $flowData->location->country;
if ($country->hasValue) {
echo "Which country is the location [" . $latitude . "," . $longitude . "] in?";
echo "<br />";
echo $flowData->location->country->value;
} else {
echo $country->noValueMessage;
}
Definition Constants.php:29
Reads an environment variable by its name and, when that is not set or is empty, by the deprecated na...
Definition EnvironmentVariable.php:33