Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

last update

  • Loading branch information...
commit 6794f8c03d8b2a15aceeec3e6b8c74def58f9e5e 1 parent e0b0949
@adam-s authored
View
30 addressfield.install
@@ -0,0 +1,30 @@
+<?php
+/**
+ * @file
+ * Installation file for addressfield.
+ * Checks if .js file is installed.
+ */
+
+/**
+ * Implementation of hook_requirements().
+ * Added to be sure that user has jmapping.js is installed
+ *
+ */
+function addressfield_requirements($phase) {
+ $requirements = array();
+ $t = get_t();
+ $file_path = drupal_get_path('module', 'addressfield') . '/js/jquery.jmapping.js';
+ switch ($phase) {
+ case 'runtime':
+ if (!file_exists($file_path)) {
+ $requirements['addressfield_js'] = array(
+ 'title' => $t('Addressfield requirements'),
+ 'value' => $t('The Addressfield module requires the <a href="httphttp://vigetlabs.github.com/jmapping/">jMapping</a> file. Download and place in the \'modules/addressfield/js/\' folder.'),
+ 'severity' => REQUIREMENT_WARNING,
+ );
+ }
+ break;
+ }
+ return $requirements;
+}
+
View
174 js/addressfield.js
@@ -0,0 +1,174 @@
+
+/**
+ * @file
+ * Javascript for Goole Map widget of Geolocation field.
+ */
+
+// This is still pretty messy DEV-Code.
+// TODO: cleanup.
+(function ($) {
+ var geocoder;
+ var map;
+ var marker;
+
+ function codeLatLng(latlng, op) {
+ if (geocoder) {
+ geocoder.geocode({'latLng': latlng}, function(results, status) {
+ if (status == google.maps.GeocoderStatus.OK) {
+ if (op == 'mapclick') {
+ $(".field-type-addressfield .form-type-textfield input:not(.geolocation-lat, .geolocation-lng)").val('');
+ for( var i in results) {
+ var type = results[i].types[0];
+ var address = results[i].address_components[0];
+ if (address.types[0] == 'street_number' && results[0].address_components[1].types[0] == 'route') {
+ address.long_name += " " + results[0].address_components[1].long_name;
+ }
+ switch (type) {
+ case 'route':
+ case 'street_address':
+ $(".addressfield-thoroughfare").val(address.long_name);
+ break;
+ case 'postal_code':
+ $(".addressfield-postal-code").val(address.long_name);
+ break;
+ case 'locality':
+ $(".addressfield-locality").val(address.long_name);
+ break;
+ case 'administrative_area_level_1':
+ $(".addressfield-administrative-area").val(address.long_name);
+ break;
+ case 'country':
+ $(".addressfield-country").val(address.short_name);
+ break;
+ }
+ }
+ }
+ } else {
+ if (status != 'ZERO_RESULTS'){
+ alert(Drupal.t('Geocoder failed due to: ') + status);
+ }
+ }
+ });
+ }
+ }
+
+ function codeAddress() {
+ var address = $('.addressfield-thoroughfare').val() + "," + $('.addressfield-locality').val() + "," + $('.addressfield-administrative-area').val() + "," + $('.addressfield-postal-code').val() + "," + $('.addressfield-country').val();
+ geocoder.geocode( { 'address': address}, function(results, status) {
+ if (status == google.maps.GeocoderStatus.OK) {
+ map.setCenter(results[0].geometry.location);
+ setMapMarker(results[0].geometry.location);
+ $("input.geolocation-lat").attr('value', results[0].geometry.location.lat());
+ $("input.geolocation-lng").attr('value', results[0].geometry.location.lng());
+ } else {
+ if (status != "ZERO_RESULTS"){
+ alert(Drupal.t('Geocode was not successful for the following reason: ') + status);
+ }
+ }
+ });
+ }
+
+ function setMapMarker(position) {
+ if (marker) {
+ marker.setMap(null);
+ }
+ marker = new google.maps.Marker({
+ map: map,
+ draggable: false,
+ animation: google.maps.Animation.DROP,
+ position: position
+ });
+ $("input.geolocation-lat").attr('value', position.lat());
+ $("input.geolocation-lng").attr('value', position.lng());
+ return false; // if called from <a>-Tag
+ }
+
+ function getFormattedLocation() {
+ if (google.loader.ClientLocation.address.country_code == "US" &&
+ google.loader.ClientLocation.address.region) {
+ return google.loader.ClientLocation.address.city + ", "
+ + google.loader.ClientLocation.address.region.toUpperCase();
+ } else {
+ return google.loader.ClientLocation.address.city + ", "
+ + google.loader.ClientLocation.address.country_code;
+ }
+ }
+
+ Drupal.behaviors.GoogleMap = {
+ attach: function(context, settings) {
+
+ geocoder = new google.maps.Geocoder();
+
+ // START: Autodetect clientlocation.
+ if (google.loader.ClientLocation) {
+ latlng = new google.maps.LatLng(google.loader.ClientLocation.latitude, google.loader.ClientLocation.longitude);
+ $("#address .description").append(Drupal.t(', or use the IP-based location by clicking this link') +': <b><span id="use-client-location">' + getFormattedLocation() + '</span></b>');
+ }
+ // END: Autodetect clientlocation.
+
+ $("#use-client-location").click(function() {
+ if (google.loader.ClientLocation) {
+ latlng = new google.maps.LatLng(google.loader.ClientLocation.latitude, google.loader.ClientLocation.longitude);
+ $(".geolocation-input").val(getFormattedLocation());
+ map.setCenter(latlng);
+ setMapMarker(latlng);
+ }
+ });
+ // Add a botton to enable manual access to lat and lng fields
+ $("#map:not(.processed)").addClass('processed')
+ .before('<div id="afLatLng" style="clear:both">'+ Drupal.t('Set Lat/Lng Manually') +'</div>')
+ .before('<div id="afGeocode" style="clear:both">'+ Drupal.t('Geocode Address') +'</div>')
+ .before('<div id="afReverseGeocode" style="clear:both">'+ Drupal.t('Look Up Address of Marker') +'</div>');
+
+ // Disable the lat / lng fields
+ $('.geolocation-lat, .geolocation-lng').attr('readonly', 'TRUE').addClass('readonly');
+
+ // Enable editing lat / lng fields
+ $('#afLatLng').click(function(){
+ $('.geolocation-lat, .geolocation-lng').attr('readonly', '').removeClass('readonly');
+ $('.geolocation-lat').focus();
+ });
+
+
+ var lat = $('input.geolocation-lat').attr('value') == '' ? Drupal.settings.map_defaults.lat : $('input.geolocation-lat').attr('value');
+ var lng = $('input.geolocation-lng').attr('value') == '' ? Drupal.settings.map_defaults.lng : $('input.geolocation-lng').attr('value');
+
+ var latlng = new google.maps.LatLng(lat, lng);
+ var myOptions = {
+ zoom: 7,
+ center: latlng,
+ mapTypeId: google.maps.MapTypeId.ROADMAP
+ }
+ map = new google.maps.Map(document.getElementById("map"), myOptions);
+
+ marker = new google.maps.Marker({
+ map: map,
+ draggable: false,
+ position: latlng
+ });
+
+ google.maps.event.addListener(map, 'click',function(me){
+ setMapMarker(me.latLng);
+ });
+
+ // Bind look up geocode address on button click.
+ $('#afGeocode').live('click', function() {
+ codeAddress();
+ });
+
+ // Bind reverse geocode on button click
+ $('#afReverseGeocode').live('click', function() {
+ codeLatLng(marker.position, 'mapclick');
+ })
+
+ // Bind move marker on map to manual lat / lng coord
+ $('.geolocation-lat, .geolocation-lng').live('blur', function() {
+ var newMarker = new google.maps.LatLng($('.geolocation-lat').val(), $('.geolocation-lng').val())
+ setMapMarker(newMarker);
+ map.setCenter(newMarker);
+ });
+
+ }
+ };
+})(jQuery);
+
View
11 theme/addressfield-formatter--city.tpl.php
@@ -0,0 +1,11 @@
+<div class="adr">
+ <div class="street-address">
+ <?php
+ print implode(', ', array_filter(array(
+ $address['locality'],
+ $address['administrative_area'],
+ $address['country']
+ )));
+ ?>
+ </div>
+</div>
View
BIN  theme/loader.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
13 views/addressfield.views.inc
@@ -123,6 +123,19 @@ function addressfield_views_proximity_get_reference_location($view, $options) {
case 'filter':
$coordinates = $view->filter[$options['filter']]->value;
break;
+ case 'nid_arg':
+ if ((arg(0) == 'node') && is_numeric(arg(1))) {
+ if ($tempnode = node_load(arg(1))) {
+ $fields = $tempnode->field_address[$tempnode->language][0];
+ $coordinates['latitude'] = (float) $fields['lat'];
+ $coordinates['longitude'] = (float) $fields['lng'];
+ }
+ if (isset($view->filter['proximity'])){
+ $view->filter['proximity']->value['latitude'] = $options['value']['latitude'] = $coordinates['latitude'];
+ $view->filter['proximity']->value['longitude'] = $options['value']['longitude'] = $coordinates['longitude'];
+ }
+ }
+ break;
}
return $coordinates;
View
3  views/addressfield_handler_field_distance.inc
@@ -14,6 +14,7 @@ class addressfield_handler_field_distance extends views_handler_field {
$options['latitude'] = array('default' => '');
$options['longitude'] = array('default' => '');
$options['php_code'] = array('default' => '');
+ $options['filter'] = array('default' => '');
return $options;
}
@@ -115,7 +116,7 @@ class addressfield_handler_field_distance extends views_handler_field {
$coordinates = addressfield_views_proximity_get_reference_location($this->view, $this->options);
if (!empty($coordinates)) {
- $this->field_alias = $this->query->add_field(NULL, earth_distance_sql($coordinates['longitude'], $coordinates['latitude'], $this->table_alias), $this->table_alias .'_'. $this->field);
+ $this->field_alias = $this->query->add_field(NULL, earth_distance_sql((float)$coordinates['longitude'],(float)$coordinates['latitude'], $this->table_alias), $this->table_alias .'_'. $this->field);
}
else {
$this->field_alias = $this->query->add_field(NULL, "'Unknown'", $this->table_alias .'_'. $this->field);
View
22 views/addressfield_handler_filter_proximity.inc
@@ -20,6 +20,7 @@ class addressfield_handler_filter_proximity extends views_handler_filter {
'latitude' => '',
'longitude' => '',
'filter' => '',
+ 'nid_arg' => '',
'search_distance' => 100,
'search_units' => 'mile',
),
@@ -100,7 +101,26 @@ class addressfield_handler_filter_proximity extends views_handler_filter {
'#dependency' => array('edit-options-origin' => array('static',)),
'#weight' => 2,
);
-
+ $arguments = $this->view->display_handler->get_handlers('argument');
+ $argument_options = array();
+ if ($arguments) {
+ foreach ($arguments as $id => $handler) {
+ if ($handler->field == 'nid') {
+ $argument_options[$id] = $handler->ui_name();
+ }
+ }
+ }
+ $form['value']['nid_arg'] = array(
+ '#type' => 'select',
+ '#title' => t('Node ID argument to use'),
+ '#options' => $argument_options,
+ '#default_value' => $this->value['nid_arg'],
+ '#description' => empty($argument_options) ? t("Select which of the view's arguments to use as the node ID. The latitude / longitude of the first location of that node will be used as the origin. Use the 'Global: Null' argument if you don't want to also restrict results to that node ID. You must have added arguments to the view to use this option.") : t("Select which of the view's arguments to use as the node ID. The latitude / longitude of the first location of that node will be used as the origin. Use the 'Global: Null' argument if you don't want to also restrict results to that node ID."),
+ '#process' => array('ctools_dependent_process'),
+ '#dependency' => array('edit-options-origin' => array('nid_arg')),
+ '#weight' => 40,
+ );
+
$form['value']['php_code'] = array(
'#type' => 'textarea',
'#title' => t('PHP code for latitude, longitude'),
View
33 views/addressfield_handler_sort_distance.inc
@@ -13,6 +13,7 @@ class addressfield_handler_sort_distance extends views_handler_sort {
$options['latitude'] = array('default' => '');
$options['longitude'] = array('default' => '');
$options['filter'] = array('default' => '');
+ $options['nid_arg'] = array('default' => '');
return $options;
}
@@ -27,6 +28,7 @@ class addressfield_handler_sort_distance extends views_handler_sort {
'#options' => array(
'static' => t('Static Latitude / Longitude'),
'filter' => t('Use the values of a filter to determine latitude/longitude'),
+ 'nid_arg' => t("Node's Latitude / Longitude from views nid argument"),
),
'#description' => t("This will be the way the latitude/longitude of origin is determined."),
'#default_value' => $this->options['origin'],
@@ -55,15 +57,6 @@ class addressfield_handler_sort_distance extends views_handler_sort {
}
}
}
- $argumentsa = $this->view->display_handler->get_handlers('argument');
- $argument_optionsa = array();
- if ($argumentsa) {
- foreach ($argumentsa as $id => $handler) {
- if ($handler->field == 'proximity') {
- $argument_optionsa[$id] = $handler->ui_name();
- }
- }
- }
$form['filter'] = array(
'#type' => 'select',
'#options' => $argument_options,
@@ -72,7 +65,25 @@ class addressfield_handler_sort_distance extends views_handler_sort {
'#description' => ("Select which of the view's filters to use as the default settings for the sort."),
'#dependency' => array('edit-options-origin' => array('filter')),
);
-
+ $nid_arguments = $this->view->display_handler->get_handlers('argument');
+ $nid_argument_options = array();
+ if ($nid_arguments) {
+ foreach ($nid_arguments as $id => $handler) {
+ if ($handler->field == 'nid') {
+ $nid_argument_options[$id] = $handler->ui_name();
+ }
+ }
+ }
+ $form['nid_arg'] = array(
+ '#type' => 'select',
+ '#title' => t('Node ID argument to use'),
+ '#options' => $nid_argument_options,
+ '#default_value' => $this->value['nid_arg'],
+ '#description' => empty($nid_argument_options) ? t("Select which of the view's arguments to use as the node ID. The latitude / longitude of the first location of that node will be used as the origin. Use the 'Global: Null' argument if you don't want to also restrict results to that node ID. You must have added arguments to the view to use this option.") : t("Select which of the view's arguments to use as the node ID. The latitude / longitude of the first location of that node will be used as the origin. Use the 'Global: Null' argument if you don't want to also restrict results to that node ID."),
+ '#process' => array('ctools_dependent_process'),
+ '#dependency' => array('edit-options-origin' => array('nid_arg')),
+ '#weight' => 40,
+ );
}
function query() {
@@ -107,7 +118,7 @@ class addressfield_handler_sort_distance extends views_handler_sort {
// This is done exactly the same as the field version.
// Views is ok with us redefining the formula for a field.
// If ANYTHING differs in the configuration, we will use a new alias.
- $this->query->add_orderby(NULL, earth_distance_sql($coordinates['longitude'], $coordinates['latitude'], $this->table_alias), $this->options['order'], $alias);
+ $this->query->add_orderby(NULL, earth_distance_sql((float)$coordinates['longitude'], (float)$coordinates['latitude'], $this->table_alias), $this->options['order'], $alias);
}
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.