Directions With Google

Put problem concerning MapActivities, MapViews, Overlays and GPS in this forum.

Directions With Google

Postby loteodor » Mon Jun 08, 2009 9:25 pm

Hi to everyone!!
So, I've to deliver a project in two week.In this project I should use the package com.google.googlenav for drawing one the maps some Driving or "Walking" DIRections but from some sites I have known that it isn't avaible anymore for SDK 1.5(as it was for SDK 0.9).So please is there anybody that can solve this problem??Is there an other way to obtain that directions?In this case can someone explain me the details and the steps to use this google Api??PLEASE HELP ME!!!!!THE TIME IS OVER!!

I read a lot of tutorial realized by Plusminus, I understand that you are very good in maps..if you know something about this please help me!
regards:)
loteodor
Freshman
Freshman
 
Posts: 3
Joined: Mon Jun 01, 2009 6:17 pm

Top

Postby zpk1234 » Sat Jun 27, 2009 1:30 pm

I encounter this problem too!so anyone can help us?
zpk1234
Junior Developer
Junior Developer
 
Posts: 22
Joined: Sat Jun 27, 2009 1:17 pm

Postby Fran » Sat Jun 27, 2009 5:26 pm

Fran
Freshman
Freshman
 
Posts: 6
Joined: Thu Jun 18, 2009 10:00 am

Postby zpk1234 » Sun Jun 28, 2009 7:47 am

Fran wrote:Did you see this post: http://csie-tw.blogspot.com/2009/06/and ... -path.html ??


sorry. but I can not open this link
zpk1234
Junior Developer
Junior Developer
 
Posts: 22
Joined: Sat Jun 27, 2009 1:17 pm

Postby zpk1234 » Sun Jun 28, 2009 8:21 am

In china, we can not access the link
http://csie-tw.blogspot.com/2009/06/and ... -path.html
zpk1234
Junior Developer
Junior Developer
 
Posts: 22
Joined: Sat Jun 27, 2009 1:17 pm

Postby Fran » Sun Jun 28, 2009 12:18 pm

I made a copy & paste of this blog:


Abstract

The DrivingDirection package (com.google.googlenav.DrivingDirection) is removed from Android SDK 1.1. However, in this article, I will show you how to adopt the driving direction function in MapView object without the DrivingDirection package.

1. Prepare the map resource and Internet accessibility.

1.1 Open the main.xml file in layout directory, and add a map reource in the file.
Syntax: [ Download ] [ Hide ]
Using xml 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.   <com.google.android.maps.MapView
  8.    android:id="@+id/myMapView1"
  9.    android:layout_width="fill_parent"
  10.    android:layout_height="fill_parent"
  11.    android:layout_x="0px"
  12.    android:enabled="true"
  13.    android:clickable="true"
  14.   android:apiKey="0mRN-6bSm63hZJtPZSmcjoZAzdCztLnZv-O4SZw"   android:layout_y="105px">
  15.   </com.google.android.maps.MapView>
  16. </LinearLayout>
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


1.1.1 You have to apply a android map api key for your computer. Find the debug.keystore path in Eclpise(Window->Rreferences).

Image

1.1.2 In cmd console, type
keytool -list -alias androiddebugkey -keystore "C:Documents and SettingsAdministratorLocal SettingsApplication Data
Androiddebug.keystore" -storepass android -keypass android


1.1.3 Go to http://code.google.com/intl/zh-TW/andro ... ignup.html, type your MD5 fingerprint, so that you can get the map api key as follow.

1.2 Open AndroidManifest.xml, add

<uses-library android:name="com.google.android.maps"/>

and

<uses-permission android:name="android.permission.INTERNET" />

Therefore, the file would be like:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3.      package="com.goolge"
  4.      android:versionCode="1"
  5.      android:versionName="1.0.0">
  6.     <application android:icon="@drawable/icon" android:label="@string/app_name">
  7.         <activity android:name=".RoutePath"
  8.                  android:label="@string/app_name">
  9.             <intent-filter>
  10.                 <action android:name="android.intent.action.MAIN" />
  11.                 <category android:name="android.intent.category.LAUNCHER" />
  12.             </intent-filter>
  13.         </activity>      
  14.        <uses-library android:name="com.google.android.maps"/>      
  15.     </application>  
  16.     <uses-permission android:name="android.permission.INTERNET" />  
  17. </manifest>
  18.  
