bradcornford / Googlmapper

An easy way to integrate Google Maps with Laravel
MIT License
463 stars 143 forks source link

gpx #374

Open dennisbusk opened 3 years ago

dennisbusk commented 3 years ago

Hi... Great product.

I was just wondering if it possible to load a gpx file and show a route on the map?

bradcornford commented 2 years ago

Hi,

This package doesn't natively support this, however I'm sure you can achieve this using PHP itself, you could read the GPX file, and iterate over each of the found lat/longs and draw polylines from the initial point and the next. Something along the lines of the following:


Mapper::map(53.381128999999990000, -1.470085000000040000);
$gpx = simplexml_load_file("test.gpx");
$points = [];

foreach ($gpx->trk as $trk) {
    foreach($trk->trkseg as $seg) {
        foreach($seg->trkpt as $pt) {
            $points[] = [
                'lat' => $pt["lat"], 
                'lon' => $pt["lon"]
            ];
        }
    }
}
unset($gpx);

$previousPoint = null;

foreach ($points as $point) {
    if ($previousPoint !== null) {
        Mapper::polyline([['latitude' =>  $previousPoint['lat'], 'longitude' => $previousPoint['lon']], ['latitude' => $point['lat'], 'longitude' => $point['lon']]]);
    }
    $previousPoint = $point;
}