Google Driving Directions - MapView overlayed

Tutorials that use the MapActivity. Many using GPS functionality.

Google Driving Directions - MapView overlayed

Postby plusminus » Sat Feb 09, 2008 4:17 pm

Google Driving Directions - MapOverlayed
Part of the AndNav!-Application :!:



:arrow: Getting the Route :larrow:
:!: below is outdated :!:




What is this: This tutorial shows how to use the Android built in access to the Google DrivingDirections API. The first step to create an actual Navigation-System.

:idea: Designed/Tested with sdk-version: m5-rc14

:?: Problems/Questions: post right below...

Difficulty: 2.5 of 5 :)

What it will look like:
Screenshot was taken with sdk-version m3 (m5 looks similar)
Image


Description: Creating a Navigation-System has never been so easy. Get the DrivingDirections from A to B with less then a handful of codelines.

0.) As usual lets start with the Layout. Its pretty simple, just a "Do it"-Button and a MapView:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:orientation="vertical"
  4.     android:layout_width="fill_parent"
  5.     android:layout_height="fill_parent"
  6.     >    
  7.     <Button android:id="@+id/cmd_submit"
  8.           android:layout_width="fill_parent"
  9.           android:layout_height="wrap_content"
  10.           android:text="Show me the Route!"/>    
  11.      <view class="com.google.android.maps.MapView"
  12.           android:id="@+id/myMapView"
  13.           android:layout_width="fill_parent"
  14.           android:layout_height="fill_parent"/>    
  15. </LinearLayout>
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


1.) Now to the DrivingDirections this tutorial is actually about. The DrivingDirections which are very very close related to GoogleMaps are fully integrated into Android. You can find them in the package: "com.google.googlenav"-Package.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. import com.google.googlenav.DrivingDirection;
Parsed in 0.031 seconds, using GeSHi 1.0.8.4

It provides the DrivingDirection-Class with the following Constructor:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. new DrivingDirection(MapPoint from, String from_string, MapPoint to, String to_string);
Parsed in 0.034 seconds, using GeSHi 1.0.8.4

You can call it either with two MapPoints or with the Description of two places (in this case pass null to the MapPoints-Parameters). In this Tutorial we will be using two pre-defined gps-coordinates, from the Google-Headquarters to the next beach :roll:.

The request for the DrivingDirections will be fired on a Click to our Button we defined in xml.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                 /* Submit one dummy search */
  2.                 findViewById(R.id.cmd_submit).setOnClickListener(new OnClickListener(){
  3.                         @Override
  4.                         public void onClick(View arg0) {
  5.                                 MapPoint mpFrom = new MapPoint(37423157, -122085008); // 37.423157,-122.085008
  6.                                 MapPoint mpTo = new MapPoint(37495999, -122457508); // 37.495999,-122.457508
  7.                                 MyDrivingDirectionsActivity.this.startFetchDirections(mpFrom,"", mpTo, "");
  8.                         }
  9.                 });
Parsed in 0.037 seconds, using GeSHi 1.0.8.4

So, a click to that button will finally cause a field in our class to be set, which was initiated with null:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. private DrivingDirection myDD = null;
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


2.) In the onCreate(...);-Function we applied a custom Overlay to our MapView using:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                 /* Find the MapView we defined in the main.xml. */
  2.                 MapView mapViewFromXML = (MapView)this.findViewById(R.id.myMapView);
  3.                 /* Retrieve its OverlayController. */
  4.                 OverlayController myOC = mapViewFromXML.createOverlayController();
  5.                 /* Add a new instance of our fancy Overlay-Class to the MapView. */
  6.                 myOC.add(new MyMapDrivingDirectionsOverlay(this), true);
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


