Stefan1
September 20, 2023, 6:57am
1
For the first time I try to feed the MapMatching API with a GPX-file, but the only message I get in return is: Unable to process JSON. Can anyone tell me what I do wrong.
php-code:
<?php
$file = "test.gpx";
$fp = fopen($file, "r");
$data = file_get_contents($file);
$query = array(
"gps_accuracy" => "0",
"profile" => "car",
"locale" => "nl",
"elevation" => "false",
"details" => "string",
"instructions" => "true",
"calc_points" => "true",
"points_encoded" => "true",
"key" => "[my_key]",
"data" => $data,
);
$curl = curl_init();
curl_setopt_array($curl, array (
CURLOPT_HTTPHEADER => array('Content-Type: application/application/gpx+xml'),
CURLOPT_URL => "https://graphhopper.com/api/1/match?" . http_build_query($query),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $query,
CURLOPT_INFILE => $fp,
));
$response = curl_exec($curl);
$error = curl_error($curl);
curl_close($curl);
if ($error) {
echo "cURL Error #:" . $error;
} else {
echo json_encode($response);
}
?>
test.gpx:
Test
<gpxx:TrackExtension xmlns:gpxx=“http://www.garmin.com/xmlschemas/GpxExtensions/v3 ”>
gpxx:DisplayColor Red</gpxx:DisplayColor>
</gpxx:TrackExtension>
Stefan1
September 20, 2023, 7:00am
2
The php-code and GPX-file again, but now preformatted
<?php
$file = "test.gpx";
$fp = fopen($file, "r");
$data = file_get_contents($file);
$query = array(
"gps_accuracy" => "0",
"profile" => "car",
"locale" => "nl",
"elevation" => "false",
"details" => "string",
"instructions" => "true",
"calc_points" => "true",
"points_encoded" => "true",
"key" => "[my_key]",
"data" => $data,
);
$curl = curl_init();
curl_setopt_array($curl, array (
CURLOPT_HTTPHEADER => array('Content-Type: application/application/gpx+xml'),
CURLOPT_URL => "https://graphhopper.com/api/1/match?" . http_build_query($query),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $query,
CURLOPT_INFILE => $fp,
));
$response = curl_exec($curl);
$error = curl_error($curl);
curl_close($curl);
if ($error) {
echo "cURL Error #:" . $error;
} else {
echo json_encode($response);
}
?>
Test
Red
```
type or paste code here
```
easbar
September 21, 2023, 8:56am
3
The Content-Type header looks odd. Shouldn’t it be application/gpx+xml
instead of application/applicaition/gpx+xml
?
Stefan1
September 21, 2023, 9:19am
4
Easbar, thanks for youtr reaction. But changing application/applicaition/gpx+xml
in application/gpx+xml
didn’t make a difference. Any other suggestion?
easbar
September 21, 2023, 9:39am
5
Were you able to run one of the examples from the docs? It’s hard to tell what the problem is from the information you provided, but I also don’t know much about PHP.
Stefan1
September 21, 2023, 9:54am
6
I ran an example from the docs. It worked well. Do you know more about Javascript? I also tried it with Javascript/Ajax, but then I get the error: Sequence is broken for submitted track at initial time step.
<script src="jquery-3.3.1.min.js"></script>
<script>
function sendGpx(gpx)
{
$.ajax(
{
url: 'https://graphhopper.com/api/1/match?gps_accuracy=0&profile=car&locale=nl&elevation=false&details=string&instructions=true&calc_points=true&points_encoded=true&key=[my_key]&data=' + gpx,
type: 'POST',
data: gpx,
contentType: 'application/gpx+xml',
headers: { 'Content-Type': 'application/gpx+xml' }
});
}
</script>
<?php
$file = "test.gpx";
$fp = fopen($file, "r");
$data = file_get_contents($file);
?>
<script>
var gpx = '<?php echo $data;?>';
sendGpx(gpx);
</script>