Failed Conditions
Pull Request — master (#162)
by PoUpA
02:17
created

pokemon.maps.js ➔ initMap   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 100

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 100
rs 8.2857

1 Function

Rating   Name   Duplication   Size   Complexity  
B pokemon.maps.js ➔ ... ➔ $.getJSON(ꞌcore/json/variables.jsonꞌ) 0 96 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
/** global: google */
2
/** global: pokemon_id */
3
var map, heatmap;
4
5
function initMap() {
6
	
7
	$.getJSON( "core/json/variables.json", function( jsondata ) {
8
9
		var variables = jsondata;
10
11
		var lattitude = Number(variables['system']['map_center_lat']);
12
		var longitude = Number(variables['system']['map_center_long']);
13
		var zoom_level = Number(variables['system']['zoom_level']);
14
15
16
		map = new google.maps.Map(document.getElementById('map'), {
17
			center: {lat: lattitude, lng: longitude},
18
			zoom: zoom_level,
19
			zoomControl: true,
20
			scaleControl: false,
21
			scrollwheel: true,
22
			disableDoubleClickZoom: false,
23
		});
24
		
25
		map.set('styles',[
26
			{
27
				"featureType":"all",
28
				"elementType":"labels.text.fill",
29
				"stylers":[{"saturation":36},{"color":"#333333"},{"lightness":40}]
30
			},
31
			{
32
				"featureType":"all",
33
				"elementType":"labels.text.stroke",
34
				"stylers":[{"visibility":"on"},{"color":"#ffffff"},{"lightness":16}]
35
			},
36
			{
37
				"featureType":"all",
38
				"elementType":"labels.icon",
39
				"stylers":[{"visibility":"off"}]
40
			},
41
			{
42
				"featureType":"administrative",
43
				"elementType":"geometry.fill",
44
				"stylers":[{"color":"#fefefe"},{"lightness":20}]
45
			},
46
			{
47
				"featureType":"administrative",
48
				"elementType":"geometry.stroke",
49
				"stylers":[{"color":"#fefefe"},{"lightness":17},{"weight":1.2}]
50
			},
51
			{
52
				"featureType":"landscape",
53
				"elementType":"geometry",
54
				"stylers":[{"color":"#f5f5f5"},{"lightness":20}]
55
			},
56
			{
57
				"featureType":"poi",
58
				"elementType":"geometry",
59
				"stylers":[{"color":"#f5f5f5"},{"lightness":21}]
60
			},
61
			{
62
				"featureType":"poi.park",
63
				"elementType":"geometry",
64
				"stylers":[{"color":"#dedede"},{"lightness":21}]
65
			},
66
			{
67
				"featureType":"poi.park",
68
				"elementType":"geometry.fill",
69
				"stylers":[{"color":"#c2ffd7"}]
70
			},
71
			{
72
				"featureType":"road.highway",
73
				"elementType":"geometry.fill",
74
				"stylers":[{"color":"#ffffff"},{"lightness":17}]},
75
			{
76
				"featureType":"road.highway",
77
				"elementType":"geometry.stroke",
78
				"stylers":[{"color":"#ffffff"},{"lightness":29},{"weight":0.2}]},
79
			{
80
				"featureType":"road.arterial",
81
				"elementType":"geometry",
82
				"stylers":[{"color":"#ffffff"},{"lightness":18}]},
83
			{
84
				"featureType":"road.local",
85
				"elementType":"geometry",
86
				"stylers":[{"color":"#ffffff"},{"lightness":16}]},
87
			{
88
				"featureType":"transit",
89
				"elementType":"geometry",
90
				"stylers":[{"color":"#f2f2f2"},{"lightness":19}]},
91
			{
92
				"featureType":"water",
93
				"elementType":"geometry",
94
				"stylers":[{"color":"#e9e9e9"},{"lightness":17}]},
95
			{
96
				"featureType":"water",
97
				"elementType":"geometry.fill",
98
				"stylers":[{"color":"#b3d8f9"}]
99
			}
100
		]);
101
		initSlider();
102
		});
103
104
}
105
function initSlider(){
106
	$.ajax({
107
		'async': true,
108
		'type': "GET",
109
		'global': false,
110
		'dataType': 'json',
111
		'url': "core/process/aru.php",
112
		'data': {
113
			'request': "",
114
			'target': 'arrange_url',
115
			'method': 'method_target',
116
			'type' : 'pokemon_slider_init'
117
		}
118
	}).done(function(bounds){
119
		var selectorMax = new Date(bounds.max);
120
		var selectorMin = new Date(bounds.min);
121
		var maxMinus2Weeks = new Date(selectorMax.getTime() - 2 * 7 * 24 * 60 * 60 * 1000);
122
		if(selectorMin < maxMinus2Weeks){
123
			selectorMin = maxMinus2Weeks;
124
		}
125
		$("#timeSelector").dateRangeSlider({
126
			bounds:{
127
				min: new Date(bounds.min),
128
				max: new Date(bounds.max)
129
			},
130
			defaultValues:{
131
				min: selectorMin,
132
				max: selectorMax
133
			}
134
		});
135
		createHeatmap();
136
	});
137
	
138
}
139
function createHeatmap() {
140
	
141
	heatmap = new google.maps.visualization.HeatmapLayer({
142
		data: [],
143
		map: map
144
	});
145
146
	var gradient = [
147
		'rgba(0, 255, 255, 0)',
148
		'rgba(0, 255, 255, 1)',
149
		'rgba(0, 191, 255, 1)',
150
		'rgba(0, 127, 255, 1)',
151
		'rgba(0, 63, 255, 1)',
152
		'rgba(0, 0, 255, 1)',
153
		'rgba(0, 0, 223, 1)',
154
		'rgba(0, 0, 191, 1)',
155
		'rgba(0, 0, 159, 1)',
156
		'rgba(0, 0, 127, 1)',
157
		'rgba(63, 0, 91, 1)',
158
		'rgba(127, 0, 63, 1)',
159
		'rgba(191, 0, 31, 1)',
160
		'rgba(255, 0, 0, 1)'
161
	];
162
	heatmap.set('gradient', gradient);
163
	heatmap.setMap(map);
164
	updateHeatmap();
165
	$("#timeSelector").bind("valuesChanged",function(){updateHeatmap()});
166
}
167
168
function updateHeatmap() {
169
	var dateMin = $("#timeSelector").dateRangeSlider("min");
170
	var dateMax = $("#timeSelector").dateRangeSlider("max");
171
	
172
	$.ajax({
173
		'async': true,
174
		'type': "GET",
175
		'global': false,
176
		'dataType': 'json',
177
		'url': "core/process/aru.php",
178
		'data': {
179
			'request': "",
180
			'target': 'arrange_url',
181
			'method': 'method_target',
182
			'type' : 'pokemon_heatmap_points',
183
			'pokemon_id' : pokemon_id,
184
			'start' : Math.floor(dateMin.getTime()/1000),
185
			'end' : Math.floor(dateMax.getTime()/1000)
186
		}
187
	}).done(function(points){
188
		var googlePoints = [];
189
		for (var i = 0; i < points.length; i++) {
190
			googlePoints.push(new google.maps.LatLng(points[i].latitude,points[i].longitude))
191
		}
192
		var newPoints = new google.maps.MVCArray(googlePoints);
193
		heatmap.set('data', newPoints);
194
	});
195
}