Brainsofsteel header

How to embed a google map with marker in PHP

Subject: Programming

Description: How to easily embed a Google map with a marker

Posted by David Caldwell on 26/08/13 at 12:13

Sometimes something that sounds complicated turns out to be easy.  I was creating a page that would display details of councils taken from an xml page, I wanted to embed a google map zoomed in on the latitude or longitude and showing a map marker.

The PHP Bit

Declare two variables one for latitude and One for longitude, in this case I'm getting them from an xml file, but you could easily get them from a database

<?

$lat = $council->lat;
$lng = $council->lng;

?>

The Google Map Bit

We are using a static map, for simplicity I am avoiding the javascript or iframe routes.  I have set the map type to hybrid. 

<img src="https://maps.google.com/maps/api/staticmap?center=<?php echo $lat ?>,<?php echo $lng ?>&zoom=14&markers=<?php echo $lat ?>,<?php echo $lng ?>&maptype=hybrid&&size=400x300&sensor=false" style="width: 400px; height: 400px;" />

 

 

 

Keywords: php, google map, marker, embed