3.) This custom Overlay will draw the DrivingDirections-Route(if found) above the Map. It mainly contains just code in the onDraw()-Function which will draw the route in a fancy way, like it can be seen in the picture in the beginning. The path-drawing-code is the following:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                         //...
  2.                                 /* Check to see if the route is too long. */
  3.                         if (!dd.routeTooLong()) {
  4.                                 /* Retrieve all (Map)Points of the route Found. */
  5.                                 MapPoint[] route = dd.getRoute();
  6.                                
  7.                                 if(route == null)
  8.                                         return;
  9.                                
  10.                                 /* Loop through all MapPoints returned. */
  11.                                 for (MapPoint current : route) {
  12.                                         /* Transform current MapPoint's Lat/Lng
  13.                                          * into corresponding point on canvas
  14.                                          * using the pixelCalculator. */
  15.                                         if(current != null){
  16.                                                 MapPointToScreenCoords(current, screenCoords, pxC);
  17.                                                 /* Add point to path. */
  18.                                                 thePath.lineTo(screenCoords[0], screenCoords[1]);
  19.                                         }
  20.                                 }
  21.                         }
  22.                         this.pathPaint.setStyle(Paint.Style.STROKE);
  23.                         /* Draw the actual route to the canvas. */
  24.                         canvas.drawPath(thePath, this.pathPaint);
  25.                         //...
Parsed in 0.039 seconds, using GeSHi 1.0.8.4


:idea: Note: After having clicked the Button the DrivingDirections are not instantly drawn. You need to move the map at least one pixel, because we need to invalidate() the MapView once. This can also be accomplished using a Handler like shown in the The Pizza Timer-Tutorial.


The Full Source:
"/res/layout/main.xml":
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:orientation="vertical"
  4.     android:layout_width="fill_parent"
  5.     android:layout_height="fill_parent"
  6.     >    
  7.     <Button android:id="@+id/cmd_submit"
  8.           android:layout_width="fill_parent"
  9.           android:layout_height="wrap_content"
  10.           android:text="Show me the Route!"/>    
  11.      <view class="com.google.android.maps.MapView"
  12.           android:id="@+id/myMapView"
  13.           android:layout_width="fill_parent"
  14.           android:layout_height="fill_parent"/>    
  15. </LinearLayout>
Parsed in 0.039 seconds, using GeSHi 1.0.8.4


"/src/your_package_structure/MyDrivingDirectionsActivity.java":
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package org.anddev.android.drivingdirections;
  2.  
  3. import android.os.Bundle;
  4. import android.util.Log;
  5. import android.view.View;
  6. import android.view.View.OnClickListener;
  7.  
  8. import com.google.android.maps.MapActivity;
  9. import com.google.android.maps.MapView;
  10. import com.google.android.maps.OverlayController;
  11. import com.google.googlenav.DrivingDirection;
  12. import com.google.googlenav.map.MapPoint;
  13.  
  14. public class MyDrivingDirectionsActivity extends MapActivity {
  15.  
  16.         private DrivingDirection myDD = null;
  17.         private boolean foundDirections = false;
  18.  
  19.         /** Called when the activity is first created. */
  20.         @Override
  21.         public void onCreate(Bundle icicle) {
  22.                 super.onCreate(icicle);
  23.                 setContentView(R.layout.main);
  24.                
  25.                 /* Find the MapView we defined in the main.xml. */
  26.                 MapView mapViewFromXML = (MapView)this.findViewById(R.id.myMapView);
  27.                 /* Retrieve its OverlayController. */
  28.                 OverlayController myOC = mapViewFromXML.createOverlayController();
  29.                 /* Add a new instance of our fancy Overlay-Class to the MapView. */
  30.                 myOC.add(new MyMapDrivingDirectionsOverlay(this), true);
  31.                
  32.                 /* Submit one dummy search */
  33.                 findViewById(R.id.cmd_submit).setOnClickListener(new OnClickListener(){
  34.                         @Override
  35.                         public void onClick(View arg0) {
  36.                                 MapPoint mpFrom = new MapPoint(37423157, -122085008); // 37.423157,-122.085008
  37.                                 MapPoint mpTo = new MapPoint(37495999, -122457508); // 37.495999,-122.457508
  38.                                 MyDrivingDirectionsActivity.this.startFetchDirections(mpFrom,"", mpTo, "");
  39.                         }
  40.                 });
  41.         }
  42.  
  43.         /** Offers the DrivingDirections to the Overlay. */
  44.         public DrivingDirection getDrivingDirections() {
  45.                 return this.myDD;
  46.         }
  47.  
  48.         private void startFetchDirections(MapPoint from_pos, String from_name,
  49.                         MapPoint to_pos, String to_name) {
  50.                 /* mDD is a class variable for the activity that will
  51.                  * hold an instance of the DrivingDirection object created here. */
  52.                 this.myDD = new DrivingDirection(from_pos, from_name, to_pos, to_name);
  53.                 if (this.myDD != null) {
  54.                         /* Add the request the dispatcher */
  55.                         this.getDispatcher().addDataRequest(this.myDD);
  56.  
  57.                         Thread t = new Thread(new Runnable() {
  58.                                 public void run() {
  59.                                         /* Wait for the search to be complete... */
  60.                                         while (!MyDrivingDirectionsActivity.this.myDD.isComplete()) {
  61.                                                 try {
  62.                                                         Thread.sleep(100);
  63.                                                 } catch (InterruptedException e) {
  64.                                                         Log.e("DEBUGTAG", "'!MyDrivingDirectionsActivity.this.myDD.isComplete()' was interruoted.",e);
  65.                                                 }
  66.                                         }
  67.                                         /* Check to see if any Placemarks were found..
  68.                                          * if 0 then there is no route! */
  69.                                         if (MyDrivingDirectionsActivity.this.myDD.getState() != DrivingDirection.SUCCESS_STATUS) {
  70.                                                 /* Set a flag to let the program know
  71.                                                  * the directions are done... */
  72.                                                 MyDrivingDirectionsActivity.this.foundDirections = true;
  73.                                         } else{ /* no route.. */
  74.                                                 MyDrivingDirectionsActivity.this.foundDirections = true;
  75.                                                 MyDrivingDirectionsActivity.this.myDD = null;
  76.                                                 /* Let the user know that no route was found... */
  77.                                         }
  78.                                 }
  79.                         });
  80.                         t.start();
  81.                 }
  82.         }
  83. }
