22
33import java .util .List ;
44import java .util .concurrent .atomic .AtomicReference ;
5+
6+ import android .R .bool ;
57import android .app .AlertDialog ;
68import android .content .Context ;
79import android .content .DialogInterface ;
810import android .content .Intent ;
11+ import android .content .SharedPreferences ;
912import android .graphics .drawable .Drawable ;
1013import android .location .Criteria ;
1114import android .location .Location ;
1215import android .location .LocationListener ;
1316import android .location .LocationManager ;
1417import android .os .Bundle ;
18+ import android .util .Log ;
19+ import android .view .Menu ;
20+ import android .view .MenuItem ;
1521import android .widget .Toast ;
1622
1723import com .google .android .maps .GeoPoint ;
@@ -39,8 +45,15 @@ public class AddPlaceMapActivity extends MapActivity {
3945 private Location currentLocation ;
4046 protected final AtomicReference <Place > destinationReference = new AtomicReference <Place >();
4147 protected Drawable destinationDrawable ;
48+ protected Drawable placeDrawable ;
4249
50+ private boolean DEBUG = true ;
51+
4352 private int REQUEST_ID = -1 ;
53+
54+ SharedPreferences prefs ;
55+ private double placeLat ;
56+ private double placeLon ;
4457
4558 /** Called when the activity is first created. */
4659 @ Override
@@ -49,33 +62,15 @@ public void onCreate(Bundle savedInstanceState) {
4962 setContentView (R .layout .placemap );
5063 Intent intent = getIntent ();
5164 REQUEST_ID = intent .getIntExtra (ContextConstants .PLACE_REQUEST_ID , -1 );
52-
53- // geopoint for overlayitem
54- // Double latitude = 37.423156 * 1E6;
55- // Double longuitude = -122.084917 * 1E6;
56- // GeoPoint point = new GeoPoint(latitude.intValue(), longuitude.intValue());
57-
58- // // create new overlayitem
59- // OverlayItem overlayitem = new OverlayItem(point, "Googleplex", "Google");
60- // overlay = new PlaceItemizedOverlay(getResources().getDrawable(R.drawable.location));
61- // overlay.addOverlay(overlayitem);
62-
63- // add overlay to map
64- map = (MapView ) findViewById (R .id .mapView );
65- List <Overlay > mapOverlays = map .getOverlays ();
66- // mapOverlays.add(overlay);
6765
66+ getPrefs ();
6867
69- mapOverlays .add (new PlaceOverlay (this ));
70-
71- // MyLocation manage your position and enable compass
72- MyLocationOverlay myLocationOverlay = new MyLocationOverlay (this , map );
73- mapOverlays .add (myLocationOverlay );
74- myLocationOverlay .enableMyLocation ();
68+ map = (MapView ) findViewById (R .id .mapView );
69+ createOverlay ();
7570
7671 // Mapcontroller set zoom and center
7772 mapController = map .getController ();
78- // mapController.setCenter(point);
73+ // mapController.setCenter(point);
7974 mapController .setZoom (16 );
8075 map .setBuiltInZoomControls (true );
8176
@@ -84,14 +79,13 @@ public void onCreate(Bundle savedInstanceState) {
8479
8580 // Set the criteria
8681 criteria = new Criteria ();
87- // other option Criteria.ACCURACY_COARSE
88- criteria .setAccuracy (Criteria .ACCURACY_FINE );
82+ // criteria.setAccuracy( Criteria.ACCURACY_FINE);
83+ criteria .setAccuracy (Criteria .ACCURACY_COARSE );
8984 criteria .setAltitudeRequired (false );
9085 criteria .setBearingRequired (false );
9186 criteria .setCostAllowed (true );
9287 // other options Criteria.POWER_HIGH Criteria.POWER_MEDIUM
9388 criteria .setPowerRequirement (Criteria .POWER_LOW );
94-
9589 }
9690
9791 @ Override
@@ -106,10 +100,7 @@ public void onStart() {
106100 Location location = locationManager .getLastKnownLocation (provider );
107101 update (location );
108102 // Start listening for location changes
109- locationManager .requestLocationUpdates (provider , 60000 , // every
110- // 1min
111- 1000 , // change locations min 1km
112- locationListener );
103+ locationManager .requestLocationUpdates (provider , 60000 , /* every 1min */ 1000 , /* min 1km */ locationListener );
113104 }
114105 }
115106
@@ -124,22 +115,98 @@ public void onStop() {
124115 public void onLocationChanged (Location location ) {
125116 update (location );
126117 }
127-
128118 public void onProviderDisabled (String provider ) {
129119 update (null );
130120 }
131-
132121 public void onProviderEnabled (String provider ) {
133- // TODO Auto-generated method stub
134-
135122 }
136-
137123 public void onStatusChanged (String provider , int status , Bundle extras ) {
138- // TODO Auto-generated method stub
139-
140124 }
141-
142125 };
126+
127+ public void createOverlay () {
128+ List <Overlay > mapOverlays = map .getOverlays ();
129+
130+ switch (REQUEST_ID ) {
131+ case ContextConstants .SET_HOME_REQUEST :
132+ if (DerivedMonitor .Home != null ) {
133+ OverlayItem overlayitem = new OverlayItem (DerivedMonitor .Home .getGeoPoint (), "Home" , "Home" );
134+ overlay = new PlaceItemizedOverlay (getResources ().getDrawable (R .drawable .home ));
135+ overlay .addOverlay (overlayitem );
136+ mapOverlays .add (overlay );
137+ }
138+ break ;
139+ case ContextConstants .SET_WORK_REQUEST :
140+ if (DerivedMonitor .Work != null ) {
141+ OverlayItem overlayitem = new OverlayItem (DerivedMonitor .Work .getGeoPoint (), "Work" , "Work" );
142+ overlay = new PlaceItemizedOverlay (getResources ().getDrawable (R .drawable .work ));
143+ overlay .addOverlay (overlayitem );
144+ mapOverlays .add (overlay );
145+ }
146+ break ;
147+ default :
148+ break ;
149+ }
150+
151+ mapOverlays .add (new PlaceOverlay (this ));
152+
153+ // MyLocation manage your position and enable compass
154+ MyLocationOverlay myLocationOverlay = new MyLocationOverlay (this , map );
155+ mapOverlays .add (myLocationOverlay );
156+ myLocationOverlay .enableMyLocation ();
157+ }
158+
159+
160+ private void getPrefs () {
161+ prefs = getSharedPreferences (ContextConstants .CONTEXT_PREFS , MODE_PRIVATE );
162+
163+ switch (REQUEST_ID ) {
164+ case ContextConstants .SET_HOME_REQUEST :
165+
166+ break ;
167+ case ContextConstants .SET_WORK_REQUEST :
168+ if (DerivedMonitor .Work != null ) {
169+
170+ }
171+ break ;
172+ default :
173+ break ;
174+ }
175+
176+ String placeString = prefs .getString (ContextConstants .HOME_COORDINATES , "" );
177+ if (DEBUG ) {
178+ // Log.d(TAG, "accuracyDismissDelay: " + accuracyDismissDelay + " dismissDelay: " + dismissDelay);
179+ }
180+ }
181+
182+
183+ // public void createOverlay(){
184+ // mapOverlays=mapView.getOverlays();
185+ // OverlayItem item;
186+ // drawable = this.getResources().getDrawable(R.drawable.icon_map);
187+ // MixOverlay mixOverlay = new MixOverlay(this, drawable);
188+ //
189+ // for(Marker marker:markerList) {
190+ // if(marker.isActive()) {
191+ // GeoPoint point = new GeoPoint((int)(marker.getLatitude()*1E6), (int)(marker.getLongitude()*1E6));
192+ // item = new OverlayItem(point, "", "");
193+ // mixOverlay.addOverlay(item);
194+ // }
195+ // }
196+ // //Solved issue 39: only one overlay with all marker instead of one overlay for each marker
197+ // mapOverlays.add(mixOverlay);
198+ //
199+ // MixOverlay myOverlay;
200+ // drawable = this.getResources().getDrawable(R.drawable.loc_icon);
201+ // myOverlay = new MixOverlay(this, drawable);
202+ //
203+ // item = new OverlayItem(startPoint, "Your Position", "");
204+ // myOverlay.addOverlay(item);
205+ // mapOverlays.add(myOverlay);
206+ // }
207+
208+
209+
143210
144211 private void update (Location location ) {
145212 if (location != null ) {
@@ -149,12 +216,12 @@ private void update(Location location) {
149216 Double lng = location .getLongitude () * 1E6 ;
150217 GeoPoint point = new GeoPoint (lat .intValue (), lng .intValue ());
151218 mapController .setCenter (point );
219+ mapController .setZoom (15 );
152220 Toast .makeText (this , "My Location:" + location .getLatitude () + "," + location .getLongitude (), Toast .LENGTH_LONG );
153- // get some data in other thread
154- // RefreshListview
155221 }
156- }
157-
222+ }
223+
224+
158225 @ Override
159226 protected boolean isRouteDisplayed () {
160227 // TODO Auto-generated method stub
@@ -164,42 +231,86 @@ protected boolean isRouteDisplayed() {
164231 public void placeSelected (Place place ) {
165232 destinationReference .set (place );
166233 final FloatingPointGeoPoint point = place .getLocation ();
167-
168- // List<Overlay> mapOverlays = map.getOverlays();
169- // mapOverlays.add(new PlaceOverlay(this));
170- // OverlayItem overlayitem = new OverlayItem(point.getGeoPoint(), "Home", "Home");
171- // overlay = new PlaceItemizedOverlay(getResources().getDrawable(R.drawable.home));
172- // overlay.addOverlay(overlayitem);
173-
174234
175- new AlertDialog .Builder (this ).setMessage ("Add Place?" ).setIcon (R .drawable .location )
176- .setPositiveButton ("Yes" , new DialogInterface .OnClickListener () {
177- public void onClick (DialogInterface dialog , int which ) {
178-
179- switch (REQUEST_ID ) {
180- case ContextConstants .SET_HOME_REQUEST :
181- DerivedMonitor .Home = new FloatingPointGeoPoint (point .getLatitude (), point .getLongitude ());
182- Toast .makeText (getApplicationContext (), "Home Updated\n Lat:" + point .getLatitude () +
183- "\n Lon:" + point .getLongitude ()+ "\n Press Back to Exit" , Toast .LENGTH_LONG ).show ();
184- break ;
185- case ContextConstants .SET_WORK_REQUEST :
186-
187- DerivedMonitor .Work = new FloatingPointGeoPoint (point .getLatitude (), point .getLongitude ());
188- Toast .makeText (getApplicationContext (), "Work Updated\n Lat:" + point .getLatitude () +
189- "\n Lon:" + point .getLongitude () + "\n Press Back to Exit" , Toast .LENGTH_LONG ).show ();
190- break ;
191- default :
192- break ;
193- }
194-
195- }
196- }).setNegativeButton ("No" , new DialogInterface .OnClickListener () {
197- public void onClick (DialogInterface dialog , int which ) {
198- // Do nothing, just let another destination be
199- // chosen.
200- }
201- }).show ();
235+ // List<Overlay> mapOverlays = map.getOverlays();
236+ // mapOverlays.add(new PlaceOverlay(this));
237+ // OverlayItem overlayitem = new OverlayItem(point.getGeoPoint(),
238+ // "Home", "Home");
239+ // overlay = new
240+ // PlaceItemizedOverlay(getResources().getDrawable(R.drawable.home));
241+ // overlay.addOverlay(overlayitem);
242+
243+ new AlertDialog .Builder (this ).setMessage ("Add Place?" ).setIcon (R .drawable .location ).setPositiveButton ("Yes" , new DialogInterface .OnClickListener () {
244+ public void onClick (DialogInterface dialog , int which ) {
245+
246+ switch (REQUEST_ID ) {
247+ case ContextConstants .SET_HOME_REQUEST :
248+ DerivedMonitor .Home = new FloatingPointGeoPoint (point .getLatitude (), point .getLongitude ());
249+ Toast .makeText (getApplicationContext (),
250+ "Home Updated\n Lat:" + point .getLatitude () + "\n Lon:" + point .getLongitude () + "\n Press Back to Exit" , Toast .LENGTH_LONG ).show ();
251+ break ;
252+ case ContextConstants .SET_WORK_REQUEST :
253+
254+ DerivedMonitor .Work = new FloatingPointGeoPoint (point .getLatitude (), point .getLongitude ());
255+ Toast .makeText (getApplicationContext (),
256+ "Work Updated\n Lat:" + point .getLatitude () + "\n Lon:" + point .getLongitude () + "\n Press Back to Exit" , Toast .LENGTH_LONG ).show ();
257+ break ;
258+ default :
259+ break ;
260+ }
261+
262+ map .invalidate ();
263+ createOverlay ();
264+
265+ }
266+ }).setNegativeButton ("No" , new DialogInterface .OnClickListener () {
267+ public void onClick (DialogInterface dialog , int which ) {
268+ // Do nothing, just let another destination be chosen.
269+ }
270+ }).show ();
271+ }
272+
273+ @ Override
274+ public boolean onCreateOptionsMenu (Menu menu ) {
275+ int base = Menu .FIRST ;
276+ /* define the first */
277+ MenuItem item1 = menu .add (base , base , base , "Map" );
278+ MenuItem item2 = menu .add (base , base + 1 , base + 1 , "Satellite" );
279+ MenuItem item3 = menu .add (base , base + 2 , base + 2 , "My Location" );
280+ MenuItem item4 = menu .add (base , base + 3 , base + 3 , "Return" );
281+
282+ /* assign icons to the menu items */
283+ item1 .setIcon (android .R .drawable .ic_menu_gallery );
284+ item2 .setIcon (android .R .drawable .ic_menu_mapmode );
285+ item3 .setIcon (android .R .drawable .ic_menu_mylocation );
286+ item4 .setIcon (android .R .drawable .ic_menu_close_clear_cancel );
287+
288+ return true ;
289+ }
290+
291+ @ Override
292+ public boolean onOptionsItemSelected (MenuItem item ) {
293+ switch (item .getItemId ()) {
294+ /* Satellite View */
295+ case 1 :
296+ map .setSatellite (false );
297+ break ;
298+ /* street View */
299+ case 2 :
300+ map .setSatellite (true );
301+ break ;
302+ /* go to users location */
303+ case 3 :
304+ if (currentLocation != null ) {
305+ update (currentLocation );
306+ }
307+ break ;
308+ /* List View */
309+ case 4 :
310+ finish ();
311+ break ;
312+ }
313+ return true ;
202314 }
203-
204315
205316}
0 commit comments