Skip to content

Commit

Permalink
Map Overlay items optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
pierrelevy committed Apr 27, 2012
1 parent 2270e43 commit 48d771d
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 71 deletions.
Binary file added releases/franceaoc-1.0.0-beta6.apk
Binary file not shown.
2 changes: 1 addition & 1 deletion res/values-fr/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@
<string name="label_list_aoc">Liste des AOC de la commune : </string>
<string name="message_activate_location">Les services de géolocalisation (GPS ou réseau) semblent désactivés !\n\nVoulez-vous en activer un pour utiliser cette application ?</string>

<string name="loading_map_poi">Chargement des %1$d communes les plus proches du centre de la carte ...</string>
<string name="message_too_many_markers">Zoom trop large. Trop de communes à afficher!\nSeules les %1$d communes les plus proches du centre de la carte sont affichées.</string>

</resources>
2 changes: 1 addition & 1 deletion res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@
<string name="label_list_aoc">List of the AOC\'s town : </string>
<string name="message_activate_location">Location services (GPS and network) seem to be disabled!\n\nDo you want to enable one of them to use this application ?</string>

<string name="loading_map_poi">Loading of %1$d towns closest to the center of the map ...</string>
<string name="message_too_many_markers">Zoom too large. Too many markers!\nOnly the %1$d towns closest to the center of the map are displayed ...</string>
</resources>
2 changes: 1 addition & 1 deletion src/com/franceaoc/app/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class Constants
public static final boolean DEMO = false;

public static final int MAX_POI_AR = 20;
public static final int MAX_POI_MAP = 300;
public static final int MAX_POI_MAP = 500;
public static final int MAX_POI_LIST = 200;
public static final String EXTRA_POINT_LAT = "lat";
public static final String EXTRA_POINT_LON = "lon";
Expand Down
21 changes: 18 additions & 3 deletions src/com/franceaoc/app/map/CommuneOverlay.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,18 @@
*/
package com.franceaoc.app.map;

import android.content.Context;
import android.graphics.drawable.Drawable;
import com.franceaoc.app.Constants;
import com.franceaoc.app.model.Commune;
import com.franceaoc.app.service.CommuneService;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
import com.readystatesoftware.mapviewballoons.BalloonItemizedOverlay;
import com.readystatesoftware.mapviewballoons.BalloonOverlayResources;
import java.util.ArrayList;
import java.util.List;

/**
*
Expand Down Expand Up @@ -47,12 +53,21 @@ public CommuneOverlay(Drawable defaultMarker, MapView mapView, POIOverlayResourc
populate();
}

public void addOverlay(OverlayItem overlay)
public int updateOverlay( Context context , GeoPoint mapCenter , long radius )
{
mOverlays.add(overlay);
mOverlays.clear();
List<Commune> listCommunes = CommuneService.instance().getNearestCommunes( context , mapCenter, Constants.MAX_POI_MAP , radius );
for (Commune commune : listCommunes )
{
GeoPoint point = new GeoPoint((int) (commune.getLatitude() * 1E6), (int) (commune.getLongitude() * 1E6));
mOverlays.add(new CommuneOverlayItem(point, commune.getTitle(), commune.getDesciption(), commune.getId()));
}
setLastFocusedIndex(-1);
populate();
}
return listCommunes.size();

}

@Override
protected OverlayItem createItem(int i)
{
Expand Down
11 changes: 6 additions & 5 deletions src/com/franceaoc/app/service/CommuneService.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public Commune get(String insee)
return mRegistry.get(insee);
}

private Collection<Commune> getCommunesList()
public Collection<Commune> getCommunesList()
{
Collection<Commune> list;
synchronized (mRegistry)
Expand All @@ -66,7 +66,7 @@ private Collection<Commune> getCommunesList()
return list;
}

private synchronized List<Commune> getNearestPOI(double latitude, double longitude, int max, long radius)
private synchronized List<Commune> getNearestPOI(double latitude, double longitude, int max, long radius )
{
Collection<Commune> list = getCommunesList();
Log.i(Constants.TAG, "getNearestPOI - begin");
Expand Down Expand Up @@ -112,20 +112,21 @@ public List<Commune> getNearestCommunes(Context context, int max)
lon = location.getLongitude();
}

return getNearestPOI(lat, lon, max, 1000000);
return getNearestPOI(lat, lon, max, 1000000 );

}

public List<Commune> getNearestCommunes(Context context, GeoPoint point, int max)
public List<Commune> getNearestCommunes(Context context, GeoPoint point, int max , long radius )
{
double lat = ((double) point.getLatitudeE6()) / 1E6;
double lon = ((double) point.getLongitudeE6()) / 1E6;

return getNearestPOI( lat, lon, max, 1000000);
return getNearestPOI( lat, lon, max, radius );
}

public int getCount()
{
return mRegistry.size();
}

}
104 changes: 44 additions & 60 deletions src/com/franceaoc/app/ui/activity/AOCMapActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
*/
package com.franceaoc.app.ui.activity;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
Expand All @@ -25,14 +24,12 @@
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;
import com.franceaoc.app.Constants;
import com.franceaoc.app.R;
import com.franceaoc.app.map.CommuneOverlayItem;
import com.franceaoc.app.map.CommuneOverlay;
import com.franceaoc.app.map.POIMapView;
import com.franceaoc.app.map.POIOverlayResources;
import com.franceaoc.app.model.Commune;
import com.franceaoc.app.service.CommuneService;
import com.google.android.maps.*;
import java.util.List;

