Created
July 3, 2012 06:52
-
-
Save stepankuzmin/3038147 to your computer and use it in GitHub Desktop.
Drupal geocode map field
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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&v=2&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 ?>: </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