0

I am following the Developer Guide Hello Views example to develope a MapView and draw path between those two geopoints. I got the two geopoints but I can't draw the path between them.

To solve this I followed the stackoverflow question

http://stackoverflow.com/questions/2176397

with 36 upvoted answer by modifying a little. But still I am unable to resolve the problem.

My code is as below

package com.softtrends.mapview;

import java.util.List;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

import android.graphics.drawable.Drawable;
import android.os.Bundle;

public class MapViewEx extends MapActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    MapView mapView = (MapView)findViewById(R.id.mapview);
    mapView.setBuiltInZoomControls(true);
    //mapView.setStreetView(true);

    List<Overlay> mapOverlays = mapView.getOverlays();
    Drawable drawable = this.getResources().getDrawable(R.drawable.icon);
    MapViewEx2 itemizedOverlay = new MapViewEx2(drawable); 

    GeoPoint point1 = new GeoPoint(19240000,-99120000);
    OverlayItem overlayitem1 = new OverlayItem(point1, "Hola, Mundo!", "I'm in Mexico City!");

    GeoPoint point2 = new GeoPoint((int)(1.352566007*1E6),(int)(103.78921587*1E6));
    OverlayItem overlayitem2 = new OverlayItem(point2, null, null);

    itemizedOverlay.addOverlay(overlayitem1);
    itemizedOverlay.addOverlay(overlayitem2);
    mapOverlays.add(itemizedOverlay);
}

@Override
protected boolean isRouteDisplayed() {
    return false;
}
}

The above one is my activity class. I have another Overlay class as given below

package com.softtrends.mapview;

import java.util.ArrayList;

import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
import com.google.android.maps.Projection;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.drawable.Drawable;

public class MapViewEx2 extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
Context mContext;
public MapViewEx2(Drawable defaultMarker) {
    super(boundCenterBottom(defaultMarker));
}

public MapViewEx2(Drawable defaultMarker, Context context){
    super(defaultMarker);
    mContext = context;
}

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(mContext);
    dialog.setTitle(item.getTitle());
    dialog.setMessage(item.getSnippet());
    dialog.show();
    return true;
}

public void draw(Canvas canvas, MapView map, boolean shadow){

    super.draw(canvas, map, shadow);
    Paint mPaint = new Paint();
    mPaint.setDither(true);
    mPaint.setColor(Color.RED);
    mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
    mPaint.setStrokeJoin(Paint.Join.ROUND);
    mPaint.setStrokeCap(Paint.Cap.ROUND);
    mPaint.setStrokeWidth(2);

    Point p1 = new Point();
    Point p2 = new Point();
    Path path = new Path();
    Projection projection = map.getProjection();
    projection.toPixels(mOverlays.get(0).getPoint(), p1);
    projection.toPixels(mOverlays.get(1).getPoint(), p2);
    path.moveTo(p2.x, p2.y);
    path.moveTo(p1.x, p1.y);
    canvas.drawPath(path, mPaint);
}
}

Someone please say me where I did the mistake. If possible please explain when the draw() method of my second class is called?

Simon Sarris
  • 58,131
  • 13
  • 128
  • 161
Chandra Sekhar
  • 16,511
  • 14
  • 71
  • 109

1 Answers1

0

Please follow this tutorial it will help you.

http://eagle.phys.utk.edu/guidry/android/mapOverlayDemo.html

This is also a way.

public void draw(Canvas canvas, MapView mapv, boolean shadow)
         {
             super.draw(canvas, mapv, shadow);

             Paint mPaint = new Paint();
             mPaint.setDither(true);
             mPaint.setColor(Color.RED);
             mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
             mPaint.setStrokeJoin(Paint.Join.ROUND);
             mPaint.setStrokeCap(Paint.Cap.ROUND);
             mPaint.setStrokeWidth(2);


         GeoPoint gP1 = new GeoPoint(19240000,-99120000);
             GeoPoint gP2 = new GeoPoint(37423157, -122085008);


             Point p1 = new Point();
             Point p2 = new Point();

             Path path = new Path();

             Projection projection = mapv.getProjection();
             projection.toPixels(gP1, p1);
             projection.toPixels(gP2, p2);

             path.moveTo(p2.x, p2.y);
             path.lineTo(p1.x,p1.y);

             canvas.drawPath(path, mPaint);
         }

     }
Newts
  • 1,346
  • 13
  • 23