0
package com.hands;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

import org.ci.geo.route.Road;
import org.ci.geo.route.RoadProvider;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.os.Bundle;
import android.os.Handler;
import android.widget.LinearLayout;
import android.widget.TextView;

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

public class RoutenamesActivity extends MapActivity {

    LinearLayout linearLayout;
    MapView mapView;
    private Road mRoad;

    @Override
    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
                            setContentView(R.layout.main);
            mapView = (MapView) findViewById(R.id.mapview);
            mapView.setBuiltInZoomControls(true);

            new Thread() {
                    @Override
                    public void run() {


                            double fromLat = 49.85, fromLon = 24.016667, toLat = 50.45, toLon = 30.523333;
                            String url = RoadProvider
                                            .getUrl(fromLat, fromLon, toLat, toLon);


                            InputStream is = getConnection(url);




                            mRoad = RoadProvider.getRoute(is);
                            mHandler.sendEmptyMessage(0);
                    }
            }.start();
    }

    Handler mHandler = new Handler() {
            public void handleMessage(android.os.Message msg) {
                    TextView textView = (TextView) findViewById(R.id.description);


                    textView.setText(mRoad.mName + " " + mRoad.mDescription);
                    MapOverlay mapOverlay = new MapOverlay(mRoad, mapView);
                    List<Overlay> listOfOverlays = mapView.getOverlays();
                    listOfOverlays.clear();
                    listOfOverlays.add(mapOverlay);
                    mapView.invalidate();
            };
    };

    private InputStream getConnection(String url) {
            InputStream is = null;
            try {
                    URLConnection conn = new URL(url).openConnection();
                    is = conn.getInputStream();
            } catch (MalformedURLException e) {
                    e.printStackTrace();
            } catch (IOException e) {
                    e.printStackTrace();
            }
            return is;
    }

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

My second class is

    class MapOverlay extends com.google.android.maps.Overlay {
    Road mRoad;
    ArrayList<GeoPoint> mPoints;

    public MapOverlay(Road road, MapView mv) {
            mRoad = road;
            if (road.mRoute.length > 0) {
                    mPoints = new ArrayList<GeoPoint>();
                    for (int i = 0; i < road.mRoute.length; i++) {
                            mPoints.add(new GeoPoint((int) (road.mRoute[i][1] * 1000000),
                                            (int) (road.mRoute[i][0] * 1000000)));
                    }
                    int moveToLat = (mPoints.get(0).getLatitudeE6() + (mPoints.get(
                                    mPoints.size() - 1).getLatitudeE6() - mPoints.get(0)
                                    .getLatitudeE6()) / 2);
                    int moveToLong = (mPoints.get(0).getLongitudeE6() + (mPoints.get(
                                    mPoints.size() - 1).getLongitudeE6() - mPoints.get(0)
                                    .getLongitudeE6()) / 2);
                    GeoPoint moveTo = new GeoPoint(moveToLat, moveToLong);

                    MapController mapController = mv.getController();
                    mapController.animateTo(moveTo);
                    mapController.setZoom(7);
            }
    }

    @Override
    public boolean draw(Canvas canvas, MapView mv, boolean shadow, long when) {
            super.draw(canvas, mv, shadow);



             drawPath(mv, canvas); 
            return true;
    }

    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++) {
   //here mPoints.size  coming as null thats why wholw appn gets crased ?**   



                Point point = new Point();
                    mv.getProjection().toPixels(mPoints.get(i), point);
                    x2 = point.x;
                    y2 = point.y;
                    if (i > 0) {
                        System.out.println("before  canvas.drawLine");

                            canvas.drawLine(x1, y1, x2, y2, paint);

                            System.out.println("after canvas.drawLine");

                    }
                    x1 = x2;
                    y1 = y2;
            }
    }
 }
skygeek
  • 1,548
  • 11
  • 24
KAREEM MAHAMMED
  • 1,675
  • 14
  • 38
  • You've failed to *ask a question*. All you've done is dropped a chunk of code (and not formatted it well), and not told us what your issue(s) are. – Damien_The_Unbeliever Aug 13 '12 at 06:23
  • @Damien_The_Unbeliever the issues when am running above code am getting errors as fatal exception and null pointer exception inside drawPath(MapView mv, Canvas canvas) having one forloop as for (int i = 0; i < mPoints.size(); i++) { here mpoints.size am getting null and can u tell me why it is getting null pls help me out of this problem . – KAREEM MAHAMMED Aug 13 '12 at 06:36
  • *Edit* your question. First, highlight all of the code and hit the `{}` button so that it's properly formatted as text. Then, above the code, give a description of the errors you're receiving. Include a suitable (code) comment to highlight the line where the error is occurring. But it's still a lot of code. It's probably worth *you* taking some time to reduce the size of the code (whilst making sure it still produces the error). We're not here to do basic debugging for you - we expect you to do *some* work. – Damien_The_Unbeliever Aug 13 '12 at 06:38
  • @Damien_The_Unbeliever here is the link that i have followed http://stackoverflow.com/questions/2023669/j2me-android-blackberry-driving-directions-route-between-two-locations could you check it out at from here..for better clarification – KAREEM MAHAMMED Aug 13 '12 at 06:46
  • @Damien_The_Unbeliever Thanks for the response as i have updated my question also in source code alo i mentioned where exactly am getting error inside getdraw() it has one for loop which contails mPoints which taking is null that i commented in {} with can you please have a look on it and pls let me know where am going wrong – KAREEM MAHAMMED Aug 13 '12 at 07:00

0 Answers0