Android Map low performance for ItemizedOverlay<OverlayItem> - android

I have
public class MyItemsOverlay extends ItemizedOverlay<OverlayItem>{
private List<OverlayItem> items=new ArrayList<OverlayItem>();
private Drawable marker=null;
private StatusData data;
private MapView mapView;
private PopupPanel panel;
public MyItemsOverlay (Drawable defaultMarker, View view, LayoutInflater getLayoutInflater) {
super(defaultMarker);
this.marker=defaultMarker;
mapView=(MapView) view.findViewById(R.id.mapview);
panel=new PopupPanel(R.layout.details_popup,getLayoutInflater, mapView);
data=new StatusData (mapView.getContext());
items= protectedZoneData.GetItems();
populate();
}
#Override
protected OverlayItem createItem(int i) {
return(items.get(i));
}
#Override
public int size() {
return(items.size());
}
#Override
protected boolean onTap(int i) {
OverlayItem item=getItem(i);
GeoPoint geo=item.getPoint();
View view=panel.getView();
((TextView)view.findViewById(R.id.latitude))
.setText(String.valueOf(geo.getLatitudeE6()/1000000.0));
((TextView)view.findViewById(R.id.longitude))
.setText(String.valueOf(geo.getLongitudeE6()/1000000.0));
((TextView)view.findViewById(R.id.Name))
.setText(item.getTitle());
((TextView)view.findViewById(R.id.Description))
.setText(item.getSnippet());
panel.show();
hidePopup();
return(true);
}
}
I need to load about 100 000 points.
And I have another overlay that will create polygon (100 000 polygons)
public class MyPolygonOverlay extends Overlay{
private StatusData data;
private Projection projection;
ArrayList<ArrayList<GeoPoint>> geArrayList;
public MyPolygonOverlay(Context context, MapView mapView)
{
data=new StatusData(context);
projection=mapView.getProjection();
geArrayList=data.GetPoyigonGeoPoints();
}
public void draw(Canvas canvas, MapView mapView, boolean shadow){
super.draw(canvas, mapView, shadow);
for (int i = 0; i < geArrayList.size(); i++) {
Path path = new Path();
ArrayList<GeoPoint> geoPoints=geArrayList.get(i);
for (int j = 0; j <geoPoints.size(); j++) {
GeoPoint gP1 =geoPoints.get(j);
Point currentScreenPoint = new Point();
Projection projection = mapView.getProjection();
projection.toPixels(gP1, currentScreenPoint);
if (j == 0){
path.moveTo(currentScreenPoint.x, currentScreenPoint.y);
}
else
{
path.lineTo(currentScreenPoint.x, currentScreenPoint.y);
}
}
canvas.drawPath(path, GetPaint());
}
}
On zoom level 5 I load first overlay, then on zoom 8 I load second overlay.
How can I speed my application?

Draw the Overlay on one bitmap and use that bitmap instead the redraw of each polygon.
You just need to calculate the scaling of your bitmap according your zoom level.

Related

Android Google Map: How to Draw pin which have text like (1,2,3,4,5..etc)?

I need to show the text inside pins which I draw on map how can I implement this in android any idea please provide me any suggestion for this. For drawing the pin I am using MyItemized overlay class. below is the code for my class..
public class MyItemizedOverlay extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
public MyItemizedOverlay(Drawable defaultMarker, Context ctx) {
super(boundCenterBottom(defaultMarker));
}
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}
public void clear() {
mOverlays.clear();
populate();
}
#Override
protected OverlayItem createItem(int i) {
return mOverlays.get(i);
}
#Override
public int size() {
return mOverlays.size();
}
#Override
protected boolean onTap(int index) {
return true;
}
#Override
public boolean onTouchEvent(MotionEvent event, MapView mapView) {
return false;
}
#Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
if (!shadow) {
super.draw(canvas, mapView, false);
}
}
}
Thanks..
do something like this and adjust position and size of text as per your convenience
public void draw(Canvas canvas, MapView mapView, boolean shadow)
{
super.draw(canvas, mapView, shadow);
int n = size();
Projection proj = mapView.getProjection();
Point out;
Paint paint = new Paint();
paint.setTextSize(20);
paint.setColor(Color.BLACK);
for (int i = 0; i < n; i++)
{
GeoPoint geoPoint = mOverlays.get(i).getPoint();
out = new Point();
proj.toPixels(geoPoint, out);
canvas.drawText("" + i, out.x, out.y, paint);
}
}
You need to supply I different drawable for each 'ItemizedOverlay'.
Either that or override that 'ItemizedOverlay's' drawing method and draw the number over a drawable?
For the dynamic drawing of the itemised overlay follow this tutorial. Even though its a balloon callout, you can customise it so it suits you.