Parsed in 0.048 seconds, using GeSHi 1.0.8.4


"/src/your_package_structure/MyMapDrivingDirectionsOverlay.java":
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. // Created by plusminus on 14:00:27 - 30.01.2008
  2. package org.anddev.android.drivingdirections;
  3.  
  4. import android.graphics.Bitmap;
  5. import android.graphics.BitmapFactory;
  6. import android.graphics.Canvas;
  7. import android.graphics.Paint;
  8. import android.graphics.Path;
  9. import android.util.Log;
  10.  
  11. import com.google.android.maps.Overlay;
  12. import com.google.android.maps.Point;
  13. import com.google.googlenav.DrivingDirection;
  14. import com.google.googlenav.map.MapPoint;
  15.  
  16. public class MyMapDrivingDirectionsOverlay extends Overlay {
  17.        
  18.         // ===========================================================
  19.         // Final Fields
  20.         // ===========================================================
  21.         /** The tip of the pin is located at x=5, y=29 */
  22.         private final android.graphics.Point PIN_HOTSPOT = new android.graphics.Point(5,29);
  23.        
  24.  
  25.         // ===========================================================
  26.         // Fields
  27.         // ===========================================================
  28.  
  29.         MyDrivingDirectionsActivity itsMapActivity = null;
  30.         Paint pathPaint = null;
  31.         DrivingDirection dd = null;
  32.        
  33.         private Bitmap PIN_START = null;
  34.         private Bitmap PIN_END = null;
  35.         private Bitmap INFO_LOWER_LEFT = null;
  36.  
  37.         // ===========================================================
  38.         // "Constructors"
  39.         // ===========================================================
  40.        
  41.         public MyMapDrivingDirectionsOverlay(MyDrivingDirectionsActivity map) {
  42.                 itsMapActivity = map;
  43.                 this.pathPaint = new Paint();
  44.                 this.pathPaint.setAntiAlias(true);
  45.                
  46.                 PIN_START = BitmapFactory.decodeResource(this.itsMapActivity.getResources(), R.drawable.mappin_blue);
  47.                 PIN_END = BitmapFactory.decodeResource(this.itsMapActivity.getResources(), R.drawable.mappin_red);
  48.                 INFO_LOWER_LEFT = BitmapFactory.decodeResource(this.itsMapActivity.getResources(), R.drawable.lower_left_info);
  49.         }
  50.  
  51.         // ===========================================================
  52.         // Getter & Setter
  53.         // ===========================================================
  54.  
  55.         // ===========================================================
  56.         // Methods
  57.         // ===========================================================
  58.  
  59.         /** This function does some fancy drawing of the
  60.          * Driving-Directions. It could be shortened a lot. */
  61.         public void draw(Canvas canvas, PixelCalculator pxC, boolean b) {
  62.                 super.draw(canvas, pxC, b);
  63.                
  64.                 /* Reset our paint. */
  65.                 this.pathPaint.setStrokeWidth(4);
  66.                 this.pathPaint.setARGB(100, 113, 105, 252);
  67.                 // holders of mapped coords...
  68.                 int[] screenCoords = new int[2];
  69.  
  70.                 // method in the custom map view to return the DrivingDirection object.
  71.                 dd = itsMapActivity.getDrivingDirections();
  72.                 if (dd != null) {
  73.                         /* First get Start end End Point of the route. */
  74.                         MapPoint startPoint = dd.getStartPoint();
  75.                         MapPoint endPoint = dd.getEndPoint();
  76.  
  77.                         MapPointToScreenCoords(startPoint, screenCoords, pxC);
  78.                         /* Create a path-that will be filled with map-points
  79.                          * and will be drawn to the canvas in the end*/
  80.                         Path thePath = new Path();
  81.                         thePath.moveTo(screenCoords[0], screenCoords[1]);
  82.                        
  83.                         /* Check to see if the route is too long. */
  84.                         if (!dd.routeTooLong()) {
  85.                                 /* Retrieve all (Map)Points of the route Found. */
  86.                                 MapPoint[] route = dd.getRoute();
  87.                                
  88.                                 if(route == null)
  89.                                         return;
  90.                                
  91.                                 /* Loop through all MapPoints returned. */
  92.                                 for (MapPoint current : route) {
  93.                                         /* Transform current MapPoint's Lat/Lng
  94.                                          * into corresponding point on canvas
  95.                                          * using the pixelCalculator. */
  96.                                         if(current != null){
  97.                                                 MapPointToScreenCoords(current, screenCoords, pxC);
  98.                                                 /* Add point to path. */
  99.                                                 thePath.lineTo(screenCoords[0], screenCoords[1]);
  100.                                         }
  101.                                 }
  102.                         }
  103.                         this.pathPaint.setStyle(Paint.Style.STROKE);
  104.                         /* Draw the actual route to the canvas. */
  105.                         canvas.drawPath(thePath, this.pathPaint);
  106.                        
  107.                         /* Finally draw a fancy PIN to mark the start... */
  108.                         MapPointToScreenCoords(endPoint, screenCoords, pxC);
  109.                        
  110.                         canvas.drawBitmap(PIN_END,
  111.                                         screenCoords[0] - PIN_HOTSPOT.x,
  112.                                         screenCoords[1] - PIN_HOTSPOT.y,
  113.                                         pathPaint);
  114.                        
  115.                         /* ...and the end of the route.*/
  116.  
  117.                         MapPointToScreenCoords(startPoint, screenCoords, pxC);
  118.                         canvas.drawBitmap(PIN_START,
  119.                                         screenCoords[0] - PIN_HOTSPOT.x,
  120.                                         screenCoords[1] - PIN_HOTSPOT.y,
  121.                                         pathPaint);
  122.                        
  123.                         /* Get the height of the underlying MapView.*/
  124.                         int mapViewHeight = this.itsMapActivity.findViewById(R.id.myMapView).getHeight();
  125.                        
  126.                         /* Now some real fancy stuff !*/
  127.                         /* Draw a info-menu for the route.*/
  128.                         canvas.drawBitmap(this.INFO_LOWER_LEFT, 0, mapViewHeight - this.INFO_LOWER_LEFT.height(), pathPaint);
  129.                        
  130.                         /* And draw i.e.the distance and time left to the info-menu.*/
  131.                         String distance = dd.getFormattedDistance();
  132.                         String time = dd.getFormattedTime().replace("Time: ", "");
  133.                         pathPaint.setARGB(255,255,255,255);
  134.                         this.pathPaint.setStrokeWidth(1);
  135.                         this.pathPaint.setStyle(Paint.Style.FILL_AND_STROKE);
  136.                        
  137.                         pathPaint.setTextSize(24);
  138.                         canvas.drawText(time, 5, mapViewHeight - 5, pathPaint);
  139.                         pathPaint.setTextSize(16);
  140.                         canvas.drawText(distance, 4, mapViewHeight - 35, pathPaint); // 2, 271
  141.                        
  142.                         /* These methods are to illustrate some
  143.                          * of what you can get out of the system. */
  144. //                      String startName = dd.getRouteStartLocation();
  145. //                      String info = dd.getRouteInfoDescriptor();
  146.                         Log.d("DEBUGTAG", dd.getTurns()[0]);
  147.                 }
  148.         }
  149.        
  150.         private void MapPointToScreenCoords(MapPoint mp, int[] targetScreenCoords, PixelCalculator pxc){
  151.                 Point p = new Point(mp.getLatitude(), mp.getLongitude());
  152.                 pxc.getPointXY(p, targetScreenCoords);
  153.         }
  154. }
  155.  
