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
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";
	}
}

}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.