Marker not Getting displayed on Map

I am using the following code to display a marker on the current location of the user. But the marker never shows up, the code shows the correct current location.
mapview.setClickable(true);
mapview.setBuiltInZoomControls(true);
mapview.setSatellite(false);
mapControl = mapview.getController();
mapControl.setZoom(12);
myLocationOverlay = new MyLocationOverlay(this, mapview);
mapview.getOverlays().add(myLocationOverlay);
LocationResult locationResult = new LocationResult() {
#Override
public void gotLocation(Location location) {
List<Overlay> overlays = mapview.getOverlays();
overlays.clear();
latE6 = (int) (location.getLatitude() * 1e6);
lonE6 = (int) (location.getLongitude() * 1e6);
String a = String.valueOf(latE6);
String b = String.valueOf(lonE6);
gp = new GeoPoint(latE6, lonE6);
mapControl.setZoom(12);
mapControl.animateTo(gp);
MapOverlay mapOverlay = new MapOverlay(getResources()
.getDrawable(R.drawable.pingreen));
OverlayItem overlayItem = new OverlayItem(new GeoPoint(latE6,
lonE6), a, b);
mapOverlay.addOverlay(overlayItem);
overlays.add(mapOverlay);
mapview.invalidate();
}
};
MyLocation myLocation = new MyLocation();
myLocation.getLocation(this, locationResult);
}
#Override
protected boolean isRouteDisplayed() {
return false;
}
#Override
protected void onResume() {
myLocationOverlay.enableCompass();
myLocationOverlay.enableMyLocation();
super.onResume();
}
#Override
protected void onPause() {
myLocationOverlay.disableCompass();
myLocationOverlay.disableMyLocation();
super.onPause();
}
public class MapOverlay extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
public MapOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
}
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}
#Override
protected OverlayItem createItem(int i) {
return mOverlays.get(i);
}
#Override
public int size() {
return mOverlays.size();
}
#Override
protected boolean onTap(int index) {
OverlayItem item = mOverlays.get(index);
AlertDialog.Builder dialog = new AlertDialog.Builder(
PickUpLocation.this);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.show();
return true;
}
#Override
public boolean onTouchEvent(MotionEvent event, MapView mapView) {
if (event.getAction() == 1) {
GeoPoint p = mapView.getProjection().fromPixels(
(int) event.getX(), (int) event.getY());
Toast.makeText(
getBaseContext(),
p.getLatitudeE6() / 1E6 + "," + p.getLongitudeE6()
/ 1E6, Toast.LENGTH_SHORT).show();
mapView.getOverlays().add(new MarkerOverlay(p));
mapView.invalidate();
}
return false;
}
}
class MarkerOverlay extends Overlay {
private GeoPoint p;
public MarkerOverlay(GeoPoint p) {
this.p = p;
}
#Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
long when) {
super.draw(canvas, mapView, shadow);
// ---translate the GeoPoint to screen pixels---
Point screenPts = new Point();
mapView.getProjection().toPixels(p, screenPts);
// ---add the marker---
Bitmap bmp = BitmapFactory.decodeResource(getResources(),
R.drawable.pingreen);
canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 50, null);
return true;
}
}
Please help me out on this.
Thanks in advance
On the emulator you can send a mock location using DDMS. Otherwise, use a real device.

drawing circle around my location