Parsed in 0.057 seconds, using GeSHi 1.0.8.4

Thats it :)


Regards,
plusminus
Attachments
DrivingDirections_m5.zip
For SDK-version m5 :!:
Full Project Source
(56.04 KiB) Downloaded 8896 times
DrivingDirections.zip
For SDK-version m3 :!:
Full Project Source
(57.46 KiB) Downloaded 6177 times
Last edited by plusminus on Fri Nov 14, 2008 5:07 pm, edited 8 times in total.
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

Driving directions application is nice

Postby gvkreddyvamsi » Mon Feb 11, 2008 7:13 am

HI,

I have done things well. But i cann't resolve R.java in this application.
can u send zip for this application?

by
vamsi
gvkreddyvamsi
Developer
Developer
 
Posts: 43
Joined: Mon Jan 21, 2008 5:10 am
Location: INDIA

Postby plusminus » Mon Feb 11, 2008 2:28 pm

Hello,

attached it above :uarrow:

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Questions regarding this tutorial...

Postby tv_sathish » Tue Feb 12, 2008 12:20 pm

Hi Plusminus,
I am getting a compilation error in the line:

Code: Select all
      /* Submit one dummy search */
      findViewById(R.id.cmd_submit).setOnClickListener(new OnClickListener(){
         @Override
         [b]public void onClick(View arg0) {[/b]


Error is The method onClick(View) of type new View.OnClickListener(){} must override a superclass method MyDrivingDirectionsActivity/src/org/anddev/android/drivingdirections/MyDrivingDirectionsActivity.java line 34

What could be the problem?

By the by, if we are using the inbuilt DrivingDirections, why haven't u used DrivingDirectionsRenderer to draw the Driving Directions? Is that class not fully implemented?

And I had posted a few questions in the "Coding Problems" forum, waiting for your reply, let me know if I am disturbing you during exam time...

Hey, I was having a similar idea that you are planning as AndNav, but since you are already into it and 75% through, let me not compete and spoil your chances too...all the best man for that entry...

Regards,
Sathish
tv_sathish
Developer
Developer
 
Posts: 29
Joined: Wed Jan 09, 2008 12:32 pm

Postby plusminus » Tue Feb 12, 2008 1:40 pm

Hello tv_sathish,

you've got two possibilities:
a.) simply Comment the @Override-Annotation out
b.) Update to JDK 1.6 (preferred)

This happens because the JDK 1.5, you are probably running, does accept @Override-Annotation only for overridden methods of Super-Classes and not for overriding functions of Interfaces we are implementing here.

On JDK 1.6 this Annotation is possible. :roll:

Hm googling for DrivingDirectionRenderer returns unbelievable two results :!: In the SDK-Documentation, even DrivingDirection returns zero results :roll:.
Both are probably not 100% ready to be used :roll:

Feel free to submit any similar application, because competition is the best for the market :)

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby wrapware » Wed Feb 13, 2008 2:54 pm

