29 messages in com.googlegroups.android-developersRe: MapView with driving directions| From | Sent On | Attachments |
|---|---|---|
| macpuddin | 29 Nov 2007 01:41 | |
| Seb | 29 Nov 2007 03:46 | |
| gpsmobiler | 29 Nov 2007 04:18 | |
| macpuddin | 29 Nov 2007 07:36 | |
| neil | 29 Nov 2007 08:35 | |
| Bob Snopes | 29 Nov 2007 23:30 | |
| ceph...@gmail.com | 29 Nov 2007 23:54 | |
| neil | 30 Nov 2007 00:31 | |
| ceph...@gmail.com | 01 Dec 2007 22:48 | |
| ceph...@gmail.com | 02 Dec 2007 12:33 | |
| neil | 02 Dec 2007 14:13 | |
| ceph...@gmail.com | 02 Dec 2007 15:28 | |
| macpuddin | 03 Dec 2007 16:59 | |
| macpuddin | 12 Dec 2007 23:38 | |
| neil | 13 Dec 2007 11:46 | |
| Cow Bay | 13 Dec 2007 22:06 | |
| macpuddin | 18 Dec 2007 00:34 | |
| neil | 18 Dec 2007 04:23 | |
| macpuddin | 18 Dec 2007 08:11 | |
| neil | 18 Dec 2007 09:34 | |
| tga...@gmail.com | 29 Dec 2007 01:28 | |
| tga...@gmail.com | 29 Dec 2007 02:26 | |
| tga...@gmail.com | 30 Dec 2007 14:20 | |
| ceph...@gmail.com | 30 Dec 2007 14:25 | |
| tga...@gmail.com | 30 Dec 2007 15:49 | |
| cocaIce | 28 Jan 2008 06:13 | |
| neil | 28 Jan 2008 23:42 | |
| neil | 30 Jan 2008 00:43 | |
| neil | 31 Jan 2008 08:49 |
| Subject: | Re: MapView with driving directions![]() |
|---|---|
| From: | macpuddin (mcpu...@gmail.com) |
| Date: | 12/18/2007 12:34:24 AM |
| List: | com.googlegroups.android-developers |
Hey Neil,
Very good point, I totally don't want to go through googlenavapis at all! Question though how are you sending the request and retrieving the result? You say:
1) Returned HTTP status must be 200 OK 2) The value of Status within the returned JSON must be 200
This is done in a "3 liner" and straight forward. Nothing special required. Check it out with your browser first.
But how?
On Dec 13, 11:47 am, neil <neil...@freenet.de> wrote:
If it is not too complicated you may ask Google directly, e.g. by attempting to obtain the geocode for a given street address.
http://maps.google.com/maps/geo?output=json&oe=utf-8&q=<your_query>&key=<your_google_key>
Substitue <your_query> with a street address and <your_google_key> with a valid key obtained from Google, send this as plain HTTP request and parse the returned JSON. Because you are not primarily interested in the geocode itself you may check the validness in a two step approach
1) Returned HTTP status must be 200 OK 2) The value of Status within the returned JSON must be 200
This is done in a "3 liner" and straight forward. Nothing special required. Check it out with your browser first.
For example for the city of Berlin/Germany, where I live, the result for <your_query> = "berlin" and a valid key provided is:
{"name":"berlin","Status":{"code":200,"request":"geocode"},"Placemark": [{"id":"p1","address":"Berlin, Germany","AddressDetails":{"Country": {"CountryNameCode":"DE","AdministrativeArea": {"AdministrativeAreaName":"Berlin","SubAdministrativeArea": {"SubAdministrativeAreaName":"Berlin","Locality": {"LocalityName":"Berlin"}}}},"Accuracy": 4},"Point":{"coordinates": [13.411494,52.523480,0]}}]}
AFAIK there is a JSON deserializer, which should do the job to provide you with the objects contained.
But probably this could be done even simpler :)
Regards
On 13 Dez., 08:38, macpuddin <mcpu...@gmail.com> wrote:
So I gave cephdon's code a whirl like I said, and it worked great! You did all the hard work! Thanks!
I do have a quesiton now regarding this line of code:
getDispatcher().addDataRequest(mDD);
It seems like you use this to start sending out mDD to find the directions from the net. Unfortunately getDispatcher is only defined for MapActivity. Does anyone know of a way to get add this data request for mDD to find directions without using getDispatcher? For example, I'm trying to just use googlenav to tell me if an address is valid or not (if it can find it there will be a starting route)
Thanks! James
On Dec 3, 4:59 pm, macpuddin <mcpu...@gmail.com> wrote:
Ah thats they key:
import com.google.googlenav.DrivingDirection;
I will give it a whirl!
On Dec 2, 2:13 pm, neil <neil...@freenet.de> wrote:
Hmm. A bit hard to follow you. Would you mind to publish the source? Maybe this would make discussions easier.
Regards
Well, it appears that I can't attach files to this discussion.. suffice it to say here is an overlay that will do the job.. Part of the problem is that it doesn't seem to maintain my code formatting.. lets see if the html tag will work...
<code>
method in MyMapView to get the directions // if you pass the start_location as an address or location name, then the DrivingDirection object will ignore the start_pos object. // you must pass a null string ( "" ) into either start_location or end_location to make the start_pos (or end_pos relative to the pos object you are using) count. private void startFetchDirections(MapPoint start_pos, String start_location, MapPoint end_pos, String end_location) { // get directions // mDD is a class variable for the activity that will hold an instance of the DrivingDirection object created here. mDD = new DrivingDirection(start_pos,start_location,end_pos,end_location); if(mDD != null) { // add the request the dispatcher getDispatcher().addDataRequest(mDD);
Thread t = new Thread(new Runnable() {
public void run()
{
// wait for the search to be
complete...
while (!mDD.isComplete()) {}
// check to see if any
Placemarks were found.. if 0 then there is no route...
if (mDD.numPlacemarks() > 0)
{
// set a flag to let the program
know the
directions are done...
// this flag is created
in your main activity and just set here to let it know..
FoundDirections = true;
}
else // no route..
{
// let the user know
that no route was found...
}
}
});
t.start();
}
}
}
// this is defined in MyMapView to allow the overlay to have access to the directions it will be rendering.. // you could also just have returned the route of MapPoint[] if you wanted since thats what we will be drawing. public DrivingDirection getDrivingDirections() { return mDD; }
// overlay file.... package your.package.name;
import android.graphics.Canvas; import android.graphics.Paint;
import com.google.android.maps.Overlay; import com.google.android.maps.Point; import com.google.googlenav.DrivingDirection; import com.google.googlenav.map.MapPoint;
public class MapDrivingDirectionOverlay extends Overlay { MyMapView mMap; Paint paint1 = new Paint(); DrivingDirection dd = null;
public MapDrivingDirectionOverlay(MyMapView map) { mMap = map; }
public void draw(Canvas canvas, PixelCalculator pixelCalculator, boolean b) { super.draw(canvas, pixelCalculator, b); // holders of mapped coords... int[] screenCoords = new int[2]; int[] screenCoords2 = new int[2];
// method in the custom map view to return the DrivingDirection object. dd = mMap.getDrivingDirections(); if(dd != null) { // these methods are to illustrate some of what you can get out of the system... MapPoint s = dd.getRouteStart(); String name = dd.getRouteStartLocation(); String distance = dd.getFormattedDistance(); String time = dd.getFormattedTime(); String info = dd.getRouteInfoDescriptor(); MapPoint e = dd.getRouteEnd();
// draw and label our start and end points. Point spoint = new Point(s.getLatitude(),s.getLongitude()); pixelCalculator.getPointXY(spoint, screenCoords); paint1.setARGB(255,0,0,0); canvas.drawCircle(screenCoords[0], screenCoords[1], 6, paint1); canvas.drawText("Start",screenCoords[0] - 6,screenCoords[1] + 6, paint1); Point epoint = new Point(e.getLatitude(),e.getLongitude()); pixelCalculator.getPointXY(epoint, screenCoords); canvas.drawText("End",screenCoords[0] - 6,screenCoords[1] + 6, paint1);
// now we will draw the route.. // check to see if the route is too long. // I dont know what this does.. but it seemed like a good idea. if(!dd.routeTooLong()) { // get the route points... MapPoint[] route = dd.getRoute(); // loop over them... for(int i = 0;i<route.length-1;i++) { // construct points out of them for the pixelCalculator... Point start = new Point(route[i].getLatitude(),route[i].getLongitude()); Point end = new Point(route[i+1].getLatitude(),route[i +1].getLongitude()); pixelCalculator.getPointXY(start, screenCoords); pixelCalculator.getPointXY(end, screenCoords2); // actually draw the line. canvas.drawLine(screenCoords[0], screenCoords[1], screenCoords2[0], screenCoords2[1], paint1); } } } }}
</code>
Thats all their is to it... its really that simple. I am sure there is a better way.. and placing this in an overlay has the side effect of slowing down all rendering on the entire map because the overlay gets drawn every time. It would be nice to somehow save the overlay image and just render it when needed.. like the zoom level changed or something. However, I have not investigated if thats even possible or not.
This does work however and it draws lines exactly on the roads. For each turn or change in direction on the map there is another Placemark generated. Drawing lines between them with your own width and color will place them on the directly over the
...




