-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathAtmMap.java
237 lines (192 loc) · 8.31 KB
/
AtmMap.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package com.genschefieste;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
import java.util.ArrayList;
import java.util.List;
/**
* ATM location on a map.
*
* @author Jeppe Knockaert, Leen De Baets, Nicolas Dierck, Benjamin Mestdagh
* (c) 2013, OKFN. All rights reserved.
*/
public class AtmMap extends MapActivity {
List<Atm> atms;
MapView mapView;
MapController mc;
public LinearLayout _bubbleLayout;
public int position;
int minLatitude = Integer.MAX_VALUE;
int maxLatitude = Integer.MIN_VALUE;
int minLongitude = Integer.MAX_VALUE;
int maxLongitude = Integer.MIN_VALUE;
public double latitude;
public double longitude;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.atm_map);
Bundle extras = getIntent().getExtras();
latitude = extras.getDouble("latitude");
longitude = extras.getDouble("longitude");
atms = new ArrayList<Atm>();
String[] atmList = this.getResources().getStringArray(R.array.atms);
for (int i = 0; i < atmList.length ; i++) {
String[] parts = atmList[i].split(";");
Atm atm = new Atm();
atm.setName(parts[0]);
atm.setAddress(parts[1]);
atm.setLatitude(Float.parseFloat(parts[2]));
atm.setLongitude(Float.parseFloat(parts[3]));
atms.add(atm);
}
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
mapView.getOverlays().clear();
Drawable currentLocationIcon = this.getResources().getDrawable(R.drawable.menu_favorites);
MyItemizedOverlay itemizedOverlay = new MyItemizedOverlay(currentLocationIcon);
addOverlayItems(latitude, longitude, itemizedOverlay, 0);
mapView.getOverlays().add(itemizedOverlay);
// Also take current position in account for zooming factor.
maxLatitude = Math.max((int) (latitude * 1E6), maxLatitude);
minLatitude = Math.min((int) (latitude * 1E6), minLatitude);
maxLongitude = Math.max((int) (longitude * 1E6), maxLongitude);
minLongitude = Math.min((int) (longitude * 1E6), minLongitude);
int index = 0;
for (Atm a : atms) {
Drawable atmIcon = this.getResources().getDrawable(R.drawable.atm);
itemizedOverlay = new MyItemizedOverlay(atmIcon);
addOverlayItems(a.getLatitude(), a.getLongitude(), itemizedOverlay, index);
index++;
mapView.getOverlays().add(itemizedOverlay);
maxLatitude = Math.max((int) (a.getLatitude() * 1E6), maxLatitude);
minLatitude = Math.min((int) (a.getLatitude() * 1E6), minLatitude);
maxLongitude = Math.max((int) (a.getLongitude() * 1E6), maxLongitude);
minLongitude = Math.min((int) (a.getLongitude() * 1E6), minLongitude);
}
mc = mapView.getController();
double fitFactor = 1.5;
mc.zoomToSpan((int) (Math.abs(maxLatitude - minLatitude) * fitFactor), (int) (Math.abs(maxLongitude - minLongitude) * fitFactor));
mc.animateTo(new GeoPoint((maxLatitude + minLatitude) / 2, (maxLongitude + minLongitude) / 2));
// Listener on back button.
TextView BackToEvent = (TextView) findViewById(R.id.back_to_list);
BackToEvent.setOnClickListener(backToList);
}
/**
* OnClickListener on "Back" button.
*/
private final View.OnClickListener backToList = new View.OnClickListener() {
public void onClick(View v) {
finish();
}
};
@Override
public void onResume() {
super.onResume();
}
@Override
public void onPause() {
super.onPause();
}
protected boolean isRouteDisplayed() {
return false;
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_S) {
mapView.setSatellite(!mapView.isSatellite());
return (true);
}
else if (keyCode == KeyEvent.KEYCODE_Z) {
mapView.displayZoomControls(true);
return (true);
}
return (super.onKeyDown(keyCode, event));
}
private void addOverlayItems(double lat, double lng, MyItemizedOverlay itemizedOverlay, int index) {
GeoPoint point = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
OverlayItem overlayItem = new OverlayItem(point, "", Integer.toString(index));
itemizedOverlay.addOverlayItem(overlayItem);
}
View.OnClickListener detailOnclick = new View.OnClickListener() {
public void onClick(View v) {
Atm atm = atms.get(position);
String mapUrl = "http://maps.google.com/maps?saddr=" + latitude + "," + longitude + "&daddr=" + atm.getLatitude() + "," + atm.getLongitude();
Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(mapUrl));
startActivity(intent);
}
};
View.OnClickListener closeBubble = new View.OnClickListener() {
public void onClick(View v) {
mapView.removeView(_bubbleLayout);
}
};
private class MyItemizedOverlay extends ItemizedOverlay<OverlayItem> {
private List<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
public MyItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
}
@Override
protected OverlayItem createItem(int i) {
return mOverlays.get(i);
}
@Override
public int size() {
return mOverlays.size();
}
public void addOverlayItem(OverlayItem overlayItem) {
mOverlays.add(overlayItem);
populate();
}
@Override
public boolean onTap(int index) {
OverlayItem item = mOverlays.get(index);
position = Integer.parseInt(item.getSnippet());
Atm atm = atms.get(position);
// If a bubble is currently displayed then clear it.
if (_bubbleLayout != null) {
mapView.removeView(_bubbleLayout);
}
// Get instance of the Bubble Layout.
LayoutInflater inflater = (LayoutInflater) mapView.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
_bubbleLayout = (LinearLayout) inflater.inflate(R.layout.bubble, mapView, false);
// Configure its layout parameters
double latD = atm.getLatitude();
double lngD = atm.getLongitude();
int lat = (int) (latD * 1E6);
int lng = (int) (lngD * 1E6);
GeoPoint p = new GeoPoint(lat, lng);
MapView.LayoutParams params = new MapView.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, p, MapView.LayoutParams.BOTTOM_CENTER);
_bubbleLayout.setLayoutParams(params);
// Add title of atm.
TextView name = (TextView) _bubbleLayout.findViewById(R.id.atm_title);
name.setText(atm.getName());
// Add title of atm.
TextView address = (TextView) _bubbleLayout.findViewById(R.id.atm_address);
address.setText(atm.getAddress());
// Link.
TextView directions = (TextView) _bubbleLayout.findViewById(R.id.directions);
directions.setText(R.string.info_directions);
directions.setOnClickListener(detailOnclick);
// Close button.
TextView closeButton = (TextView) _bubbleLayout.findViewById(R.id.close_bubble);
closeButton.setOnClickListener(closeBubble);
// Add the view to the Map.
mapView.addView(_bubbleLayout);
// Animate the map to center on the location.
mapView.getController().animateTo(p);
return true;
}
}
}