Hi plusminus,

its a really nice app. There is one question:

Why are you assign two time the 'true' to foundDirections?

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                         if (MyDrivingDirectionsActivity.this.myDD.numPlacemarks() > 0) {
  2.  
  3.                               /* Set a flag to let the program know
  4.  
  5.                                * the directions are done... */
  6.  
  7.                               MyDrivingDirectionsActivity.this.foundDirections = true;
  8.  
  9.                          } else{ /* no route.. */
  10.  
  11.                               MyDrivingDirectionsActivity.this.foundDirections = true;
  12.  
  13.                               MyDrivingDirectionsActivity.this.myDD = null;
  14.  
  15.                               /* Let the user know that no route was found... */
  16.  
  17.                          }
Parsed in 0.038 seconds, using GeSHi 1.0.8.4


In your code is no reference to the variable foundDirections, so whats the reason for the whole thread processing with the foundDirections member? I know the user should not wait in the main thread, but ...
Maybe there is a dig deeper I'm unable to see?

Regards,
ww
wrapware
Freshman
Freshman
 
Posts: 7
Joined: Thu Jan 17, 2008 7:48 pm
Location: Germany

Top

Postby plusminus » Wed Feb 13, 2008 3:01 pm

Hello wrapware,

the second one should be false of course :oops: (corrected it above)

