public
Created

combining geocode and infowindow

  • Download Gist
map.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
<?php
$address = "Sutorejo Utara, Surabaya, Indonesia";
$address = urlencode($address);
$url = "http://maps.googleapis.com/maps/api/geocode/json?address=$address&sensor=false";
$raw = file_get_contents($url);
$data = json_decode($raw);
 
$lat = $data->results[0]->geometry->location->lat;
$lng = $data->results[0]->geometry->location->lng;
 
// print $lat. ", ".$lng;
 
$marker_text = "<strong>Location:</strong> <br />Jalan Sutorejo Utara VII/1 <br/> Surabaya, INDONESIA."
?>
<html>
<head>
<style type="text/css">
div#map{ width:450px; height:350px; }
</style>
</head>
<body>
<div id="map"></div>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script>
<script type="text/javascript">
window.onload = function() {
var settings = {
mapTypeId: google.maps.MapTypeId.ROADMAP,
zoom: 15,
center: new google.maps.LatLng(<?php print $lat ?>, <?php print $lng ?>)
};
 
var map = new google.maps.Map(document.getElementById("map"), settings);
 
var marker = new google.maps.Marker({
position: new google.maps.LatLng(<?php print $lat ?>, <?php print $lng ?>),
map: map
});
 
var infowindow = new google.maps.InfoWindow({
content: '<?php print $marker_text ?>'
});
 
google.maps.event.addListener(marker, 'click', function() {
infowindow.open(map, marker);
});
}
</script>
</body>
</html>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.