Parsed in 0.003 seconds, using GeSHi 1.0.8.4


2. Draw the route path in your map.

2.1 Get KML route file from google.

For a normal user, the google maps help them get the route path in map figures. However, we would like to get the KML file of the route path. I found a parameter in the google map URL controls the output type.

* output= Output format (blank (default) is a standard webpage for user)
o output=html Uses the old style Google Local page format from before it merged with Google Maps, with the small map and large sidebar.
o output=js Outputs JavaScript object literals and function calls used by Google Maps, including encoded polyline data for driving directions, and stage information in HTML format.
o output=kml Outputs a KML file containing information representing the current map. (works with Normal Searches, Directions and MyMaps)
o output=nl Outputs a small KML file containing a NetworkLink wrapper linking to a URL from which Google Earth and Google Maps can obtain the information (only known to work with MyMaps).
o output=embed Outputs HTML suitable for embedding in third party sites, only works with the presence of the encrypted s= param, presumably to stop arbitrary content being included.
o output=dragdir returns a JSON object that contains the reverse geocode and a an encoded polyline for a given saddr (start point of the route) and daddr (endpoint of the route)
o output=georss (Geo)RSS output for the current map - probably only MyMaps

And the latitude and longitude of source and destination are determined by saddr and daddr parameter, respectively.

For example, a route KML file can be accessed through this URL:

http://maps.google.com/maps?f=d&hl=en&s ... output=kml


Image

In the KML file, each point in the route path is shown in terms of (longitude, latitude, heigth).

2.2 Create DrawPath(…) in your activity. This function do the following procedure.

a) Building the URL from src and dest.

b) Connecting to the URL and create a DocumentBuilder to parse the KML file.

c) Split each point in the path and draw each the line on the mMapView01.


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. private void DrawPath(GeoPoint src,GeoPoint dest, int color, MapView mMapView01)
  2.    {
  3.        // connect to map web service
  4.        StringBuilder urlString = new StringBuilder();
  5.        urlString.append("http://maps.google.com/maps?f=d&hl=en");
  6.        urlString.append("&saddr=");//from
  7.        urlString.append( Double.toString((double)src.getLatitudeE6()/1.0E6 ));
  8.        urlString.append(",");
  9.        urlString.append( Double.toString((double)src.getLongitudeE6()/1.0E6 ));
  10.        urlString.append("&daddr=");//to
  11.        urlString.append( Double.toString((double)dest.getLatitudeE6()/1.0E6 ));
  12.        urlString.append(",");
  13.        urlString.append( Double.toString((double)dest.getLongitudeE6()/1.0E6 ));  
  14.        urlString.append("&ie=UTF8&0&om=0&output=kml");
  15.         Log.d("xxx","URL="+urlString.toString());
  16.        // get the kml (XML) doc. And parse it to get the coordinates(direction route).
  17.        Document doc = null;
  18.        HttpURLConnection urlConnection= null;
  19.        URL url = null;
  20.        try
  21.        {                
  22.              url = new URL(urlString.toString());
  23.              urlConnection=(HttpURLConnection)url.openConnection();
  24.              urlConnection.setRequestMethod("GET");
  25.              urlConnection.setDoOutput(true);
  26.              urlConnection.setDoInput(true);
  27.              urlConnection.connect();                          
  28.  
  29.            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  30.            DocumentBuilder db = dbf.newDocumentBuilder();
  31.            doc = db.parse(urlConnection.getInputStream());              
  32.  
  33.            if(doc.getElementsByTagName("GeometryCollection").getLength()>0)
  34.            {
  35.                //String path = doc.getElementsByTagName("GeometryCollection").item(0).getFirstChild().getFirstChild().getNodeName();
  36.                String path = doc.getElementsByTagName("GeometryCollection").item(0).getFirstChild().getFirstChild().getFirstChild().getNodeValue() ;
  37.                Log.d("xxx","path="+ path);
  38.                String [] pairs = path.split(" ");                      
  39.                String[] lngLat = pairs[0].split(",");    // lngLat[0]=longitude lngLat[1]=latitude lngLat[2]=height
  40.                // src
  41.                GeoPoint startGP = new GeoPoint((int)(Double.parseDouble(lngLat[1])*1E6),(int)(Double.parseDouble(lngLat[0])*1E6));
  42.                mMapView01.getOverlays().add(new MyOverLay(startGP,startGP,1));
  43.                GeoPoint gp1;
  44.                GeoPoint gp2 = startGP;                      
  45.                for(int i=1;i<pairs.length;i++) // the last one would be crash
  46.                {
  47.                    lngLat = pairs[i].split(",");  
  48.                    gp1 = gp2;
  49.                    // watch out! For GeoPoint, first:latitude, second:longitude
  50.                    gp2 = new GeoPoint((int)(Double.parseDouble(lngLat[1])*1E6),(int)(Double.parseDouble(lngLat[0])*1E6));
  51.                    mMapView01.getOverlays().add(new MyOverLay(gp1,gp2,2,color));
  52.                    Log.d("xxx","pair:" + pairs[i]);
  53.                }
  54.                mMapView01.getOverlays().add(new MyOverLay(dest,dest, 3)); // use the default color
  55.            }        
  56.        }
  57.        catch (MalformedURLException e)
  58.        {  
  59.            e.printStackTrace();
  60.        }
  61.        catch (IOException e)
  62.        {
  63.            e.printStackTrace();
  64.        }
  65.        catch (ParserConfigurationException e)
  66.        {
  67.            e.printStackTrace();
  68.        }
  69.        catch (SAXException e)
  70.        {
  71.            e.printStackTrace();
  72.        }
  73.    }
  74.  