I didn't remove it because it could become useful if one takes this code as a base for another Application.

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby wrapware » Fri Feb 15, 2008 9:30 am

Hi plusminus

Yesterday I switch to m5_rc14 and after some modifications, your code run still fine, but with one difference.

The onDraw method is now called immediately after returning, when nothing is available for drawing (route==null)

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.     /** This function does some fancy drawing of the
  2.  
  3.       * Driving-Directions. It could be shortened a lot. */
  4.  
  5.      public void draw(Canvas canvas, PixelCalculator pxC, boolean b) {
  6.  
  7.           super.draw(canvas, pxC, b);
  8.  
  9.  
  10.  
  11.           // ...
  12.  
  13.  
  14.  
  15.                  if(route == null)
  16.  
  17.                          return;
Parsed in 0.037 seconds, using GeSHi 1.0.8.4

So the cpu usage run up to 100%, but there is nothing to draw. Is there a way to tell the
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. MyMapDrivingDirectionsOverlay
Parsed in 0.036 seconds, using GeSHi 1.0.8.4
or the
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. maps.Overlay
Parsed in 0.036 seconds, using GeSHi 1.0.8.4
to giveup redrawing? I dump the callers hierarchie when if(route == null) happens, and thats the stack for every cycle:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.       at MyMapDrivingDirectionsOverlay.draw(MyMapDrivingDirectionsOverlayjava:88 )
  2.  
  3.       at com.google.android.maps.Overlay.draw(Overlay.java:154)
  4.  
  5.       at com.google.android.maps.OverlayBundle.draw(OverlayBundle.java:43)
  6.  
  7.       at com.google.android.maps.MapView.onDraw(MapView.java:337)
  8.  
  9.       at android.view.View.draw(View.java:4574)
  10.  
  11.       at android.view.ViewGroup.drawChild(ViewGroup.java:1089)
  12.  
  13.       at android.view.ViewGroup.dispatchDraw(ViewGroup.java:915)
  14.  
  15.       at android.view.View.draw(View.java:4550)
  16.  
  17.       at android.widget.FrameLayout.draw(FrameLayout.java:208)
  18.  
  19.       at android.view.ViewGroup.drawChild(ViewGroup.java:1089)
  20.  
  21.       at android.view.ViewGroup.dispatchDraw(ViewGroup.java:915)
  22.  
  23.       at android.view.View.draw(View.java:4578)
  24.  
  25.       at android.view.ViewGroup.drawChild(ViewGroup.java:1089)
  26.  
  27.       at android.view.ViewGroup.dispatchDraw(ViewGroup.java:915)
  28.  
  29.       at android.view.View.draw(View.java:4550)
  30.  
  31.       at android.widget.FrameLayout.draw(FrameLayout.java:208)
  32.  
  33.       at android.view.ViewGroup.drawChild(ViewGroup.java:1089)
  34.  
  35.       at android.view.ViewGroup.dispatchDraw(ViewGroup.java:915)
  36.  
  37.       at android.view.View.draw(View.java:4550)
  38.  
  39.       at android.view.ViewGroup.drawChild(ViewGroup.java:1089)
  40.  
  41.       at android.view.ViewGroup.dispatchDraw(ViewGroup.java:915)
  42.  
  43.       at android.view.View.draw(View.java:4550)
  44.  
  45.       at android.widget.FrameLayout.draw(FrameLayout.java:208)
  46.  
  47.       at android.view.ViewGroup.drawChild(ViewGroup.java:1089)
  48.  
  49.       at android.view.ViewGroup.dispatchDraw(ViewGroup.java:915)
  50.  
  51.       at android.view.View.draw(View.java:4578)
  52.  
  53.       at android.widget.FrameLayout.draw(FrameLayout.java:208)
  54.  
  55.       at android.view.ViewRoot.draw(ViewRoot.java:531)
  56.  
  57.       at android.view.ViewRoot.performTraversals(ViewRoot.java:429)
  58.  
  59.       at android.view.ViewRoot.handleMessage(ViewRoot.java:584)
  60.  
  61.       at android.os.Handler.dispatchMessage(Handler.java:80)
  62.  
  63.       at android.os.Looper.loop(Looper.java:91)
  64.  
  65.       at android.app.ActivityThread.main(ActivityThread.java:3052)
  66.  
  67.       at java.lang.reflect.Method.invokeNative(Native Method)
  68.  
  69.       at java.lang.reflect.Method.invoke(Method.java:356)
  70.  
  71.       at android.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1547)
  72.  
  73.       at android.os.ZygoteInit.main(ZygoteInit.java:1445)
  74.  
  75.       at android.dalvik.NativeStart.main(Native Method)
