Skip to content

Instantly share code, notes, and snippets.

@gormus
Forked from clnmcgrw/gist:9086505
Created October 22, 2015 19:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gormus/8bae683acb53237fd770 to your computer and use it in GitHub Desktop.
Save gormus/8bae683acb53237fd770 to your computer and use it in GitHub Desktop.
Google Maps API Geocode Example - PHP/JSON
<?php
// address to map
$map_address = "";
$url = "http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=".urlencode($map_address);
$lat_long = get_object_vars(json_decode(file_get_contents($url)));
// pick out what we need (lat,lng)
$lat_long = $lat_long['results'][0]->geometry->location->lat . "," . $lat_long['results'][0]->geometry->location->lng;
?>
<script src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<div id="map_canvas"></div>
<script>
(function() {
function initialize() {
var myLatlng = new google.maps.LatLng(<?php echo $lat_long; ?>),
mapOptions = {
zoom: 15,
center: myLatlng
},
map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions),
marker = new google.maps.Marker({
position: myLatlng,
map: map,
title: '<?php echo $map_address; ?>'
});
}
google.maps.event.addDomListener(window, 'load', initialize);
})();
</script>
@RJTMediaGroup
Copy link

Stupid Question. Where on the page should I put the code. I had a basic jscript from Google between the closing Container DIV and the closing Body Tag.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment