Skip to content
Snippets Groups Projects
Commit 728a1487 authored by sydneyshan's avatar sydneyshan Committed by Andriy Podanenko
Browse files

Issue #1921872 by sydneyshan: Added Find address on map button.

parent 094a8bbe
No related branches found
No related tags found
No related merge requests found
name = Location + Gmap 'Find Address' button
description = "Adds a 'Find Address' button to all location fields that use gmap"
version = 7.x-1.2
core = 7.x
dependencies[] = location
dependencies[] = gmap
(function ($) {
Drupal.behaviors.mjh_views_alter = {
attach: function(){
$("button.location-gmap-find-address-button").click(function(e){
e.preventDefault();
var address_parts = new Array();
$("fieldset#edit-" + $(this).val() + " .form-item input[type=text]," +
"fieldset#edit-" + $(this).val() + " .form-item select > option:selected").each(function(){
if (!$(this).hasClass('gmap-control') && $(this).val() != '') {
if($(this).is('option')) {
address_parts.push($(this).text());
} else {
address_parts.push($(this).val());
}
}
});
var address_string = address_parts.join(', ');
var gmap_id = $("fieldset#edit-" + $(this).val() + " .gmap-map").attr('id');
if (google.maps.version !== 'undefined') { // assume Google Maps API v3 as API v2 did not have this variable
var geocoder = new google.maps.Geocoder();
geocoder.geocode({'address': address_string}, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var m = Drupal.gmap.getMap(gmap_id);
m.locpick_coord = results[0].geometry.location;
m.change('locpickchange');
m.map.setCenter(results[0].geometry.location);
m.map.setZoom(14);
}
else {
alert(Drupal.t("Your address was not found."));
}
});
}
else {
var geocoder = new GClientGeocoder();
geocoder.reset(); // Clear the client-side cache
geocoder.getLatLng(address_string, function(point){
if (!point){
alert(Drupal.t("Your address was not found."));
}
else {
var m = Drupal.gmap.getMap(gmap_id);
m.locpick_coord = point;
m.change('locpickchange');
m.map.setCenter(point, 14);
}
});
}
});
}
}
})(jQuery);
<?php
/**
* hook_form_alter() override
*/
function location_gmap_find_address_form_alter(&$form, &$form_state, $form_id) {
if (isset($form['#node_edit_form']) && $form['#node_edit_form'] && variable_get('location_usegmap', 0)) {
$fields = field_info_instances($form['#entity_type'], $form['#bundle']);
$form['#location_gmap_find_address_fields'] = array();
foreach($fields as $field_name => $field) {
if ($field['widget']['type'] == 'location') {
$form['#location_gmap_find_address_fields'][] = $field_name;
}
}
if (count($form['#location_gmap_find_address_fields'])) {
$form["#after_build"][] = "location_gmap_find_address_after_build";
}
}
}
/**
* After build event for this form
*/
function location_gmap_find_address_after_build($form, &$form_state) {
foreach ($form['#location_gmap_find_address_fields'] as $field_name) {
$field_instance_count = $form[$field_name]['und']['#max_delta'];
for ($i = 0; $i <= $field_instance_count; $i++) {
if(isset($form[$field_name]['und'][$i]['locpick'])) {
$form[$field_name]['und'][$i]['locpick']["#prefix"] = '<div class="location-gmap-find-address-button-wrapper"><button type="button" class="location-gmap-find-address-button" value="' . str_replace('_', '-', $field_name) . '-und-' . $i . '">' . t('Find Address on Map') . '</button></div>';
}
}
}
drupal_add_js(drupal_get_path("module", "location_gmap_find_address") . "/location_gmap_find_address.js");
return $form;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment