Jump to content

Google Maps API Directions


kostakondras

Recommended Posts

I am attempting to create a Map on my web page that will have the directions (which will be dynamically pulled from a database) however I have been unable to find a good tutorial on how to achieve this, also there doesn't seem to be any PHP classes available out there (for free anyway) to create this easily.

 

Any suggestions?

Link to comment
https://forums.phpfreaks.com/topic/226320-google-maps-api-directions/
Share on other sites

It's a fairly simply matter to use the Directions API with PHP. 

 

Here's an example which takes you down the Royal Mile in Edinburgh, Scotland.

 

<?php

$endpoint = 'http://maps.googleapis.com/maps/api/directions/json?';
$params   = array(
'origin'      => 'St Giles Cathedral, Edinburgh',
'destination' => 'Holyrood Palace, Edinburgh',
'mode'        => 'walking',
'sensor'      => 'false',
);

// Fetch and decode JSON string into a PHP object
$json = file_get_contents($endpoint.http_build_query($params));
$data = json_decode($json);

// If we got directions, output all of the HTML instructions
if ($data->status === 'OK') {
$route = $data->routes[0];
foreach ($route->legs as $leg) {
	foreach ($leg->steps as $step) {
		echo $step->html_instructions . "<br>\n";
	}
}

}

Will this display the graphical map itself...

 

No, not at all. The code should be simple enough to see that it doesn't!

 

If you just want the directions on a map, read the JavaScript API documentation. Which makes your request not a PHP question, but instead a JavaScript one.  There likely are PHP functions/classes available to put a map onto a page, but they will just be writing the appropriate JavaScript rather than doing anything special or specific to PHP.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.