Parsed in 0.044 seconds, using GeSHi 1.0.8.4



Regards,
ww
wrapware
Freshman
Freshman
 
Posts: 7
Joined: Thu Jan 17, 2008 7:48 pm
Location: Germany

Postby bavarol » Fri Feb 15, 2008 12:27 pm

Hi PlusMinus,
I'd like to know where u have founded "DrivingDirection" API, I know, DrivingDirection.class is in android.jar but i couldn find any reference or api for it.

Kind Regards ;)
bavarol
Experienced Developer
Experienced Developer
 
Posts: 52
Joined: Mon Dec 10, 2007 9:19 pm

Postby plusminus » Fri Feb 15, 2008 12:44 pm

Hello bavarol,
@ Wrapware
What kind of Exception is thrown :?:

@ bavarol
seen the buzzword some in GoogleGroups and decided that I have to make an Navigation-Application of it :)
:arrow: :src: AndNav!.

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby MrSnowflake » Sat Feb 16, 2008 3:13 pm

Nice, these google map based navigation systems would be very nice and low priced (free :) ) navigation systems, but they would cost a lot in use (downloading maps). Is there a way to download (say, in my case) the whole area of Belgium on multiple zooming levels and use them? So I would only need an internet connection to get the route information.
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby wrapware » Sun Feb 17, 2008 10:53 am

Hi plusminus,

There is no exception, I only use this code, to see the callers hierarchie in the trace:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.             try
  2.  
  3.             {
  4.  
  5.               throw new Exception();
  6.  
  7.             }
  8.  
  9.             catch (Exception e)
  10.  
  11.             {
  12.  
  13.               Log.e("", "", e);
  14.  
  15.             }
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


The questions is: Why is the draw method called, and called, and called, but there is no update necessary?

Regards, and nice sunday
ww
wrapware
Freshman
Freshman
 
Posts: 7
Joined: Thu Jan 17, 2008 7:48 pm
Location: Germany

Postby plusminus » Sun Feb 17, 2008 1:44 pm