I am trying to draw a circle around my position. I am not sure what i am doing wrong, but the circle s not showing:
#Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
Projection projection = mapView.getProjection();
if (shadow == false && location != null) {
// Get the current location
Double latitude = location.getLatitude() * 1E6;
Double longitude = location.getLongitude() * 1E6;
GeoPoint geoPoint = new GeoPoint(latitude.intValue(),
longitude.intValue());
int radius = metersToRadius(100, mapView, latitude);
// Convert the location to screen pixels
Point point = new Point();
projection.toPixels(geoPoint, point);
// Setup the paint
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setStrokeWidth(2.0f);
paint.setColor(0xff6666ff);
paint.setStyle(Style.STROKE);
canvas.drawCircle(point.x, point.y, radius, paint);
paint.setColor(0x186666ff);
paint.setStyle(Style.FILL);
canvas.drawCircle(point.x, point.y, radius, paint);
}
super.draw(canvas, mapView, shadow);
}
Edit: Just to make it clear i am going to post my classes:
CustomItemizedOverlay
public class CustomItemizedOverlay extends ItemizedOverlay<OverlayItem> {
protected final List<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
protected final Context mContext;
public CustomItemizedOverlay(Drawable defaultMarker, Context context) {
super(boundCenterBottom(defaultMarker));
this.mContext = context;
}
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}
#Override
protected OverlayItem createItem(int i) {
// TODO Auto-generated method stub
return mOverlays.get(i);
}
public void removeOverlay(OverlayItem overlay) {
mOverlays.remove(overlay);
populate();
}
public void clear() {
mOverlays.clear();
populate();
}
#Override
public int size() {
// TODO Auto-generated method stub
return mOverlays.size();
}
#Override
protected boolean onTap(int i) {
OverlayItem itemClicked = this.mOverlays.get(i);
AlertDialog.Builder builder = new AlertDialog.Builder(this.mContext);
builder.setTitle(itemClicked.getTitle());
builder.setMessage(itemClicked.getSnippet());
builder.setCancelable(true);
AlertDialog alert = builder.create();
alert.show();
return true;
}
And PcCustomizedOverlay
public class PcCustomItemizedOverlay extends CustomItemizedOverlay {
public static int metersToRadius(float meters, MapView map, double latitude) {
return (int) (map.getProjection().metersToEquatorPixels(meters) * (1 / Math
.cos(Math.toRadians(latitude))));
}
private Location location;
public PcCustomItemizedOverlay(Drawable defaultMarker, Context context) {
super(defaultMarker, context);
}
#Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
Projection projection = mapView.getProjection();
if (shadow == false && location != null) {
// Get the current location
Double latitude = location.getLatitude() * 1E6;
Double longitude = location.getLongitude() * 1E6;
GeoPoint geoPoint = new GeoPoint(latitude.intValue(),
longitude.intValue());
int radius = metersToRadius(40, mapView, latitude);
// Convert the location to screen pixels
Point point = new Point();
projection.toPixels(geoPoint, point);
// Setup the paint
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setStrokeWidth(2.0f);
paint.setColor(0xff6666ff);
paint.setStyle(Style.STROKE);
canvas.drawCircle(point.x, point.y, radius, paint);
paint.setColor(0x186666ff);
paint.setStyle(Style.FILL);
canvas.drawCircle(point.x, point.y, radius, paint);
}
super.draw(canvas, mapView, shadow);
}
public Location getLocation() {
return location;
}
public void setLocation(Location location) {
this.location = location;
}
}
Does anyone know where is the problem?
Thank you very much
try this code
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mapView = (MapView) findViewById(R.id.mapview);
MapController mc = mapView.getController();
MyLocationOverlay myLocationOverlay = new MyLocationOverlay(MainMap.this, mapView);
mapView.getOverlays().add(myLocationOverlay);
mc.animateTo( new GeoPoint(lat, lng));
mc.setZoom(15);
mapView.invalidate();
}
Dont forget to add overlay.enableMyLocation(); in onresume() and overlay.disableMyLocation(); in on pause
Instead of the above code if you want to draw circle around you point you can use following sample code:
Point screenPnts =new Point();
GeoPoint curr_geopoint = new GeoPoint((int)(location.getLatitude()*1E6),(int)(location.getLongitude()*1E6));
mapview.getProjection().toPixels(curr_geopoint, screenPnts);
canvas.drawCircle(screenPnts.x, screenPnts.y, 15, paint);
do some trial & error to get that circle around the point by manipulating screenPnts.x and screenPnts.y values. here paint is the object of Paint class to give the color to the circle
I think problem in your void draw.
#Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
super.draw(canvas, mapView, shadow);
Projection projection = mapView.getProjection();
if (shadow == false && location != null) {
// Get the current location
Double latitude = location.getLatitude() * 1E6;
Double longitude = location.getLongitude() * 1E6;
GeoPoint geoPoint = new GeoPoint(latitude.intValue(),
longitude.intValue());
int radius = metersToRadius(100, mapView, latitude);
// Convert the location to screen pixels
Point point = new Point();
projection.toPixels(geoPoint, point);
// Setup the paint
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setStrokeWidth(2.0f);
paint.setColor(0xff6666ff);
paint.setStyle(Style.STROKE);
canvas.drawCircle(point.x, point.y, radius, paint);
paint.setColor(0x186666ff);
paint.setStyle(Style.FILL);
canvas.drawCircle(point.x, point.y, radius, paint);
}
}
Look here also for put image over your map.drawing image as mapoverlay
I've had similar problem.
Solved it with overriding boolean draw instead of void one in inner class that extended Overlay.
It would look like this:
//inner class MapOverlay
class MapOverlay extends com.google.android.maps.Overlay
{
#Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when)
{
super.draw(canvas, mapView, shadow);
Projection projection = mapView.getProjection();
//the rest of your code here................
super.draw(canvas,mapView,shadow);
return true;
}
}
Construct your circle with
MapOverlay mapOverlayCircle = new MapOverlay();
and add it to your Overlays in your mapView. that's it.

