What are the differences between using Google Maps API and OpenStreetMap for routing and mapping functionalities?
Google Maps API is a paid service that offers advanced routing and mapping functionalities with detailed and up-to-date data. On the other hand, OpenStreetMap is a free and open-source platform that provides mapping data contributed by users worldwide. Using Google Maps API may provide more accurate and reliable results, but it comes with a cost, while OpenStreetMap is free to use but may have limitations in terms of data accuracy and coverage.
// Example of using Google Maps API for routing and mapping functionalities
$apiKey = 'YOUR_GOOGLE_MAPS_API_KEY';
$origin = 'New York, NY';
$destination = 'Los Angeles, CA';
$url = "https://maps.googleapis.com/maps/api/directions/json?origin=$origin&destination=$destination&key=$apiKey";
$response = file_get_contents($url);
$data = json_decode($response, true);
// Extract and display routing information from the response data
echo "Distance: " . $data['routes'][0]['legs'][0]['distance']['text'] . "<br>";
echo "Duration: " . $data['routes'][0]['legs'][0]['duration']['text'];
```
```php
// Example of using OpenStreetMap for routing and mapping functionalities
$origin = 'New York, NY';
$destination = 'Los Angeles, CA';
$url = "https://router.project-osrm.org/route/v1/driving/$origin;$destination?overview=false";
$response = file_get_contents($url);
$data = json_decode($response, true);
// Extract and display routing information from the response data
echo "Distance: " . $data['routes'][0]['distance'] . " meters<br>";
echo "Duration: " . $data['routes'][0]['duration'] . " seconds";