:P Now I know what you were doing...

A whole Activity consists of many views that need to get drawn. A Father/Container-View manages to draw all of its children. And this may also be a Father/Container-View to draw all his children..........:
protected void dispatchDraw(Canvas canvas)
Called by draw to draw the child views. This may be overridden by derived classes to gain control just before its children are drawn (but after its own view has been drawn).


Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby agent00 » Sun Feb 17, 2008 2:05 pm

Hi everybody,

I just update my SDK version and saw that (in the multiple corrections to do) this piece of code is no more valid in the m5 SDK version.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. /* Check to see if any Placemarks were found..
  2.  
  3.                           * if 0 then there is no route! */
  4.  
  5.                          if (MyDrivingDirectionsActivity.this.myDD.numPlacemarks() > 0) {
  6.  
  7.                               /* Set a flag to let the program know
  8.  
  9.                                * the directions are done... */
  10.  
  11.                               MyDrivingDirectionsActivity.this.foundDirections = true;
  12.  
  13.                          } else{ /* no route.. */
  14.  
  15.                               MyDrivingDirectionsActivity.this.foundDirections = false;
  16.  
  17.                               MyDrivingDirectionsActivity.this.myDD = null;
  18.  
  19.                               /* Let the user know that no route was found... */
  20.  
  21.                          }
Parsed in 0.039 seconds, using GeSHi 1.0.8.4


In fact, the myDD.numPlacemarks() method doesn't exist in this version. The lack of API for com.google.googlenav package make the update quite hard.

Is someone knows what are the substitution code to write ?

Thanks in advance,

Regards,
Agent00
agent00
Junior Developer
Junior Developer
 
Posts: 12
Joined: Fri Jan 18, 2008 4:23 pm

Postby plusminus » Sun Feb 17, 2008 3:37 pm

Hello agent00,

got it working a bit smoother with:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         public void run() {
  2.  
  3.                 /* Wait for the search to be complete... */
  4.  
  5.                 while (!MyDrivingDirectionsActivity.this.myDD.isComplete()) {
  6.  
  7.                         try {
  8.  
  9.                                 Thread.sleep(100);
  10.  
  11.                         } catch (InterruptedException e) {
  12.  
  13.                                 Log.e("DEBUGTAG", "'!MyDrivingDirectionsActivity.this.myDD.isComplete()' was interruoted.",e);
  14.  
  15.                         }
  16.  
  17.                 }
  18.  
  19.                 /* Check to see if any Placemarks were found..
  20.  
  21.                  * if 0 then there is no route! */
  22.  
  23.                 if (MyDrivingDirectionsActivity.this.myDD.getState() != DrivingDirection.SUCCESS_STATUS) {
  24.  
  25.                         /* Set a flag to let the program know
  26.  
  27.                          * the directions are done... */
  28.  
  29.                         MyDrivingDirectionsActivity.this.foundDirections = true;
  30.  
  31.                 } else{ /* no route.. */
  32.  
  33.                         MyDrivingDirectionsActivity.this.foundDirections = true;
  34.  
  35.                         MyDrivingDirectionsActivity.this.myDD = null;
  36.  
  37.                         /* Let the user know that no route was found... */
  38.  
  39.                 }
  40.  
  41.         }
Parsed in 0.039 seconds, using GeSHi 1.0.8.4


Also adapted this in Overlay (threw a NullPointerException once):
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         /* Loop through all MapPoints returned. */
  2.  
  3.         for (MapPoint current : route) {
  4.  
  5.                 /* Transform current MapPoint's Lat/Lng
  6.  
  7.                  * into corresponding point on canvas
  8.  
  9.                  * using the pixelCalculator. */
  10.  
  11.                 if(current != null){
  12.  
  13.                         MapPointToScreenCoords(current, screenCoords, pxC);
  14.  
  15.                         /* Add point to path. */
  16.  
  17.                         thePath.lineTo(screenCoords[0], screenCoords[1]);
  18.  
  19.                 }
  20.  
  21.         }
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top
Next

Return to Map Tutorials

Who is online

Users browsing this forum: No registered users and 3 guests