Author Topic: Google Map Droplet  (Read 5691 times)

Offline mdemaree99

  • Posts: 102
Google Map Droplet
« on: May 03, 2014, 05:15:05 PM »
Droplet for Google Maps that does not require an API Key

Can change address, zoom, height and width.
Default zoom: 14
Width:  500
Height:  500


[[GoogleMap?address=The White House 1600 Pennsylvania Ave NW Washington DC&zoom=14&zoom=14&width=500&height=500]]

Code: [Select]
if (!isset($zoom)) $zoom=14;
if (!isset($width)) $width=500;
if (!isset($height)) $height=500;

$data = file_get_contents("http://maps.googleapis.com/maps/api/geocode/json?address=".urlencode($address)."&sensor=false");
$obj = json_decode($data);
  if ($obj) {
    if (isset($obj->results[0]->geometry->location)) {
      $loc = $obj->results[0]->geometry->location;
      $array = array('latitude'=>$loc->lat, 'longitude'=>$loc->lng);
     $string_address = implode(',', $array);
    } else {
      throw new Exception('Lookup failed and/or address does not exist!');
    }
  } else {
    throw new Exception('Lookup failed and/or address does not exist!');
  }

echo'
<style>
       #map_canvas {
        margin: 0;
        padding: 0;
        height:'.$height .';
        width:'.$width .'
      }
</style>
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
    <script>
      function initialize() {
        var mapOptions = {
          zoom: '. $zoom .',
          center: new google.maps.LatLng(' . $string_address .'),
          mapTypeId: google.maps.MapTypeId.ROADMAP};       
          map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions); 
          var marker = new google.maps.Marker({ position: new google.maps.LatLng(' . $string_address .') , map: map });         
           }
google.maps.event.addDomListener(window, "load", initialize);
    </script>';
return '<div id="map_canvas"></div>';

Offline Hans

  • Posts: 912
  • Gender: Male
Re: Google Map Droplet
« Reply #1 on: May 04, 2014, 09:10:39 AM »
Thank you, works perfect!
Hans
Hans - Nijmegen - The Netherlands

jacobi22

  • Guest
Re: Google Map Droplet
« Reply #2 on: May 05, 2014, 03:33:18 PM »
thanks for sharing!