alessandroanjos / j2memaprouteprovider

Automatically exported from code.google.com/p/j2memaprouteprovider
0 stars 0 forks source link

Anti-alias improvement #5

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
This is more a suggestion than an issue.

I've successfully used this code (many thanks to the author!) but I noticed 
that the drawPath method of the MapOverlay class draws a path which is not 
smooth at all.

To dramatically improve the path smoothness I activated anti-alias on the Paint 
and made a single call to the canvas.drawPath method instead of several calls 
to canvas.drawLine.

In the following I report the modified drawPath method, hoping that it will be 
useful to someone else.

public void drawPath(MapView mv, Canvas canvas) {
                int x1 = -1, y1 = -1, x2 = -1, y2 = -1;
                // this activates anti-alias
                Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
                paint.setColor(Color.GREEN);
                paint.setStyle(Paint.Style.STROKE);
                paint.setStrokeWidth(3);

                // creates a path to be drawn
                Path path = new Path();

                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;
                        // original code commented out
                        //if (i > 0) {
                        //        canvas.drawLine(x1, y1, x2, y2, paint);
                        //}
                        x1 = x2;
                        y1 = y2;

                        if (i == 0) {
                            path.moveTo(x1, y1);
                        }
                        else {
                            path.lineTo(x1, y1);
                        }
                }
                // draws the whole path
                canvas.drawPath(path, paint);
        }

Thanks again.
All the best.

Original issue reported on code.google.com by luca.dem...@gmail.com on 21 Mar 2012 at 5:13

GoogleCodeExporter commented 8 years ago
Thanks a tone for the anti alias..  also we can add any push pin like logo on 
map by making MapOverlay class to inner class!

public class Gmap extends MapActivity {

    MapView mapView;
    MapController mc;
    GeoPoint p;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mapView=(MapView)findViewById(R.id.mapView);
        mapView.setBuiltInZoomControls(true);
        mc = mapView.getController();
        String coordinates[] = {"1.352566007", "103.78921587"};
        double lat = Double.parseDouble(coordinates[0]);
        double lng = Double.parseDouble(coordinates[1]);

        p = new GeoPoint(        //for 2 point display have to make 2points
            (int) (lat * 1E6), 
            (int) (lng * 1E6));

        mc.animateTo(p);
        mc.setZoom(17); 
        MapOverlay mapOverlay = new MapOverlay();
        List<Overlay> listOfOverlays = mapView.getOverlays();
        listOfOverlays.clear();
        listOfOverlays.add(mapOverlay);
        mapView.invalidate();

    }

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

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);                   

            //---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.push_pin);            
            canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);         
            return true;
          //...
        }
                          ......

Original comment by somayaji...@gmail.com on 13 Apr 2012 at 1:40

Attachments:

GoogleCodeExporter commented 8 years ago
hi 
can you pls tell me why do i have "null null" in the description and the name 
of the route ?? when i run the application it dispalys this "null,null" 
textView and closes 
and these are the logCat: 
08-01 14:28:30.738: E/MapActivity(682): Couldn't get connection factory client
08-01 14:28:31.598: W/System.err(682): 
org.apache.harmony.xml.ExpatParser$ParseException: At line 1, column 1083: not 
well-formed (invalid token)
08-01 14:28:31.609: W/System.err(682):  at 
org.apache.harmony.xml.ExpatParser.parseFragment(ExpatParser.java:515)
08-01 14:28:31.609: W/System.err(682):  at 
org.apache.harmony.xml.ExpatParser.parseDocument(ExpatParser.java:474)
08-01 14:28:31.609: W/System.err(682):  at 
org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:321)
08-01 14:28:31.669: W/System.err(682):  at 
org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:279)
08-01 14:28:31.769: W/System.err(682):  at 
javax.xml.parsers.SAXParser.parse(SAXParser.java:390)
08-01 14:28:31.769: W/System.err(682):  at 
javax.xml.parsers.SAXParser.parse(SAXParser.java:187)
08-01 14:28:31.769: W/System.err(682):  at 
org.ci.geo.route.RoadProvider.getRoute(RoadProvider.java:23)
08-01 14:28:31.769: W/System.err(682):  at 
com.test.MainActivity$2.run(MainActivity.java:50)
08-01 14:28:31.918: W/dalvikvm(682): threadid=1: thread exiting with uncaught 
exception (group=0x409c01f8)
08-01 14:28:32.079: E/AndroidRuntime(682): FATAL EXCEPTION: main
08-01 14:28:32.079: E/AndroidRuntime(682): java.lang.NullPointerException
08-01 14:28:32.079: E/AndroidRuntime(682):  at 
com.test.MapOverlay.drawPath(MainActivity.java:126)
08-01 14:28:32.079: E/AndroidRuntime(682):  at 
com.test.MapOverlay.draw(MainActivity.java:116)
08-01 14:28:32.079: E/AndroidRuntime(682):  at 
com.google.android.maps.OverlayBundle.draw(OverlayBundle.java:42)
08-01 14:28:32.079: E/AndroidRuntime(682):  at 
com.google.android.maps.MapView.onDraw(MapView.java:530)
08-01 14:28:32.079: E/AndroidRuntime(682):  at 
android.view.View.draw(View.java:10978)
08-01 14:28:32.079: E/AndroidRuntime(682):  at 
android.view.ViewGroup.drawChild(ViewGroup.java:2887)
08-01 14:28:32.079: E/AndroidRuntime(682):  at 
android.view.ViewGroup.dispatchDraw(ViewGroup.java:2489)
08-01 14:28:32.079: E/AndroidRuntime(682):  at 
android.view.ViewGroup.drawChild(ViewGroup.java:2885)
08-01 14:28:32.079: E/AndroidRuntime(682):  at 
android.view.ViewGroup.dispatchDraw(ViewGroup.java:2489)
08-01 14:28:32.079: E/AndroidRuntime(682):  at 
android.view.ViewGroup.drawChild(ViewGroup.java:2885)
08-01 14:28:32.079: E/AndroidRuntime(682):  at 
android.view.ViewGroup.dispatchDraw(ViewGroup.java:2489)
08-01 14:28:32.079: E/AndroidRuntime(682):  at 
android.view.ViewGroup.drawChild(ViewGroup.java:2885)
08-01 14:28:32.079: E/AndroidRuntime(682):  at 
android.view.ViewGroup.dispatchDraw(ViewGroup.java:2489)
08-01 14:28:32.079: E/AndroidRuntime(682):  at 
android.view.View.draw(View.java:10981)
08-01 14:28:32.079: E/AndroidRuntime(682):  at 
android.widget.FrameLayout.draw(FrameLayout.java:450)
08-01 14:28:32.079: E/AndroidRuntime(682):  at 
com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:212
6)
08-01 14:28:32.079: E/AndroidRuntime(682):  at 
android.view.ViewRootImpl.draw(ViewRootImpl.java:2026)
08-01 14:28:32.079: E/AndroidRuntime(682):  at 
android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1634)
08-01 14:28:32.079: E/AndroidRuntime(682):  at 
android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
08-01 14:28:32.079: E/AndroidRuntime(682):  at 
android.os.Handler.dispatchMessage(Handler.java:99)
08-01 14:28:32.079: E/AndroidRuntime(682):  at 
android.os.Looper.loop(Looper.java:137)
08-01 14:28:32.079: E/AndroidRuntime(682):  at 
android.app.ActivityThread.main(ActivityThread.java:4424)
08-01 14:28:32.079: E/AndroidRuntime(682):  at 
java.lang.reflect.Method.invokeNative(Native Method)
08-01 14:28:32.079: E/AndroidRuntime(682):  at 
java.lang.reflect.Method.invoke(Method.java:511)
08-01 14:28:32.079: E/AndroidRuntime(682):  at 
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-01 14:28:32.079: E/AndroidRuntime(682):  at 
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-01 14:28:32.079: E/AndroidRuntime(682):  at 
dalvik.system.NativeStart.main(Native Method)

Original comment by berrich....@gmail.com on 31 Jul 2012 at 4:54

GoogleCodeExporter commented 8 years ago
Hi
Even i am also getting the same issue.
Can any one pls help?

Thanks 
Ravikanth

Original comment by raviknt...@gmail.com on 12 Apr 2013 at 7:52