Expand All @@ -46,17 +43,14 @@ public class AOCMapActivity extends MapActivity implements CommuneOverlay.OnTapL
private static final int ZOOM_GLOBAL = 10;
private static final int ZOOM_FOCUSED = 13;
private static final boolean MODE_SATELLITE = false;
private static final int DISMISS = 0;
private static final int SHOW = 1;
private static final int UPDATE = 1;
private POIMapView mMapView;
private MapController mMapController;
private LocationManager mLocationManager;
private GeoPoint mMapCenter;
private List<Overlay> mMapOverlays;
private CommuneOverlay mItemizedOverlay;
private int mZoom;
private static ProgressDialog mDialogProgress;
private static boolean mProgress;
private Handler mHandler;

@Override
Expand Down Expand Up @@ -104,48 +98,57 @@ else if (Constants.DEMO)
mMapView.setOnPanChangeListener(this);

mMapController.animateTo(mMapCenter);
mHandler = new UIHandler();

setPOIOverlay();
initOverlay();

mMapView.invalidate();

mHandler = new UIHandler();


}

private void showProgress()
public void onTap(String ci)
{
Message msg = new Message();
msg.what = SHOW;
mHandler.sendMessage(msg);
startCommuneActivity(ci);
}

public void onPanChange(GeoPoint newCenter, GeoPoint oldCenter)
{

mMapCenter = newCenter;

if (distance(newCenter, oldCenter) > 3000.0)
{
updateUI();
Log.i(Constants.TAG, "Rebuild overlay");
}
}

private void hideProgress()
private void updateUI()
{
Message msg = new Message();
msg.what = DISMISS;
msg.what = UPDATE;
mHandler.sendMessage(msg);
}

private long calculateRadius()
{
GeoPoint center = mMapView.getMapCenter();
GeoPoint a = new GeoPoint( center.getLatitudeE6() + ( mMapView.getLatitudeSpan() / 2 ), center.getLongitudeE6() + ( mMapView.getLongitudeSpan() / 2 ));
GeoPoint b = new GeoPoint( center.getLatitudeE6() - ( mMapView.getLatitudeSpan() / 2 ), center.getLongitudeE6() - ( mMapView.getLongitudeSpan() / 2 ));
return (long) ( distance( a , b ) / 2 );
}

class UIHandler extends Handler
{

@Override
public void handleMessage(Message msg)
{
switch (msg.what)
if( msg.what == UPDATE )
{
case SHOW:
String message = String.format( getString( R.string.loading_map_poi ) , Constants.MAX_POI_MAP );
mDialogProgress = ProgressDialog.show(AOCMapActivity.this, "", message , true);
mProgress = true;
break;

case DISMISS:
mDialogProgress.dismiss();
mProgress = false;
break;
updateOverlay();
}
}
}
Expand Down Expand Up @@ -180,8 +183,7 @@ private GeoPoint convertLatLon(double latitude, double longitude)
return new GeoPoint(lat, lng);

}

private void setPOIOverlay()
private void initOverlay()
{
mMapOverlays = mMapView.getOverlays();
Drawable marker = getResources().getDrawable(R.drawable.wine_marker);
Expand All @@ -196,26 +198,22 @@ private void setPOIOverlay()
res.setMainLayoutId(R.id.balloon_main_layout);

mItemizedOverlay = new CommuneOverlay(marker, mMapView, res, this);

for (Commune commune : CommuneService.instance().getNearestCommunes(this, mMapCenter, Constants.MAX_POI_MAP))
{
GeoPoint point = new GeoPoint((int) (commune.getLatitude() * 1E6), (int) (commune.getLongitude() * 1E6));
mItemizedOverlay.addOverlay(new CommuneOverlayItem(point, commune.getTitle(), commune.getDesciption(), commune.getId()));
}

mMapOverlays.clear();
mMapOverlays.add(mItemizedOverlay);

mMapView.postInvalidate();
if (mProgress)
{
hideProgress();
}
updateOverlay();

}

public void onTap(String ci)
private void updateOverlay()
{
startCommuneActivity(ci);
long radius = calculateRadius();
int inRadius = mItemizedOverlay.updateOverlay(this, mMapCenter , radius );
if( inRadius == Constants.MAX_POI_MAP )
{
Log.d(Constants.TAG, "Radius = " + radius +" In radius : " + inRadius );
String msg = String.format( getString(R.string.message_too_many_markers ) , Constants.MAX_POI_MAP );
Toast.makeText(this, msg , Toast.LENGTH_LONG).show();
}
mMapView.postInvalidate();
}

private void startCommuneActivity(String ci)
Expand All @@ -226,20 +224,6 @@ private void startCommuneActivity(String ci)
startActivity(intent);
}

public void onPanChange(GeoPoint newCenter, GeoPoint oldCenter)
{

mMapCenter = newCenter;

if (distance(newCenter, oldCenter) > 3000.0)
{
showProgress();
setPOIOverlay();
Log.i(Constants.TAG, "Rebuild overlay");
}

}

private float distance(GeoPoint p1, GeoPoint p2)
{
float[] results = new float[3];
Expand Down

0 comments on commit 48d771d

Please sign in to comment.