How to plot markers alone between two places? - android

I have plotted the line between two points(locations), but i dont know how to plot the markers on those points. Can anyone give me some ideas.....
public void drawPath(MapView mv, Canvas canvas)
{
int x1 = -1, y1 = -1, x2 = -1, y2 = -1;
Paint paint = new Paint();
paint.setColor(Color.GREEN);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(3);
for (int i = 0; i < mPoints.size(); i++)
{
Point point = new Point();
mv.getProjection().toPixels(mPoints.get(i), point);
x2 = point.x;
y2 = point.y;
if (i > 0)
{
canvas.drawLine(x1, y1, x2, y2, paint);
}
x1 = x2;
y1 = y2;
}
}
You can add overlays to the starting and ending point of your line.
Here's example.. Hope this will help you..
RouteActivity.java
public class RouteActivity extends MapActivity
{
private List<Overlay> mapOverlays;
private Projection projection;
MapView mapView;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Drawable drawable = this.getResources().getDrawable(R.drawable.pin); // Marker that you want to display..
mapOverlays = mapView.getOverlays();
List<Overlay> mapOverlays = mapView.getOverlays();
MyOverlay itemizedoverlay = new MyOverlay(drawable,this);
GeoPoint point = new GeoPoint(19240000,-99120000); // overlay 1
OverlayItem overlayitem = new OverlayItem(point, null, null);
GeoPoint point1 = new GeoPoint(44046665, 72559236); // overlay 2
OverlayItem overlayitem1 = new OverlayItem(point1, null, null);
itemizedoverlay.addOverlay(overlayitem);
itemizedoverlay.addOverlay(overlayitem1);
mapOverlays.add(itemizedoverlay);
}
#Override
protected boolean isRouteDisplayed() {
return false;
}
}
MyOverlay.java
public class MyOverlay extends ItemizedOverlay
{
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
Context mContext;
public MyOverlay(Drawable defaultMarker)
{
super(boundCenterBottom(defaultMarker));
}
public MyOverlay(Drawable defaultMarker, Context context) {
// super(defaultMarker);
super(boundCenterBottom(defaultMarker));
mContext = context;
}
public void draw(Canvas canvas, MapView mapv, boolean shadow)
{
super.draw(canvas, mapv, shadow);
// line drawing code goes here.....
canvas.drawPath(path, mPaint);
}
#Override
protected OverlayItem createItem(int i) {
// TODO Auto-generated method stub
return mOverlays.get(i);
}
#Override
public int size() {
// TODO Auto-generated method stub
return mOverlays.size();
}
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}
}
Thanks...

Connect points on map with lines

I have three gps points in android app. How to set on map connect first and second with red and second and third with blue line ? How to connect any two points on map, draw line between them?
Here's a minimal implementation (2 points only, no markers) using a map overlay and mapView.getProjection() fro you to expand upon:
public class HelloGoogleMaps extends MapActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MapController mMapController;
MapView mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
mMapController = mapView.getController();
mMapController.setZoom(18);
// Two points in Mexico about 1km apart
GeoPoint point1 = new GeoPoint(19240000,-99120000);
GeoPoint point2 = new GeoPoint(19241000,-99121000);
mMapController.setCenter(point2);
// Pass the geopoints to the overlay class
MapOverlay mapOvlay = new MapOverlay(point1, point2);
mapView.getOverlays().add(mapOvlay);
}
public class MapOverlay extends com.google.android.maps.Overlay {
private GeoPoint mGpt1;
private GeoPoint mGpt2;
protected MapOverlay(GeoPoint gp1, GeoPoint gp2 ) {
mGpt1 = gp1;
mGpt2 = gp2;
}
#Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
long when) {
super.draw(canvas, mapView, shadow);
Paint paint;
paint = new Paint();
paint.setColor(Color.RED);
paint.setAntiAlias(true);
paint.setStyle(Style.STROKE);
paint.setStrokeWidth(2);
Point pt1 = new Point();
Point pt2 = new Point();
Projection projection = mapView.getProjection();
projection.toPixels(mGpt1, pt1);
projection.toPixels(mGpt2, pt2);
canvas.drawLine(pt1.x, pt1.y, pt2.x, pt2.y, paint);
return true;
}
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
.

Categories

Resources