11
11
import android .widget .ImageView ;
12
12
13
13
import com .squareup .picasso .Picasso ;
14
+ import com .stackunderflow .stackptrmap .StackPtrMapTileCalc ;
14
15
15
16
import org .json .JSONArray ;
16
17
import org .json .JSONException ;
@@ -24,31 +25,46 @@ public class StackPtrCompassViewGroup extends ViewGroup {
24
25
int width , height , centre_x , centre_y ;
25
26
26
27
SparseArray <ImageView > views ;
28
+ SparseArray <JSONObject > users ;
29
+ ImageView bgTile ;
30
+ int fuser = 3 ;
31
+ int zoom = 16 ;
27
32
28
33
public StackPtrCompassViewGroup (Context context ) {
29
34
super (context );
30
- views = new SparseArray <ImageView >();
31
- init (null , 0 );
35
+ views = new SparseArray <>();
36
+ users = new SparseArray <>();
37
+ bgTile = new ImageView (context );
38
+ addView (bgTile );
39
+ bgTile .layout (0 ,0 ,512 ,512 );
40
+
32
41
}
33
42
34
43
public void updateDataAndRepaint (JSONArray jUsers , Location lastloc ) {
35
44
try {
36
45
37
46
Context context = this .getContext ();
38
47
39
- ImageView bgTile = new ImageView (context );
40
- Picasso .with (context ).load ("https://tile1.stackcdn.com/osm_tiles_2x/16/59159/40213.png" ).into (bgTile );
41
- addView (bgTile );
42
- bgTile .layout (0 ,0 ,512 ,512 );
43
-
44
-
45
- double half_width = width / 2.0 ;
46
- ArrayList <Integer > presentIds = new ArrayList <Integer >();
47
-
48
48
for (int i = 0 ; i < jUsers .length (); i ++) {
49
49
JSONObject thisUser = jUsers .getJSONObject (i );
50
50
Integer user = thisUser .getInt ("id" );
51
- presentIds .add (user );
51
+ users .append (user , thisUser );
52
+ }
53
+
54
+ JSONObject tracked_user = users .get (fuser );
55
+ JSONArray tu_loc = tracked_user .getJSONArray ("loc" );
56
+ final double tracked_user_lat = tu_loc .getDouble (0 );
57
+ final double tracked_user_lon = tu_loc .getDouble (1 );
58
+ double tracked_user_xtile = StackPtrMapTileCalc .xtileForLon (tracked_user_lon ,zoom );
59
+ double tracked_user_ytile = StackPtrMapTileCalc .ytileForLat (tracked_user_lat ,zoom );
60
+ Picasso .with (context ).load (
61
+ StackPtrMapTileCalc .mapUrl (tracked_user_xtile ,tracked_user_ytile ,zoom )
62
+ ).into (bgTile );
63
+
64
+
65
+ for (int i = 0 ; i < users .size (); i ++) {
66
+ int user = users .keyAt (i );
67
+ JSONObject thisUser = users .valueAt (i );
52
68
53
69
ImageView userView = views .get (user );
54
70
if (userView == null ) {
@@ -63,41 +79,39 @@ public void updateDataAndRepaint(JSONArray jUsers, Location lastloc) {
63
79
JSONArray jLoc = thisUser .getJSONArray ("loc" );
64
80
final double lat = jLoc .getDouble (0 );
65
81
final double lon = jLoc .getDouble (1 );
66
- Location userLocation = new Location ("StackPtr" );
67
- userLocation .setLatitude (lat );
68
- userLocation .setLongitude (lon );
69
82
70
- float dist = lastloc .distanceTo (userLocation );
71
- float bearing = lastloc .bearingTo (userLocation );
72
- if (bearing < 0 ) {
73
- bearing += 360 ;
74
- }
83
+ double xtile = StackPtrMapTileCalc .xtileForLon (lon ,zoom );
84
+ double ytile = StackPtrMapTileCalc .ytileForLat (lat ,zoom );
75
85
76
- double xvect = Math . sin ( Math . toRadians ( bearing ) );
77
- double yvect = - Math . cos ( Math . toRadians ( bearing ) );
86
+ double xcoord = StackPtrMapTileCalc . pxCoord ( xtile , 512 );
87
+ double ycoord = StackPtrMapTileCalc . pxCoord ( ytile , 512 );
78
88
79
- //dist = 100;
80
- double r = Math .log10 (dist ) - 1.0 ;
81
- r = Math .max (r , 0.0 );
82
- r *= half_width / 3.0 ;
89
+ if ((Math .floor (xtile ) == Math .floor (tracked_user_xtile )) &&
90
+ (Math .floor (ytile ) == Math .floor (tracked_user_ytile ))) {
91
+ userView .setVisibility (VISIBLE );
92
+ } else {
93
+ userView .setVisibility (INVISIBLE );
94
+ }
83
95
84
- double xcoord = xvect * r ;
85
- double ycoord = yvect * r ;
96
+ iconMove (userView , (int ) xcoord , (int ) ycoord , 96 );
86
97
87
- xcoord = Math .max (-half_width + 16 , xcoord );
88
- xcoord = Math .min (half_width - 16 , xcoord );
98
+ /*Location userLocation = new Location("StackPtr");
99
+ userLocation.setLatitude(lat);
100
+ userLocation.setLongitude(lon);*/
89
101
90
- ycoord = Math .max (-half_width + 16 , ycoord );
91
- ycoord = Math .min (half_width - 16 , ycoord );
102
+ /*float dist = lastloc.distanceTo(userLocation);
103
+ float bearing = lastloc.bearingTo(userLocation);
104
+ if (bearing < 0) {
105
+ bearing += 360;
106
+ }*/
92
107
93
- iconMove (userView , (int ) xcoord , (int ) ycoord , 64 );
94
108
}
95
109
96
110
// remove all the image views for users we didn't see again
97
111
ArrayList <Integer > viewsToRemove = new ArrayList <Integer >();
98
112
for (int i =0 ; i <views .size (); i ++) {
99
113
Integer viewId = views .keyAt (i );
100
- if (! presentIds . contains (viewId )) {
114
+ if (users . get (viewId ) == null ) {
101
115
viewsToRemove .add (viewId );
102
116
}
103
117
}
@@ -107,16 +121,16 @@ public void updateDataAndRepaint(JSONArray jUsers, Location lastloc) {
107
121
views .remove (rmView );
108
122
}
109
123
124
+
125
+
110
126
} catch (JSONException e ) {
111
127
System .out .print (e );
112
128
}
113
129
}
114
130
115
131
private void iconMove (ImageView icon , int x , int y , int size ) {
116
- int centre_x = (getWidth () - getPaddingLeft () - getPaddingRight ())/2 ;
117
- int centre_y = (getHeight () - getPaddingTop () - getPaddingBottom ())/2 ;
118
132
int hsize = size / 2 ;
119
- icon .layout (centre_x + x - hsize , centre_y + y - hsize , centre_x + x + hsize , centre_y + y + hsize );
133
+ icon .layout (x - hsize , y - hsize , x + hsize , y + hsize );
120
134
}
121
135
122
136
@@ -135,28 +149,5 @@ protected void onLayout(boolean changed, int left, int top, int right, int botto
135
149
//System.out.printf("left %d, top %d, right %d, bottom %d, width %d, height %d\n", left, top, right, bottom, contentWidth, contentHeight);
136
150
}
137
151
138
- Paint greenLine ;
139
-
140
- private void init (AttributeSet attrs , int defStyle ) {
141
- setWillNotDraw (false );
142
- greenLine = new Paint ();
143
- greenLine .setColor (Color .GREEN );
144
- greenLine .setStyle (Paint .Style .STROKE );
145
- greenLine .setStrokeWidth (2.0f );
146
- }
147
-
148
- @ Override
149
- protected void onDraw (Canvas canvas ) {
150
- super .onDraw (canvas );
151
-
152
- for (int i =0 ; i <5 ; i ++) {
153
- canvas .drawCircle (centre_x , centre_y , (float ) ((i * width / 6.0 ) - 1.0 ), greenLine );
154
- }
155
-
156
- canvas .drawLine (0.0f , centre_y , width , centre_y , greenLine );
157
- canvas .drawLine (centre_x , 0 , centre_x , height , greenLine );
158
- canvas .drawLine (0 , 0 , width , height , greenLine );
159
- canvas .drawLine (width , 0 , 0 , height , greenLine );
160
- }
161
152
162
153
}
0 commit comments