debug(print_r($paths, true)); $skip_path = false; // skip if coordinates are outside of 111 meters of middle point // because mapbox only allows 100 paths max foreach($path as $coords) { $lat_less = $gps_lat; $lat_more = $coords[0]; $long_less = $gps_long; $long_more = $coords[1]; if($coords[0] < $gps_lat) { $lat_less = $coords[0]; $lat_more = $gps_lat; } if($coords[1] < $gps_long) { $long_less = $coords[1]; $long_more = $gps_long; } mfLoghandler::singleton()->debug(__METHOD__.": lat_more($lat_more) - lat_less($lat_less) = ".($lat_more - $lat_less)); mfLoghandler::singleton()->debug(__METHOD__.": long_more($long_more) - long_less($long_less) = ".($long_more - $long_less)); if($lat_more - $lat_less > 0.0005 || $long_more - $long_less > 0.0005) { mfLoghandler::singleton()->debug(__METHOD__.": Skipping path"); $skip_path = true; } } if($skip_path) continue; if(!is_array($path) || !count($path)) { mfLoghandler::singleton()->debug("path not array or empty: ".print_r($path, true)); continue; } if($path_count > 99) break; $path_count++; $path_enc_polyline = urlencode(self::encodeCoordArrayToPolyline($path)); //$path_enc_polyline = self::encodeCoordArrayToPolyline($path); //if(!$path_enc_polyline) continue; //$pp = "path-$path_stroke_width+$path_stroke_color-$path_stroke_opacity+$path_fill_color-$path_fill_opacity($path_enc_polyline)" $pp = "path-$path_stroke_width+$path_stroke_color-$path_stroke_opacity($path_enc_polyline)"; $path_parts[] = $pp; } } mfLoghandler::singleton()->debug(__METHOD__.": path count: $path_count | initial path count: $initial_path_count"); // build url $url_opt_parts = []; if($pin_part) $url_opt_parts[] = $pin_part; if(count($path_parts)) { foreach($path_parts as $path_part) { $url_opt_parts[] = $path_part; } } if(is_array($url_opt_parts) && count($url_opt_parts)) { $url .= "/".implode(",", $url_opt_parts); } $url .= "/$gps_long,$gps_lat,$zoom/{$size_x}x{$size_y}?access_token=$access_token"; //$url .= "/auto/{$size_x}x{$size_y}?access_token=$access_token"; mfLoghandler::singleton()->debug($url); //exit; $ctx_opts = [ 'http' => [ 'method' => 'GET', 'header' => 'accept: application/json' ] ]; $ctx = stream_context_create($ctx_opts); $response = file_get_contents($url, false, $ctx); //fLoghandler::singleton()->debug(print_r($response, true)); if($response === false) { return null; } return $response; } public static function encodeCoordArrayToPolyline($coords, $precision = 5) { //if(!is_array($coords)) return false; $points = []; array_walk_recursive( $coords, function ($current) use (&$points) { $points[] = $current; } ); mfLoghandler::singleton()->debug(__METHOD__.": flattened path: ".print_r($points, true)); $encodedString = ''; $index = 0; $previous = array(0,0); foreach ( $points as $number ) { $number = (float)($number); $number = (int)round($number * pow(10, $precision)); $diff = $number - $previous[$index % 2]; $previous[$index % 2] = $number; $number = $diff; $index++; $number = ($number < 0) ? ~($number << 1) : ($number << 1); $chunk = ''; while ( $number >= 0x20 ) { $chunk .= chr((0x20 | ($number & 0x1f)) + 63); $number >>= 5; } $chunk .= chr($number + 63); $encodedString .= $chunk; } mfLoghandler::singleton()->debug(__METHOD__.": encoded polyline: $encodedString"); //return str_replace("?", "%3f", $encodedString); return $encodedString; } }