Parsed in 0.044 seconds, using GeSHi 1.0.8.4

2.3 Adding MyOverlay class – Drawing the points and lines on the ViewMap.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.goolge;
  2.  
  3. import android.graphics.Bitmap;
  4. import android.graphics.Canvas;
  5. import android.graphics.Color;
  6. import android.graphics.Paint;
  7. import android.graphics.Point;
  8. import android.graphics.RectF;
  9. import com.google.android.maps.GeoPoint;
  10. import com.google.android.maps.MapView;
  11. import com.google.android.maps.Overlay;
  12. import com.google.android.maps.Projection;
  13.  
  14. public class MyOverLay extends Overlay
  15. {
  16.   private GeoPoint gp1;
  17.   private GeoPoint gp2;
  18.   private int mRadius=6;
  19.   private int mode=0;
  20.   private int defaultColor;
  21.   private String text="";
  22.   private Bitmap img = null;
  23.  
  24.   public MyOverLay(GeoPoint gp1,GeoPoint gp2,int mode) //  GeoPoint is a int. (6E)
  25.   {
  26.     this.gp1 = gp1;
  27.     this.gp2 = gp2;
  28.     this.mode = mode;
  29.     defaultColor = 999; // no defaultColor
  30.  
  31.   }
  32.  
  33.   public MyOverLay(GeoPoint gp1,GeoPoint gp2,int mode, int defaultColor)
  34.   {
  35.     this.gp1 = gp1;
  36.     this.gp2 = gp2;
  37.     this.mode = mode;
  38.     this.defaultColor = defaultColor;
  39.   }
  40.   public void setText(String t)
  41.   {
  42.       this.text = t;
  43.   }
  44.   public void setBitmap(Bitmap bitmap)
  45.   {
  46.       this.img = bitmap;
  47.   }
  48.   public int getMode()
  49.   {
  50.       return mode;
  51.   }
  52.  
  53.   @Override
  54.   public boolean draw
  55.   (Canvas canvas, MapView mapView, boolean shadow, long when)
  56.   {
  57.     Projection projection = mapView.getProjection();
  58.     if (shadow == false)
  59.     {    
  60.       Paint paint = new Paint();
  61.       paint.setAntiAlias(true);
  62.       Point point = new Point();
  63.       projection.toPixels(gp1, point);
  64.       // mode=1:start
  65.       if(mode==1)
  66.       {
  67.         if(defaultColor==999)
  68.             paint.setColor(Color.BLUE);  
  69.         else
  70.              paint.setColor(defaultColor);  
  71.         RectF oval=new RectF(point.x - mRadius, point.y - mRadius,
  72.                              point.x + mRadius, point.y + mRadius);
  73.         // start point
  74.         canvas.drawOval(oval, paint);
  75.       }
  76.       // mode=2:path
  77.       else if(mode==2)
  78.       {
  79.           if(defaultColor==999)
  80.             paint.setColor(Color.RED);  
  81.         else
  82.              paint.setColor(defaultColor);  
  83.         Point point2 = new Point();
  84.         projection.toPixels(gp2, point2);
  85.         paint.setStrokeWidth(5);
  86.         paint.setAlpha(120);      
  87.         canvas.drawLine(point.x, point.y, point2.x,point2.y, paint);      
  88.       }
  89.       /* mode=3:end */
  90.       else if(mode==3)
  91.       {
  92.         /* the last path */
  93.  
  94.           if(defaultColor==999)
  95.             paint.setColor(Color.GREEN);  
  96.         else
  97.              paint.setColor(defaultColor);  
  98.         Point point2 = new Point();
  99.         projection.toPixels(gp2, point2);
  100.         paint.setStrokeWidth(5);
  101.         paint.setAlpha(120);
  102.         canvas.drawLine(point.x, point.y, point2.x,point2.y, paint);
  103.         RectF oval=new RectF(point2.x - mRadius,point2.y - mRadius,
  104.                              point2.x + mRadius,point2.y + mRadius);
  105.         /* end point */
  106.         paint.setAlpha(255);
  107.         canvas.drawOval(oval, paint);
  108.       }
  109.     }
  110.     return super.draw(canvas, mapView, shadow, when);
  111.   }
  112.  
  113. }
  114.  
