Skip to content

Commit 4d99d29

Browse files
committed
More work on overlay map view
1 parent e2c2b51 commit 4d99d29

File tree

4 files changed

+61
-65
lines changed

4 files changed

+61
-65
lines changed

app/src/main/java/com/stackunderflow/stackptr/StackPtrUserList.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import com.squareup.picasso.Picasso;
2424
import com.stackunderflow.stackptrapi.StackPtrApiGetUsers;
2525
import com.stackunderflow.stackptrapi.StackPtrApiGetUsersParams;
26-
import com.stackunderflow.stackptroverlay.StackPtrCompassViewGroup;
26+
import com.stackunderflow.stackptroverlay.StackPtrMapViewGroup;
2727

2828
import org.json.JSONArray;
2929
import org.json.JSONException;
@@ -39,7 +39,7 @@ public class StackPtrUserList extends Activity {
3939
StackPtrApiGetUsersParams spagup;
4040
StackPtrListViewArrayAdaptor adapter;
4141
JSONArray jUsers;
42-
StackPtrCompassViewGroup spcvg;
42+
StackPtrMapViewGroup spcvg;
4343
Location lastloc;
4444

4545

@@ -59,7 +59,7 @@ protected void onCreate(Bundle savedInstanceState) {
5959
adapter = new StackPtrListViewArrayAdaptor(this);
6060
listview.setAdapter(adapter);
6161

62-
spcvg = new StackPtrCompassViewGroup(ctx);
62+
spcvg = new StackPtrMapViewGroup(ctx);
6363
spcvg.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 200));
6464

6565
listview.addHeaderView(spcvg);

app/src/main/java/com/stackunderflow/stackptrmap/StackPtrMapTileCalc.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,9 @@ public static String mapUrl(double xtile, double ytile, int zoom) {
2020
return String.format("https://tile1.stackcdn.com/osm_tiles_2x/%d/%d/%d.png", zoom, xtile_f, ytile_f);
2121
}
2222

23+
public static double pxCoord(double coord, int px) {
24+
double coordFrac = coord - Math.floor(coord);
25+
return coordFrac * px;
26+
}
27+
2328
}

app/src/main/java/com/stackunderflow/stackptroverlay/StackPtrCompassViewGroup.java renamed to app/src/main/java/com/stackunderflow/stackptroverlay/StackPtrMapViewGroup.java

Lines changed: 51 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import android.widget.ImageView;
1212

1313
import com.squareup.picasso.Picasso;
14+
import com.stackunderflow.stackptrmap.StackPtrMapTileCalc;
1415

1516
import org.json.JSONArray;
1617
import org.json.JSONException;
@@ -24,31 +25,46 @@ public class StackPtrCompassViewGroup extends ViewGroup {
2425
int width, height, centre_x, centre_y;
2526

2627
SparseArray<ImageView> views;
28+
SparseArray<JSONObject> users;
29+
ImageView bgTile;
30+
int fuser = 3;
31+
int zoom = 16;
2732

2833
public StackPtrCompassViewGroup(Context context) {
2934
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+
3241
}
3342

3443
public void updateDataAndRepaint(JSONArray jUsers, Location lastloc) {
3544
try {
3645

3746
Context context = this.getContext();
3847

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-
4848
for (int i = 0; i < jUsers.length(); i++) {
4949
JSONObject thisUser = jUsers.getJSONObject(i);
5050
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);
5268

5369
ImageView userView = views.get(user);
5470
if (userView == null) {
@@ -63,41 +79,39 @@ public void updateDataAndRepaint(JSONArray jUsers, Location lastloc) {
6379
JSONArray jLoc = thisUser.getJSONArray("loc");
6480
final double lat = jLoc.getDouble(0);
6581
final double lon = jLoc.getDouble(1);
66-
Location userLocation = new Location("StackPtr");
67-
userLocation.setLatitude(lat);
68-
userLocation.setLongitude(lon);
6982

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);
7585

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);
7888

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+
}
8395

84-
double xcoord = xvect * r;
85-
double ycoord = yvect * r;
96+
iconMove(userView, (int) xcoord, (int) ycoord, 96);
8697

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);*/
89101

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+
}*/
92107

93-
iconMove(userView, (int) xcoord, (int) ycoord, 64);
94108
}
95109

96110
// remove all the image views for users we didn't see again
97111
ArrayList<Integer> viewsToRemove = new ArrayList<Integer>();
98112
for (int i=0; i<views.size(); i++) {
99113
Integer viewId = views.keyAt(i);
100-
if (!presentIds.contains(viewId)) {
114+
if (users.get(viewId) == null) {
101115
viewsToRemove.add(viewId);
102116
}
103117
}
@@ -107,16 +121,16 @@ public void updateDataAndRepaint(JSONArray jUsers, Location lastloc) {
107121
views.remove(rmView);
108122
}
109123

124+
125+
110126
} catch (JSONException e) {
111127
System.out.print(e);
112128
}
113129
}
114130

115131
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;
118132
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);
120134
}
121135

122136

@@ -135,28 +149,5 @@ protected void onLayout(boolean changed, int left, int top, int right, int botto
135149
//System.out.printf("left %d, top %d, right %d, bottom %d, width %d, height %d\n", left, top, right, bottom, contentWidth, contentHeight);
136150
}
137151

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-
}
161152

162153
}

app/src/main/java/com/stackunderflow/stackptroverlay/StackPtrOverlay.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class StackPtrOverlay {
2121
private LocationManager lm;
2222
private StackPtrOverlayLocationListener ll;
2323
private WindowManager.LayoutParams wmp;
24-
private StackPtrCompassViewGroup vg;
24+
private StackPtrMapViewGroup vg;
2525
private StackPtrApiGetUsersParams spagup;
2626
private Boolean overlayShown;
2727
private Location lastloc;
@@ -36,7 +36,7 @@ public StackPtrOverlay(Context ctx) {
3636

3737
overlayShown = false;
3838

39-
vg = new StackPtrCompassViewGroup(ctx);
39+
vg = new StackPtrMapViewGroup(ctx);
4040

4141
wmp = new WindowManager.LayoutParams(
4242
512,

0 commit comments

Comments
 (0)