Skip to content

Instantly share code, notes, and snippets.

@stepankuzmin
Created July 3, 2012 06:52
Show Gist options
  • Save stepankuzmin/3038147 to your computer and use it in GitHub Desktop.
Save stepankuzmin/3038147 to your computer and use it in GitHub Desktop.
Drupal geocode map field
<?php
$url = url('http://maps.googleapis.com/maps/api/geocode/json', array('query' => array('address' => 'Екатеринбург, Чайковского 86/3', 'sensor' => 'false')));
$contents = file_get_contents($url);
$response = drupal_json_decode($contents);
if ($response['status'] == 'OK') {
drupal_add_js('https://maps.google.com/maps?file=api&amp;v=2&amp;key=AIzaSyD4iE2xVSpkLLOXoyqT-RuPwURN3ddScAI', 'external');
foreach ($response['results'] as $address) {
$lat = $address['geometry']['location']['lat'];
$lng = $address['geometry']['location']['lng'];
$points = 'map.addOverlay(new GMarker(new GLatLng('.$lat.', '.$lng.')));';
}
drupal_add_js('jQuery(document).ready(function () {if (GBrowserIsCompatible()) {var map = new GMap2(document.getElementById("geocodemap")); map.addControl(new GSmallMapControl()); map.setCenter(new GLatLng('.$lat.', '.$lng.'), 15); ' . $points . '} });', array('type' => 'inline', 'scope' => 'footer'));
}
else {
watchdog('geocode', 'Coud not geocode address %status', array('%status' => $response['status']), WATCHDOG_ERROR);
}
?>
<div class="<?php print $classes; ?>"<?php print $attributes; ?>>
<?php if (!$label_hidden): ?>
<div class="field-label"<?php print $title_attributes; ?>><?php print $label ?>:&nbsp;</div>
<?php endif; ?>
<div class="field-items"<?php print $content_attributes; ?>>
<div id="geocodemap" style="width: 100%; height: 300px"></div>
</div>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment