Skip to content

Commit

Permalink
Dev Moved to Google Maps API V3
Browse files Browse the repository at this point in the history
git-svn-id: file:///Users/Shitiz/Downloads/lssvn/source/limesurvey_dev@10706 b72ed6b6-b9f8-46b5-92b4-906544132732
  • Loading branch information
Tony Partner committed Aug 12, 2011
1 parent db8dbe0 commit ec4767d
Show file tree
Hide file tree
Showing 2 changed files with 164 additions and 180 deletions.
2 changes: 1 addition & 1 deletion qanda.php
Original file line number Diff line number Diff line change
Expand Up @@ -5102,7 +5102,7 @@ class=\"mapservice\" value = \"{$qidattributes['location_mapservice']}\" >
<div id=\"gmap_canvas_$ia[1]_c\" style=\"width: {$qidattributes['location_mapwidth']}px; height: {$qidattributes['location_mapheight']}px\"></div>";

if ($qidattributes['location_mapservice']==1)
$js_header_includes[] = "http://maps.google.com/maps?file=api&amp;v=2&amp;sensor=false&amp;key={$googleMapsAPIKey}";
$js_header_includes[] = "http://maps.googleapis.com/maps/api/js?sensor=false";
elseif ($qidattributes['location_mapservice']==2)
$js_header_includes[] = "http://www.openlayers.org/api/OpenLayers.js";

Expand Down
342 changes: 163 additions & 179 deletions scripts/survey_runtime.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var DOM1;

$(document).ready(function()
{
DOM1 = (typeof document.getElementsByTagName!='undefined');
Expand All @@ -12,98 +13,101 @@ $(document).ready(function()
$(".question").find("select").each(function () {
hookEvent($(this).attr('id'),'mousewheel',noScroll);
});
var kp = $("input.num-keypad");
if(kp.length) kp.keypad({showAnim: 'fadeIn', keypadOnly: false});
kp = $("input.text-keypad");
if(kp.length)
{
var spacer = $.keypad.HALF_SPACE;
for(var i = 0; i != 8; ++i) spacer += $.keypad.SPACE;
kp.keypad({
showAnim: 'fadeIn',
keypadOnly: false,
layout: [
spacer + $.keypad.CLEAR + $.keypad.CLOSE, $.keypad.SPACE,
'!@#$%^&*()_=' + $.keypad.HALF_SPACE + $.keypad.BACK,
$.keypad.HALF_SPACE + '`~[]{}<>\\|/' + $.keypad.SPACE + $.keypad.SPACE + '789',
'qwertyuiop\'"' + $.keypad.HALF_SPACE + $.keypad.SPACE + '456',
$.keypad.HALF_SPACE + 'asdfghjkl;:' + $.keypad.SPACE + $.keypad.SPACE + '123',
$.keypad.SPACE + 'zxcvbnm,.?' + $.keypad.SPACE + $.keypad.SPACE + $.keypad.HALF_SPACE + '-0+',
$.keypad.SHIFT + $.keypad.SPACE_BAR + $.keypad.ENTER]});
}

$(".location").each(function(index,element){
var question = $(element).attr('name');
var coordinates = $(element).val();
var latLng = coordinates.split(" ");
var question_id = question.substr(0,question.length-2);
if ($("#mapservice_"+question_id).val()==1){
// Google Maps
if (gmaps[''+question]==undefined)
gmaps[''+question] = GMapsInitialize(question,latLng[0],latLng[1]);

// Virtual keypad/keyboard
var kp = $("input.num-keypad");
if(kp.length) kp.keypad({showAnim: 'fadeIn', keypadOnly: false});
kp = $("input.text-keypad");
if(kp.length)
{
var spacer = $.keypad.HALF_SPACE;
for(var i = 0; i != 8; ++i) spacer += $.keypad.SPACE;
kp.keypad({
showAnim: 'fadeIn',
keypadOnly: false,
layout: [
spacer + $.keypad.CLEAR + $.keypad.CLOSE, $.keypad.SPACE,
'!@#$%^&*()_=' + $.keypad.HALF_SPACE + $.keypad.BACK,
$.keypad.HALF_SPACE + '`~[]{}<>\\|/' + $.keypad.SPACE + $.keypad.SPACE + '789',
'qwertyuiop\'"' + $.keypad.HALF_SPACE + $.keypad.SPACE + '456',
$.keypad.HALF_SPACE + 'asdfghjkl;:' + $.keypad.SPACE + $.keypad.SPACE + '123',
$.keypad.SPACE + 'zxcvbnm,.?' + $.keypad.SPACE + $.keypad.SPACE + $.keypad.HALF_SPACE + '-0+',
$.keypad.SHIFT + $.keypad.SPACE_BAR + $.keypad.ENTER]});
}

// Maps
$(".location").each(function(index,element){
var question = $(element).attr('name');
var coordinates = $(element).val();
var latLng = coordinates.split(" ");
var question_id = question.substr(0,question.length-2);
if ($("#mapservice_"+question_id).val()==1){
// Google Maps
if (gmaps[''+question] == undefined) {
GMapsInitialize(question,latLng[0],latLng[1]);
}
}
else if ($("#mapservice_"+question_id).val()==2){
// Open Street Map
if (osmaps[''+question]==undefined) {
osmaps[''+question] = OSMapInitialize(question,latLng[0],latLng[1]);
}
}
});
$(".location").live('focusout',function(event){
var question = $(event.target).attr('name');
var name = question.substr(0,question.length - 2);
var coordinates = $(event.target).attr('value');
var xy = coordinates.split(" ");
var currentMap = gmaps[question];
var marker = gmaps['marker__'+question];
var markerLatLng = new google.maps.LatLng(xy[0],xy[1]);
geocodeAddress(name, markerLatLng);
marker.setPosition(markerLatLng);
currentMap.panTo(markerLatLng);
});

if ((typeof(autoArray) != "undefined")){
if ((autoArray.list != 'undefined') && (autoArray.list.length > 0)){
var aListOfQuestions = autoArray.list;

$(aListOfQuestions).each(function(index,element){

var elementInfo = autoArray[element];
var strJSelector = "#answer" + (elementInfo.children.join(", #answer"));

var aJSelectors = strJSelector.split(", ");
var strCheckedSelector = (aJSelectors.join(":checked ,"))+":checked";

$(strJSelector).live('change',function(event){

if ($(strCheckedSelector).length == $(strJSelector).length){

$("#answer"+elementInfo.focus).trigger('click');

eval("excludeAllOthers"+elementInfo.parent + "('answer"+elementInfo.focus + "', 'yes')");

checkconditions($("#answer"+elementInfo.focus).val(),
$("#answer"+elementInfo.focus).attr("name"),
$("#answer"+elementInfo.focus).attr('type')
);

}
else if ($("#mapservice_"+question_id).val()==2){
// Open Street Map
if (osmaps[''+question]==undefined)
osmaps[''+question] = OSMapInitialize(question,latLng[0],latLng[1]);
}
});
$(".location").live('focusout',function(event){
var question = $(event.target).attr('name');
var name = question.substr(0,question.length - 2);
var coordinates = $(event.target).attr('value');
var xy = coordinates.split(" ");
var currentMap = gmaps[question];
var marker = gmaps["marker__"+question];
var markerLatLng = new GLatLng(xy[0],xy[1]);
marker.setLatLng(markerLatLng);
var geocoder = new GClientGeocoder();
geocoder.getLocations(markerLatLng,function(response){

parseGeocodeAddress(response,name);
});
currentMap.panTo(markerLatLng);
});
if ((typeof(autoArray) != "undefined")){
if ((autoArray.list != 'undefined') && (autoArray.list.length > 0)){
var aListOfQuestions = autoArray.list;

$(aListOfQuestions).each(function(index,element){

var elementInfo = autoArray[element];
var strJSelector = "#answer" + (elementInfo.children.join(", #answer"));

var aJSelectors = strJSelector.split(", ");
var strCheckedSelector = (aJSelectors.join(":checked ,"))+":checked";

$(strJSelector).live('change',function(event){

if ($(strCheckedSelector).length == $(strJSelector).length){

$("#answer"+elementInfo.focus).trigger('click');

eval("excludeAllOthers"+elementInfo.parent + "('answer"+elementInfo.focus + "', 'yes')");

checkconditions($("#answer"+elementInfo.focus).val(),
$("#answer"+elementInfo.focus).attr("name"),
$("#answer"+elementInfo.focus).attr('type')
);

}
});

});
}
}
// disable double-posts in all forms
$('form').submit(function()
{
$('input[type=button], input[type=submit]', this).attr('disabled', 'disabled');
});
});

});
}
}

// disable double-posts in all forms
$('form').submit(function()
{
$('input[type=button], input[type=submit]', this).attr('disabled', 'disabled');
});
});

gmaps = new Object;
osmaps = new Object;
gmaps = new Object();
osmaps = new Object();
zoom = [];

// OSMap functions
Expand All @@ -125,115 +129,96 @@ function OSMapInitialize(question,lat,lng){

}

// Google Maps Functions
//// Google Maps Functions (for API V3) ////

// Initialize map
function GMapsInitialize(question,lat,lng) {
if (GBrowserIsCompatible()) {
var map = new GMap2(document.getElementById("gmap_canvas_" + question));
var center = new GLatLng(lat,lng);
map.addControl(new GSmallMapControl());

var name = question.substr(0,question.length - 2);
map.setCenter(center, zoom[name]);
var marker = new GMarker(center, {draggable: true});
GEvent.addListener(map, "singlerightclick", function(GP) {
var markerLatLng = map.fromContainerPixelToLatLng(GP);
marker.setLatLng(markerLatLng);
var geocoder = new GClientGeocoder();
geocoder.getLocations(markerLatLng,function(response){

parseGeocodeAddress(response,name);
});
$("#answer"+question).val(Math.round(markerLatLng.lat()*10000)/10000 + " " + Math.round(markerLatLng.lng()*10000)/10000);

});
gmaps['marker__'+question] = marker;
GEvent.addListener(marker, "dragend", function() {
var markerLatLng = marker.getLatLng();
var geocoder = new GClientGeocoder();
geocoder.getLocations(markerLatLng,function(response){
parseGeocodeAddress(response,name);
});
$("#answer"+question).val(Math.round(markerLatLng.lat()*10000)/10000 + " " + Math.round(markerLatLng.lng()*10000)/10000);
});
map.addOverlay(marker);
}
return map;

var name = question.substr(0,question.length - 2);
var latlng = new google.maps.LatLng(lat, lng);

var mapOptions = {
zoom: zoom[name],
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};

var map = new google.maps.Map(document.getElementById("gmap_canvas_" + question), mapOptions);
gmaps[''+question] = map;

var marker = new google.maps.Marker({
position: latlng,
draggable:true,
map: map,
id: 'marker__'+question
});
gmaps['marker__'+question] = marker;

google.maps.event.addListener(map, 'rightclick', function(event) {
marker.setPosition(event.latLng);
map.panTo(event.latLng);
geocodeAddress(name, event.latLng);
$("#answer"+question).val(Math.round(event.latLng.lat()*10000)/10000 + " " + Math.round(event.latLng.lng()*10000)/10000);
});

google.maps.event.addListener(marker, 'dragend', function(event) {
//map.panTo(event.latLng);
geocodeAddress(name, event.latLng);
$("#answer"+question).val(Math.round(event.latLng.lat()*10000)/10000 + " " + Math.round(event.latLng.lng()*10000)/10000);
});
}

// Reset map when shown by conditions
function resetMap(qID) {
var question = $('#question'+qID+' input.location').attr('name');
var name = question.substr(0,question.length - 2);
var coordinates = $('#question'+qID+' input.location').attr('value');
var xy = coordinates.split(" ");
var currentMap = gmaps[question];
var marker = gmaps["marker__"+question];
var markerLatLng = new GLatLng(xy[0],xy[1]);
marker.setLatLng(markerLatLng);
var geocoder = new GClientGeocoder();
geocoder.getLocations(markerLatLng,function(response){
parseGeocodeAddress(response,name);
});
currentMap.checkResize();
var marker = gmaps['marker__'+question];
var markerLatLng = new google.maps.LatLng(xy[0],xy[1]);
marker.setPosition(markerLatLng);
google.maps.event.trigger(currentMap, 'resize')
currentMap.setCenter(markerLatLng);
}

function parseGeocodeAddress(response, name){
// Reverse geocoder
var geocoder = new google.maps.Geocoder();
function geocodeAddress(name, pos) {
var city = '';
var state = '';
var country = '';
var postal = '';

if (!(!response || response.Status.code != 200)) {
place = response.Placemark[0];
point = new GLatLng(place.Point.coordinates[1],
place.Point.coordinates[0]);
var lat = place.Point.coordinates[1];
var lng = place.Point.coordinates[0];



try{
city = place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.LocalityName;
}
catch(e){
city = '';
}

try{
state = place.AddressDetails.Country.AdministrativeArea.AdministrativeAreaName;
}
catch(e){
state = '';
}

try{
country = place.AddressDetails.Country.CountryNameCode;
}
catch(e){
country = '';
}

try{
postal = place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.PostalCode.PostalCodeNumber;
}
catch(e){
postal = '';
geocoder.geocode({
latLng: pos
}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK && results[0]) {
$(results[0].address_components).each(function(i, val) {
if($.inArray('locality', val.types) > -1) {
city = val.short_name;
}
else if($.inArray('administrative_area_level_1', val.types) > -1) {
state = val.short_name;
}
else if($.inArray('country', val.types) > -1) {
country = val.short_name;
}
else if($.inArray('postal_code', val.types) > -1) {
postal = val.short_name;
}
});

var location = (results[0].geometry.location);
}
getInfoToStore(name,lat,lng,city,state,country,postal);
}
else{
var latlong = (response.name).split(",");


getInfoToStore(name,latlong[0],latlong[1],city,state,country,postal);
}
getInfoToStore(name, pos.lat(), pos.lng(), city, state, country, postal);
});
}



// General Function
function getInfoToStore(name, lat,lng,city,state,country,postal){
var boycott = $("#boycott_"+name).val();
// Store address info
function getInfoToStore(name, lat, lng, city, state, country, postal){

var boycott = $("#boycott_"+name).val();
// 2 - city; 3 - state; 4 - country; 5 - postal
if (boycott.indexOf("2")!=-1)
city = '';
Expand All @@ -245,7 +230,6 @@ function getInfoToStore(name, lat,lng,city,state,country,postal){
postal = '';

$("#answer"+name).val(lat + ';' + lng + ';' + city + ';' + state + ';' + country + ';' + postal);

}

Array.prototype.push = function()
Expand Down

0 comments on commit ec4767d

Please sign in to comment.