views:

2389

answers:

5

Hello,

I'v been busy for a long long long time finding out how to draw a line between two (gps) points on the map in HelloMapView but with no luck.

Could anyone please tell me how to do so.

Suppose I use the HelloMapView which extends MapView. Do I need to use overlays? If so do I have to override the onDraw() method of the overlay and draw a line here? I actually tried these things but with no result.

thank you in advance

+1  A: 

Yes you need to use overlays.

You need to get the MapView's overlays and add your new overlay onto it.

Your class extends Overlay, which is a transparent canvas in which you can draw on it like any other canvas.

You can use mapView.getProjection() to get projection of map view.

...

More info found here: http://blogoscoped.com/archive/2008-12-15-n14.html

ddcruver
+3  A: 

Thank you for your help. At last I could draw a line on the map. This is how I done it:

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    linearLayout = (LinearLayout) findViewById(R.id.zoomview);
    mapView = (MapView) findViewById(R.id.mapview);
    mapView.setBuiltInZoomControls(true);

    mapOverlays = mapView.getOverlays();        
    projection = mapView.getProjection();
    mapOverlays.add(new MyOverlay());        

}

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

class MyOverlay extends Overlay{

    public MyOverlay(){

    }   

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

        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 = new Path();

        projection.toPixels(gP1, p1);
        projection.toPixels(gP2, p2);

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

        canvas.drawPath(path, mPaint);
    }
mnish
A: 

Also I need that, but I have a problem . The object "projection" is initialized in another class and then you try to use in class MyOverlay . I get a undefined variable (because this is created in another class) .

Anyone to help ? :)

One solution is to define MyOverlay as an Inner class of the class where projection declared and initialized. Or pass projection as a parameter to MyOverlay object.
mnish
A: 

You can get the projection from the MapView object which is passed into the draw() method: mapv.getProjection().toPixels(gP1, p1);

John J Smith
A: 

// This Activity will draw a line between two selected points on Map

public class MainActivity extends MapActivity { MapView myMapView = null; MapController myMC = null; GeoPoint geoPoint = null;

/** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState); setContentView(R.layout.main); myMapView = (MapView) findViewById(R.id.mapview); geoPoint = null; myMapView.setSatellite(false);

String pairs[] = getDirectionData("ahmedabad", "vadodara"); String[] lngLat = pairs[0].split(",");

// STARTING POINT GeoPoint startGP = new GeoPoint( (int) (Double.parseDouble(lngLat[1]) * 1E6), (int) (Double .parseDouble(lngLat[0]) * 1E6));

myMC = myMapView.getController(); geoPoint = startGP; myMC.setCenter(geoPoint); myMC.setZoom(15); myMapView.getOverlays().add(new DirectionPathOverlay(startGP, startGP));

// NAVIGATE THE PATH

GeoPoint gp1; GeoPoint gp2 = startGP;

for (int i = 1; i < pairs.length; i++) { lngLat = pairs[i].split(","); gp1 = gp2; // watch out! For GeoPoint, first:latitude, second:longitude

gp2 = new GeoPoint((int) (Double.parseDouble(lngLat[1]) * 1E6), (int) (Double.parseDouble(lngLat[0]) * 1E6)); myMapView.getOverlays().add(new DirectionPathOverlay(gp1, gp2)); Log.d("xxx", "pair:" + pairs[i]); }

// END POINT myMapView.getOverlays().add(new DirectionPathOverlay(gp2, gp2));

myMapView.getController().animateTo(startGP); myMapView.setBuiltInZoomControls(true); myMapView.displayZoomControls(true);

}

@Override protected boolean isRouteDisplayed() { // TODO Auto-generated method stub return false; }

private String[] getDirectionData(String srcPlace, String destPlace) {

String urlString = "http://maps.google.com/maps?f=d&amp;hl=en&amp;saddr=" + srcPlace + "&daddr=" + destPlace + "&ie=UTF8&0&om=0&output=kml";

Log.d("URL", urlString); Document doc = null; HttpURLConnection urlConnection = null; URL url = null; String pathConent = "";

try {

url = new URL(urlString.toString()); urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setRequestMethod("GET"); urlConnection.setDoOutput(true); urlConnection.setDoInput(true); urlConnection.connect(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); doc = db.parse(urlConnection.getInputStream());

} catch (Exception e) { }

NodeList nl = doc.getElementsByTagName("LineString"); for (int s = 0; s < nl.getLength(); s++) { Node rootNode = nl.item(s); NodeList configItems = rootNode.getChildNodes(); for (int x = 0; x < configItems.getLength(); x++) { Node lineStringNode = configItems.item(x); NodeList path = lineStringNode.getChildNodes(); pathConent = path.item(0).getNodeValue(); } } String[] tempContent = pathConent.split(" "); return tempContent; }

}

//**************************************************

DirectionPathOverlay

public class DirectionPathOverlay extends Overlay {

private GeoPoint gp1;
private GeoPoint gp2;

public DirectionPathOverlay(GeoPoint gp1, GeoPoint gp2) {
    this.gp1 = gp1;
    this.gp2 = gp2;
}

@Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
        long when) {
    // TODO Auto-generated method stub
    Projection projection = mapView.getProjection();
    if (shadow == false) {

        Paint paint = new Paint();
        paint.setAntiAlias(true);
        Point point = new Point();
        projection.toPixels(gp1, point);
        paint.setColor(Color.BLUE);
        Point point2 = new Point();
        projection.toPixels(gp2, point2);
        paint.setStrokeWidth(2);
        canvas.drawLine((float) point.x, (float) point.y, (float) point2.x,
                (float) point2.y, paint);
    }
    return super.draw(canvas, mapView, shadow, when);
}

@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
    // TODO Auto-generated method stub

    super.draw(canvas, mapView, shadow);
}

}

richa123