Parsed in 0.044 seconds, using GeSHi 1.0.8.4


3. How to use DrawPath(…) function in your activity.

3.1 Your activity must extends MapActivity, instead of Activity.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. ublic class RoutePath extends MapActivity {
  2.     /** Called when the activity is first created. */
  3.  
  4.     MapView mapView;
  5.  
  6.     @Override
  7.     public void onCreate(Bundle savedInstanceState) {
  8.         super.onCreate(savedInstanceState);
  9.         setContentView(R.layout.main);
  10.  
  11.         MapView mapView = (MapView) findViewById(R.id.myMapView1);
  12.         double src_lat = 25.04202; // the testing source
  13.         double src_long = 121.534761;
  14.         double dest_lat = 25.05202; // the testing destination
  15.         double dest_long = 121.554761;
  16.         GeoPoint srcGeoPoint = new GeoPoint((int) (src_lat * 1E6),
  17.                 (int) (src_long * 1E6));
  18.         GeoPoint destGeoPoint = new GeoPoint((int) (dest_lat * 1E6),
  19.                 (int) (dest_long * 1E6));
  20.  
  21.         DrawPath(srcGeoPoint, destGeoPoint, Color.GREEN, mapView);
  22.  
  23.         mapView.getController().animateTo(srcGeoPoint);
  24.         mapView.getController().setZoom(15);
  25.  
  26.     }
  27.  
  28.     @Override
  29.     protected boolean isRouteDisplayed() {
  30.         // TODO Auto-generated method stub
  31.         return false;
  32.     }
  33.  
  34.     private void DrawPath(GeoPoint src, GeoPoint dest, int color,
  35.             MapView mMapView01) {
  36.  
  37.     // code in section 2.1
  38.  
  39.     }
  40.  
  41. }
  42.  
Parsed in 0.038 seconds, using GeSHi 1.0.8.4

3.2 The screenshot

Emulator:
Image
Fran
Freshman
Freshman
 
Posts: 6
Joined: Thu Jun 18, 2009 10:00 am

Top

Postby Fran » Sun Jun 28, 2009 12:26 pm

I hope that csie-tw doesn't matter that I made a copy & paste for you
Fran
Freshman
Freshman
 
Posts: 6
Joined: Thu Jun 18, 2009 10:00 am

Postby zpk1234 » Sun Jun 28, 2009 2:58 pm

Fran wrote:I hope that csie-tw doesn't matter that I made a copy & paste for you

thank you Fran, that very kind of you.
i have try this, but the point of the direction is very large if the distance between queried location is far away.
zpk1234
Junior Developer
Junior Developer
 
Posts: 22
Joined: Sat Jun 27, 2009 1:17 pm

Postby smcbride15 » Wed Jul 22, 2009 10:21 pm

That's so useful, thanks a million! Been looking everywhere for how to do this. Thanks again!

Stephen
smcbride15
Freshman
Freshman
 
Posts: 2
Joined: Wed Jul 22, 2009 10:19 pm

Postby myshoes » Thu Sep 03, 2009 5:08 am

thats nice that loteodor can't even say thanks :cry:
myshoes
Junior Developer
Junior Developer
 
Posts: 12
Joined: Mon May 04, 2009 2:07 am

Top

Return to Map Problems

Who is online

Users browsing this forum: No registered users and 2 guests