Home > database >  How can I calculate the KM's between two points?
How can I calculate the KM's between two points?

Time:04-04

I've been working on a website and one of its functionalities is a budget maker, that is, a place where the user can somewhat find out what they'd be spending for their service of choice, however, googles API is paid and at the moment, for this project, it isn't viable for me to do it that way, I've been trying to use Microsoft's Bing maps API, but its all with "URLs" and JSON's and I'm kind of out of my depth, can anyone explain to me maybe what I need to do to "decode" the JSON file and get the distance parameters?

Documentation: https://docs.microsoft.com/en-us/bingmaps/rest-services/routes/calculate-a-route

My URL code: "http://dev.virtualearth.net/REST/v1/Routes?wayPoint.1=$startAdress&viaWaypoint.2=$UserInputedAdress&travelMode=Driving&optimize=distance&distanceUnit=km&key=BingMapsKey";

My code for the decoding json - print_r(json_decode($distanciaBing)); (always returns '1', nothing else no matter the inputs.

CodePudding user response:

It really depends on what $distanciaBing is from your question. I will infer that that is a string that contains the query data you need to parse.

From there it will just be a matter of locating the beginning and end of the JSON data that you actually need.

You will have to do this manually (at first) before hard coding it. It will simply be a matter of locating in that return result, manually, something unique that begins that JSON string. Again, and manually locate something unique that ends that JSON string. Then you will be able to extract it from $distanciaBing.

E.g.

$uniqueStart = ""; // replace what you found on that which is unique that is just before the JSON string

$uniqueEnd = ""; // replace what you found on that which is unique just after the JSON string

$jsonBeginCharPos = strpos($distanciaBing, $uniqueStart)   strlen($uniqueStart);

$jsonEndCharPos = strpos($distanciaBing, $uniqueEnd) - 1;

$jsonStringNeeded = substr($distanciaBing, $jsonBeginCharPos, ($jsonEndCharPos - $jsonBeginCharPos)   1);

On latter, we extract the string we want, that being the JSON string. If on manual tests this ends up being the string we want, we can then hard code this functionality.

So up to this point we have sucessfully extracted the json string, and now can convert it into an associative array via PHP parlance:

$jsonArray = json_decode( $jsonStringNeeded, true );

And from here you can do whatever it is that needs to be done to use that data.

CodePudding user response:

Might be worth it to someone in the future if they ever come across this post, so: this is the fix to get the object back from the JSON:

$getDistance = "http://dev.virtualearth.net/REST/v1/Routes?o=xml&wp.0=$startAdress&wp.1=$endAdress&travelMode=Driving&optimize=distance&distanceUnit=km&key=(bingKey)";

$fileContents = file_get_contents($getDistance);
$fileContents = str_replace(array("\n", "\r", "\t"), '', $fileContents);
$fileContents = trim(str_replace('"', "'", $fileContents));
$simpleXml = simplexml_load_string($fileContents);

$json = json_encode($simpleXml);
$obj= json_decode($json);

$TravelDistance = $obj->ResourceSets->ResourceSet->Resources->Route->TravelDistance; 

It's a tad weird had to get it from JSON to XML back to JSON with 'encode' and then finally decode it one last time to get access to the object with every field to go through it.

  • Related