> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/Braian551/viax/llms.txt
> Use this file to discover all available pages before exploring further.

# Routing

> Calculate routes and distances between locations

## Calculate Route

```
POST /map/calculate_route.php
```

Calculate the optimal route between two locations.

### Request Body

<ParamField body="origin" type="object" required>
  Starting location

  <Expandable title="origin fields">
    <ParamField body="latitud" type="number" required>
      Origin latitude
    </ParamField>

    <ParamField body="longitud" type="number" required>
      Origin longitude
    </ParamField>

    <ParamField body="direccion" type="string">
      Origin address
    </ParamField>
  </Expandable>
</ParamField>

<ParamField body="destination" type="object" required>
  Destination location (same structure as origin)
</ParamField>

### Response

<ResponseField name="success" type="boolean">
  Route calculation success status
</ResponseField>

<ResponseField name="route" type="object">
  Calculated route information

  <Expandable title="route structure">
    <ResponseField name="origen" type="object">
      Origin location details
    </ResponseField>

    <ResponseField name="destino" type="object">
      Destination location details
    </ResponseField>

    <ResponseField name="distancia_km" type="number">
      Total distance in kilometers
    </ResponseField>

    <ResponseField name="duracion_minutos" type="integer">
      Estimated duration in minutes
    </ResponseField>

    <ResponseField name="puntos" type="array">
      Array of coordinates forming the route path
    </ResponseField>

    <ResponseField name="instrucciones" type="string">
      Optional turn-by-turn directions
    </ResponseField>
  </Expandable>
</ResponseField>

### Request Example

<CodeGroup>
  ```bash cURL theme={null}
  curl -X POST https://76.13.114.194/map/calculate_route.php \
    -H "Content-Type: application/json" \
    -d '{
      "origin": {
        "latitud": 4.6814,
        "longitud": -74.0479,
        "direccion": "Carrera 15 #85-30"
      },
      "destination": {
        "latitud": 4.6533,
        "longitud": -74.0602,
        "direccion": "Calle 72 #10-20"
      }
    }'
  ```

  ```dart Dart theme={null}
  Future<RouteModel> calculateRoute(
    Map<String, dynamic> origin,
    Map<String, dynamic> destination,
  ) async {
    final response = await http.post(
      Uri.parse('https://76.13.114.194/map/calculate_route.php'),
      headers: {'Content-Type': 'application/json'},
      body: jsonEncode({
        'origin': origin,
        'destination': destination,
      }),
    );

    if (response.statusCode == 200) {
      final data = jsonDecode(response.body);
      if (data['success'] == true) {
        return RouteModel.fromJson(data['route']);
      }
    }
    throw Exception('Failed to calculate route');
  }
  ```
</CodeGroup>

### Response Example

```json Success theme={null}
{
  "success": true,
  "route": {
    "origen": {
      "latitud": 4.6814,
      "longitud": -74.0479,
      "direccion": "Carrera 15 #85-30"
    },
    "destino": {
      "latitud": 4.6533,
      "longitud": -74.0602,
      "direccion": "Calle 72 #10-20"
    },
    "distancia_km": 4.2,
    "duracion_minutos": 15,
    "puntos": [
      {
        "latitud": 4.6814,
        "longitud": -74.0479
      },
      {
        "latitud": 4.6750,
        "longitud": -74.0510
      },
      {
        "latitud": 4.6650,
        "longitud": -74.0550
      },
      {
        "latitud": 4.6533,
        "longitud": -74.0602
      }
    ],
    "instrucciones": "Siga por Carrera 15 hacia el sur..."
  }
}
```

***

## Calculate Distance

```
POST /map/calculate_distance.php
```

Calculate the distance between two points without full route details.

### Request Body

<ParamField body="origin" type="object" required>
  Starting location with latitud and longitud
</ParamField>

<ParamField body="destination" type="object" required>
  Destination location with latitud and longitud
</ParamField>

### Response

<ResponseField name="success" type="boolean">
  Calculation success status
</ResponseField>

<ResponseField name="distance" type="number">
  Distance in kilometers
</ResponseField>

### Request Example

```bash cURL theme={null}
curl -X POST https://76.13.114.194/map/calculate_distance.php \
  -H "Content-Type: application/json" \
  -d '{
    "origin": {"latitud": 4.6814, "longitud": -74.0479},
    "destination": {"latitud": 4.6533, "longitud": -74.0602}
  }'
```

### Response Example

```json Success theme={null}
{
  "success": true,
  "distance": 4.2
}
```

***

## Display Route on Map

Use the route points to draw a polyline on the map:

```dart theme={null}
import 'package:google_maps_flutter/google_maps_flutter.dart';

Polyline createRoutePolyline(RouteModel route) {
  final points = route.puntos.map((location) {
    return LatLng(location.latitud, location.longitud);
  }).toList();

  return Polyline(
    polylineId: PolylineId('route'),
    points: points,
    color: Colors.blue,
    width: 5,
    geodesic: true,
  );
}

// Usage in GoogleMap widget:
GoogleMap(
  polylines: {createRoutePolyline(route)},
  // ... other properties
);
```

***

## Alternative Routes

<Note>
  Currently, the API returns a single optimal route. Support for alternative routes may be added in future versions.
</Note>

***

## Traffic Consideration

The duration estimate considers:

* **Time of day**: Rush hour vs. off-peak
* **Day of week**: Weekday vs. weekend
* **Historical data**: Average speeds on specific roads
* **Real-time updates**: Current traffic conditions (if available)

***

## Error Responses

<ResponseField name="400" type="Bad Request">
  Invalid coordinates or missing parameters
</ResponseField>

<ResponseField name="500" type="Internal Server Error">
  Routing service error
</ResponseField>

## See Also

* [Geocoding](/api/map/geocoding) - Convert addresses to coordinates
* [Create Trip](/api/trips/create) - Request a trip with calculated route
* [Trip Pricing](/api/trips/pricing) - Estimate trip cost based on distance
