Handle click on route - android

I'm trying to validate a clickable route functionality using google map API.
I established route display between two coordinates using the map API.
To realize this, I used the following tutorial http://csie-tw.blogspot.com/2009/06/android-driving-direction-route-path.html ,
which basically parse a KML file (the response of a google map direction request) and use a custom map overlay to draw a route.
Custom Overlay :
package com.test.route;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.RectF;
import android.util.Log;
import android.view.MotionEvent;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;
public class MyOverLay extends Overlay
{
private GeoPoint gp1;
private GeoPoint gp2;
private int mRadius=6;
private int mode=0;
private int defaultColor;
private String text="";
private Bitmap img = null;
public MyOverLay(GeoPoint gp1,GeoPoint gp2,int mode) // GeoPoint is a int. (6E)
{
this.gp1 = gp1;
this.gp2 = gp2;
this.mode = mode;
defaultColor = 999; // no defaultColor
}
public MyOverLay(GeoPoint gp1,GeoPoint gp2,int mode, int defaultColor)
{
this.gp1 = gp1;
this.gp2 = gp2;
this.mode = mode;
this.defaultColor = defaultColor;
}
public void setText(String t)
{
this.text = t;
}
public void setBitmap(Bitmap bitmap)
{
this.img = bitmap;
}
public int getMode()
{
return mode;
}
#Override
public boolean draw
(Canvas canvas, MapView mapView, boolean shadow, long when)
{
Projection projection = mapView.getProjection();
if (shadow == false)
{
Paint paint = new Paint();
paint.setAntiAlias(true);
Point point = new Point();
projection.toPixels(gp1, point);
// mode=1¡Gstart
if(mode==1)
{
if(defaultColor==999)
paint.setColor(Color.BLUE);
else
paint.setColor(defaultColor);
RectF oval=new RectF(point.x - mRadius, point.y - mRadius,
point.x + mRadius, point.y + mRadius);
// start point
canvas.drawOval(oval, paint);
}
// mode=2¡Gpath
else if(mode==2)
{
if(defaultColor==999)
paint.setColor(Color.RED);
else
paint.setColor(defaultColor);
Point point2 = new Point();
projection.toPixels(gp2, point2);
paint.setStrokeWidth(5);
paint.setAlpha(120);
canvas.drawLine(point.x, point.y, point2.x,point2.y, paint);
}
/* mode=3¡Gend */
else if(mode==3)
{
/* the last path */
if(defaultColor==999)
paint.setColor(Color.GREEN);
else
paint.setColor(defaultColor);
Point point2 = new Point();
projection.toPixels(gp2, point2);
paint.setStrokeWidth(5);
paint.setAlpha(120);
canvas.drawLine(point.x, point.y, point2.x,point2.y, paint);
RectF oval=new RectF(point2.x - mRadius,point2.y - mRadius,
point2.x + mRadius,point2.y + mRadius);
/* end point */
paint.setAlpha(255);
canvas.drawOval(oval, paint);
}
/* mode=4¡Gcar */
else if(mode==4)
{
if(defaultColor==999)
paint.setColor(Color.GREEN);
else
paint.setColor(defaultColor);
Point point2 = new Point();
projection.toPixels(gp2, point2);
paint.setTextSize(20);
paint.setAntiAlias(true);
canvas.drawBitmap(img, point2.x, point2.y,paint);
canvas.drawText(this.text, point2.x, point2.y, paint);
}
else if(mode==5)
{
if(defaultColor==999)
paint.setColor(Color.GREEN);
else
paint.setColor(defaultColor);
Point point2 = new Point();
projection.toPixels(gp2, point2);
paint.setTextSize(20);
paint.setAntiAlias(true);
canvas.drawBitmap(img, point2.x, point2.y,paint);
}
}
return super.draw(canvas, mapView, shadow, when);
}
#Override
public boolean onTouchEvent(MotionEvent event, MapView mapView)
{
Log.i("Map", "Clicked");
return false;
}
}
Test Map Activity :
package com.test.route;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import com.dailymates.carmate.utils.MyOverLay;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
public class TestMap extends MapActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map);
MapView mapView = (MapView) findViewById(R.id.mapview);
/*
* Defining the starting and end point of the route (latitude / longitude)
* Oh Paris
*
*/
final double src_lat = 48.847378;
final double src_long = 2.340417;
final double dest_lat = 48.931466;
final double dest_long = 2.504525;
GeoPoint srcGeoPoint = new GeoPoint((int) (src_lat * 1E6),
(int) (src_long * 1E6));
GeoPoint destGeoPoint = new GeoPoint((int) (dest_lat * 1E6),
(int) (dest_long * 1E6));
DrawPath(srcGeoPoint, destGeoPoint, Color.GREEN, mapView);
mapView.getController().animateTo(srcGeoPoint);
mapView.getController().setZoom(12);
}
protected boolean isRouteDisplayed() {
return false;
}
private void DrawPath(GeoPoint src, GeoPoint dest, int color, MapView mMapView01) {
StringBuilder urlString = new StringBuilder();
urlString.append("http://maps.google.com/maps?f=d&hl=en");
urlString.append("&saddr=");
urlString.append(Double.toString((double) src.getLatitudeE6() / 1.0E6));
urlString.append(",");
urlString.append(Double.toString((double) src.getLongitudeE6() / 1.0E6));
urlString.append("&daddr=");// to
urlString.append(Double.toString((double) dest.getLatitudeE6() / 1.0E6));
urlString.append(",");
urlString.append(Double.toString((double) dest.getLongitudeE6() / 1.0E6));
urlString.append("&ie=UTF8&0&om=0&output=kml");
Log.d("xxx", "URL=" + urlString.toString());
Document doc = null;
HttpURLConnection urlConnection = null;
URL url = null;
try {
url = new URL(urlString.toString());
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.connect();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
doc = db.parse(urlConnection.getInputStream());
if (doc.getElementsByTagName("GeometryCollection").getLength() > 0) {
String path = doc.getElementsByTagName("GeometryCollection")
.item(0).getFirstChild().getFirstChild()
.getFirstChild().getNodeValue();
Log.d("xxx", "path=" + path);
String[] pairs = path.split(" ");
String[] lngLat = pairs[0].split(",");
// lngLat[0]=longitude
// lngLat[1]=latitude
// lngLat[2]=height
GeoPoint startGP = new GeoPoint((int) (Double.parseDouble(lngLat[1]) * 1E6),
(int) (Double.parseDouble(lngLat[0]) * 1E6));
mMapView01.getOverlays().add(new MyOverLay(startGP, startGP, 1));
GeoPoint gp1;
GeoPoint gp2 = startGP;
for (int i = 1; i < pairs.length; i++)
{
lngLat = pairs[i].split(",");
gp1 = gp2;
gp2 = new GeoPoint(
(int) (Double.parseDouble(lngLat[1]) * 1E6),
(int) (Double.parseDouble(lngLat[0]) * 1E6));
mMapView01.getOverlays().add(
new MyOverLay(gp1, gp2, 2, color));
Log.d("xxx", "pair:" + pairs[i]);
}
mMapView01.getOverlays().add(new MyOverLay(dest, dest, 3));
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
}
}
}
So, as I said, I correctly get a route drawing.
But how could I handle click events on those drawn routes ?
For information, I was thinking about something which would probably be inefficient, but I'll tell it, we never knows :)
Since the route establishement is basicaly a succession of drawings beetween two geopoints (segments), we could prehaps handle route clicking this way :
Using the onTouchEvent method of the map overlay, we can get the geopoint associated to a click.
Then we try to establish if the clicked geopoint is part of a route segment.
One last information : Several routes will be drawn on the map at the same instant, so the click must be associated to a unique route and not all of them.
Thanks.

10 month later, I'm giving the work around I used (10 month ago), who knows, perhaps it will helps someone :).
In order to handle clicks/touch on map routes, one method can be to estimate if the clicked point is located near a displayed route.
Using the OnTouchEvent method, you should calculate if the clicked point is near a segment (a route part).
A route can be represented as a list of segments.
The following method let you calculate the distance between a point and a segment (from Java java.awt.geom.Line2D) :
/**
* Get the closest distance between a point and a segment
*
* #param x1 , x coordinate of the 1st segment point
* #param y1 , y coordinate of the 1st segment point
* #param x2 , x coordinate of the 2st segment point
* #param y2 , y coordinate of the 2st segment point
* #param x , x coordinate of the point from which distance will be calculated
* #param y , y coordinate of the point from which distance will be calculated
*
* #return the distance
*
*/
public static float ptSegDistSq(float x1, float y1, float x2, float y2, float px, float py){
// Adjust vectors relative to x1,y1
// x2,y2 becomes relative vector from x1,y1 to end of segment
x2 -= x1;
y2 -= y1;
// px,py becomes relative vector from x1,y1 to test point
px -= x1;
py -= y1;
float dotprod = px * x2 + py * y2;
float projlenSq;
if (dotprod <= 0.0) {
// px,py is on the side of x1,y1 away from x2,y2
// distance to segment is length of px,py vector
// "length of its (clipped) projection" is now 0.0
projlenSq = (float) 0.0;
} else {
// switch to backwards vectors relative to x2,y2
// x2,y2 are already the negative of x1,y1=>x2,y2
// to get px,py to be the negative of px,py=>x2,y2
// the dot product of two negated vectors is the same
// as the dot product of the two normal vectors
px = x2 - px;
py = y2 - py;
dotprod = px * x2 + py * y2;
if (dotprod <= 0.0) {
// px,py is on the side of x2,y2 away from x1,y1
// distance to segment is length of (backwards) px,py vector
// "length of its (clipped) projection" is now 0.0
projlenSq = (float) 0.0;
} else {
// px,py is between x1,y1 and x2,y2
// dotprod is the length of the px,py vector
// projected on the x2,y2=>x1,y1 vector times the
// length of the x2,y2=>x1,y1 vector
projlenSq = dotprod * dotprod / (x2 * x2 + y2 * y2);
}
}
// Distance to line is now the length of the relative point
// vector minus the length of its projection onto the line
// (which is zero if the projection falls outside the range
// of the line segment).
float lenSq = px * px + py * py - projlenSq;
if (lenSq < 0) {
lenSq = 0;
}
return lenSq;
}
Having this distance, you can establish fixed rules to estimate if the point is near a route, taking in count the map zoom level (at each zoom level correspond a maximum distance tolerance).

Related

MapView display route between a lot of GeoPoints

I have an app where an user is given a lot of points (100 or even more) and he has to physically go to those points and "check in". They have to go to those points in a certain order, so I need to display a route in the MapView which passes through all those points.
I've read a lot about getting the route between two points, but I can't find anything about drawing a complex route with a lot of points. Is this behavior possible?
public class RouteOverlay extends Overlay {
private GeoPoint gp1;
private GeoPoint gp2;
private int color;
public RouteOverlay(GeoPoint gp1, GeoPoint gp2, int color) {
this.gp1 = gp1;
this.gp2 = gp2;
this.color = color;
}
Now all that's left now for our Overlay is to override the draw() method and draw the line as we need it:
#Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
Projection projection = mapView.getProjection();
Paint paint = new Paint();
Point point = new Point();
projection.toPixels(gp1, point);
paint.setColor(color);
Point point2 = new Point();
projection.toPixels(gp2, point2);
paint.setStrokeWidth(5);
paint.setAlpha(120);
canvas.drawLine(point.x, point.y, point2.x, point2.y, paint);
super.draw(canvas, mapView, shadow);
}
Back in the Activity, just iterate over the GeoPoints that you got from google maps and add each of them to the MapView:
private void drawPath(List geoPoints, int color) {
List overlays = mapView.getOverlays();
for (int i = 1; i < geoPoints.size(); i++) {
overlays.add(new RouteOverlay(geoPoints.get(i - 1), geoPoints.get(i), color));
}
}
Try something like this
if(DataSources.ActivitiesList.length >0)
{
String address = "http://maps.google.com/maps?daddr=" + DataSources.ActivitiesList[0].SiteLatitude.toString() + "," + DataSources.ActivitiesList[0].SiteLongitude.toString();
for (int i= 1 ;i < DataSources.ActivitiesList.length ; i++)
{
if(DataSources.ActivitiesList[i].SiteLatitude != null)
address += "+to:" + DataSources.ActivitiesList[i].SiteLatitude + "," + DataSources.ActivitiesList[i].SiteLongitude;
}
Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(address));
startActivity(intent);
break;
}

want to draw a line between any two location in android

This is my main activity
import java.net.HttpURLConnection;
import java.net.URL;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import android.os.Bundle;
import android.util.Log;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
public class DrawActivity extends MapActivity {
MapView myMapView = null;
MapController myMC = null;
GeoPoint geoPoint = null;
String pairs[]=null;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myMapView = (MapView) findViewById(R.id.mapview);
geoPoint = null;
myMapView.setSatellite(false);
System.out.println("before pairs");
String pairs[] = getDirectionData("Hyderabad","hyderguda");
System.out.println("pairs:"+pairs[1]);
String[] lngLat = pairs[0].split(",");
// STARTING POINT
GeoPoint startGP = new GeoPoint(
(int) (Double.parseDouble(lngLat[1]) * 1E6), (int) (Double
.parseDouble(lngLat[0]) * 1E6));
myMC = myMapView.getController();
geoPoint = startGP;
myMC.setCenter(geoPoint);
myMC.setZoom(15);
myMapView.getOverlays().add(new DirectionPathOverlay(startGP, startGP));
// NAVIGATE THE PATH
GeoPoint gp1;
GeoPoint gp2 = startGP;
for (int i = 1; i < pairs.length; i++) {
lngLat = pairs[i].split(",");
gp1 = gp2;
// watch out! For GeoPoint, first:latitude, second:longitude
gp2 = new GeoPoint((int) (Double.parseDouble(lngLat[1]) * 1E6),
(int) (Double.parseDouble(lngLat[0]) * 1E6));
myMapView.getOverlays().add(new DirectionPathOverlay(gp1, gp2));
Log.d("xxx", "pair:" + pairs[i]);
}
// END POINT
myMapView.getOverlays().add(new DirectionPathOverlay(gp2, gp2));
myMapView.getController().animateTo(startGP);
myMapView.setBuiltInZoomControls(true);
myMapView.displayZoomControls(true);
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
private String[] getDirectionData(String srcPlace, String destPlace) {
String urlString = "http://maps.google.com/maps?f=d&hl=en&saddr="
+ srcPlace + "&daddr=" + destPlace
+ "&ie=UTF8&0&om=0&output=kml";
Log.d("URL", urlString);
Document doc = null;
HttpURLConnection urlConnection = null;
URL url = null;
String pathConent = "";
try {
url = new URL(urlString.toString());
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.connect();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
doc = db.parse(urlConnection.getInputStream());
//The above code "doc" is getting null value so thats why its getting crashed here itself not going further execution and returning as `nullpointerexception`
} catch (Exception e) {
}
NodeList nl = doc.getElementsByTagName("LineString");
for (int s = 0; s < nl.getLength(); s++) {
Node rootNode = nl.item(s);
NodeList configItems = rootNode.getChildNodes();
for (int x = 0; x < configItems.getLength(); x++) {
Node lineStringNode = configItems.item(x);
NodeList path = lineStringNode.getChildNodes();
pathConent = path.item(0).getNodeValue();
}
}
String[] tempContent = pathConent.split(" ");
return tempContent;
}
}
and my another part of code
package com.hands;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;
public class DirectionPathOverlay extends Overlay {
private GeoPoint gp1;
private GeoPoint gp2;
public DirectionPathOverlay(GeoPoint gp1, GeoPoint gp2) {
this.gp1 = gp1;
this.gp2 = gp2;
}
#Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
long when) {
// TODO Auto-generated method stub
Projection projection = mapView.getProjection();
if (shadow == false) {
Paint paint = new Paint();
paint.setAntiAlias(true);
Point point = new Point();
projection.toPixels(gp1, point);
paint.setColor(Color.BLUE);
Point point2 = new Point();
projection.toPixels(gp2, point2);
paint.setStrokeWidth(2);
canvas.drawLine((float) point.x, (float) point.y, (float) point2.x,
(float) point2.y, paint);
}
return super.draw(canvas, mapView, shadow, when);
}
#Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
// TODO Auto-generated method stub
super.draw(canvas, mapView, shadow);
}
}
The above is my complete source code plz help me out from this problem
You are using output=kml, this parameter no longer works. You get the error because this URL now retrieves a webpage not KML file.
This way of extracting the Google Directions from Google by parsing the KML file is no longer available since 27 July 2012 (because Google has changed the structure of retrieving Google Directions, now you can only get it by JSON or XML), it is time to migrate your code to JSON instead of KML.
See the answer in my own question here.

Wrong distances when creating random locations

I am trying to create random locations nearby my location. What i want is to create random latitude/longitude pairs inside a 200 meters circle surrounding my location. After asking this question: generate random locations nearby my location this is what i have come up with.
in onLocationChanged, this is what i do:
private void updateWithNewLocation(Location location) {
if (location != null) {
this.myItemizedOverlay.clear();
this.nonPlayerItemizedOverlay.clear();
this.mapOverlays.clear();
// Update my map location.
Double latitude = location.getLatitude() * 1E6;
Double longitude = location.getLongitude() * 1E6;
GeoPoint geoPoint = new GeoPoint(latitude.intValue(),
longitude.intValue());
CustomOverlayItem pcOverlayItem = new CustomOverlayItem(geoPoint,
"", "", "");
this.mapController.animateTo(geoPoint);
this.myItemizedOverlay.setLocation(location);
this.myItemizedOverlay.addOverlay(pcOverlayItem);
this.mapOverlays.add(this.myItemizedOverlay);
// Everytime i get a new location i generate random non player
// characters near my location
int numberOfNonPlayers = new Random().nextInt(5);
for (int i = 0; i < numberOfNonPlayers; i++) {
int radius = (int) this.myMapView.getProjection()
.metersToEquatorPixels(200);
double lowerLimit = -1;
double upperLimit = 1;
Double newLatitude = location.getLatitude()
* 1E6
+ (radius * (lowerLimit + (Math.random() * ((upperLimit - lowerLimit) + 1))));
Double newLongitude = location.getLongitude()
* 1E6
+ (radius * (lowerLimit + (Math.random() * ((upperLimit - lowerLimit) + 1))));
GeoPoint geoPoint2 = new GeoPoint(newLatitude.intValue(),
newLongitude.intValue());
CustomOverlayItem npcOverlayItem = new CustomOverlayItem(
geoPoint2, "", "", "");
Location newLocation = new Location("npc " + i);
newLocation.setLatitude(newLatitude);
newLocation.setLongitude(newLongitude);
this.nonPlayerItemizedOverlay = new NonPlayerItemizedOverlay(
this.nonPlayerDrawable, this.myMapView);
this.nonPlayerItemizedOverlay.setLocation(newLocation);
this.nonPlayerItemizedOverlay.addOverlay(npcOverlayItem);
this.mapOverlays.add(this.nonPlayerItemizedOverlay);
}
}
}
And this is my NonPlayerItemizedOverlay class:
public class NonPlayerItemizedOverlay extends BaseItemizedOverlay {
public NonPlayerItemizedOverlay(Drawable defaultMarker, MapView mapView) {
super(defaultMarker, mapView);
// TODO Auto-generated constructor stub
}
private Location location;
public static int metersToRadius(float meters, MapView map, double latitude) {
return (int) (map.getProjection().metersToEquatorPixels(meters) * (1 / Math
.cos(Math.toRadians(latitude))));
}
#Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
super.draw(canvas, mapView, shadow);
if (shadow == false) {
Projection projection = mapView.getProjection();
// Get the current location
Double latitude = location.getLatitude();
Double longitude = location.getLongitude();
GeoPoint geoPoint = new GeoPoint(latitude.intValue(),
longitude.intValue());
// Convert the location to screen pixels
Point point = new Point();
projection.toPixels(geoPoint, point);
// int radius = metersToRadius(30, mapView, latitude);
int radius = (int) mapView.getProjection()
.metersToEquatorPixels(50);
RectF oval = new RectF(point.x - radius, point.y - radius, point.x
+ radius, point.y + radius);
// Setup the paint
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setStrokeWidth(2.0f);
paint.setColor(0xffE62020);
paint.setStyle(Style.STROKE);
canvas.drawOval(oval, paint);
paint.setColor(0x18E62020);
paint.setStyle(Style.FILL);
canvas.drawOval(oval, paint);
}
}
public Location getLocation() {
return location;
}
public void setLocation(Location location) {
this.location = location;
}
And my MyItemizedOverlay class:
public class MyItemizedOverlay extends BaseItemizedOverlay {
public MyItemizedOverlay(Drawable defaultMarker, MapView mapView) {
super(defaultMarker, mapView);
// TODO Auto-generated constructor stub
}
private Location location;
public static int metersToRadius(float meters, MapView map, double latitude) {
return (int) (map.getProjection().metersToEquatorPixels(meters) * (1 / Math
.cos(Math.toRadians(latitude))));
}
#Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
super.draw(canvas, mapView, shadow);
if (shadow == false) {
Projection projection = mapView.getProjection();
// Get the current location
Double latitude = location.getLatitude() * 1E6;
Double longitude = location.getLongitude() * 1E6;
GeoPoint geoPoint = new GeoPoint(latitude.intValue(),
longitude.intValue());
// Convert the location to screen pixels
Point point = new Point();
projection.toPixels(geoPoint, point);
// int radius = metersToRadius(100, mapView, latitude);
int radius = (int) mapView.getProjection().metersToEquatorPixels(
200);
RectF oval = new RectF(point.x - radius, point.y - radius, point.x
+ radius, point.y + radius);
// Setup the paint
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setStrokeWidth(2.0f);
paint.setColor(0xff6666ff);
paint.setStyle(Style.STROKE);
canvas.drawOval(oval, paint);
paint.setColor(0x186666ff);
paint.setStyle(Style.FILL);
canvas.drawOval(oval, paint);
}
}
public Location getLocation() {
return location;
}
public void setLocation(Location location) {
this.location = location;
}
The thing is that something weird is happening because all the random locations are too near of my location center, it seems that the formula does not cover the whole radius and i think that distances are not in real meters.
Any idea of what could be wrong with my formula?
Thanks in advance
For questions regarding map projections and coordinate calculations, this SE site could be helpful: http://gis.stackexchange.com (Geographic Information Systems).
To be sure use the distanceTo() formula of location class.You can do it this way:
double distance = a.distanceTo(b);
where a and b are Location objects.

Google Maps Api Directions

Can i mark two points on the google maps in my application and then calculate the direction on the map as well as turn by turn navigation ?
I am create an application which are used to get the driving direction between two points in the map
Step be step
step1 First create a class MyOverlay.java which are used to draw a path
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.RectF;
import android.util.Log;
//import android.util.Log;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;
public class MyOverLay extends Overlay
{
private GeoPoint gp1;
private GeoPoint gp2;
private int mRadius=6;
private int mode=0;
private int defaultColor;
private String text="";
private Bitmap img = null;
public MyOverLay(GeoPoint gp1,GeoPoint gp2,int mode) // GeoPoint is a int. (6E)
{
this.gp1 = gp1;
this.gp2 = gp2;
this.mode = mode;
defaultColor = 999; // no defaultColor
}
public MyOverLay(GeoPoint gp1,GeoPoint gp2,int mode, int defaultColor)
{
this.gp1 = gp1;
this.gp2 = gp2;
this.mode = mode;
this.defaultColor = defaultColor;
}
public void setText(String t)
{
this.text = t;
}
public void setBitmap(Bitmap bitmap)
{
this.img = bitmap;
}
public int getMode()
{
return mode;
}
#Override
public boolean draw
(Canvas canvas, MapView mapView, boolean shadow, long when)
{
Projection projection = mapView.getProjection();
if (shadow == false)
{
Paint paint = new Paint();
paint.setAntiAlias(true);
Point point = new Point();
projection.toPixels(gp1, point);
// mode=1¡Gstart
if(mode==1)
{
if(defaultColor==999)
paint.setColor(Color.BLUE);
else
paint.setColor(defaultColor);
RectF oval=new RectF(point.x - mRadius, point.y - mRadius,
point.x + mRadius, point.y + mRadius);
// start point
canvas.drawOval(oval, paint);
}
// mode=2¡Gpath
else if(mode==2)
{
if(defaultColor==999)
paint.setColor(Color.RED);
else
paint.setColor(defaultColor);
Point point2 = new Point();
projection.toPixels(gp2, point2);
paint.setStrokeWidth(5);
paint.setAlpha(120);
canvas.drawLine(point.x, point.y, point2.x,point2.y, paint);
}
/* mode=3¡Gend */
else if(mode==3)
{
/* the last path */
if(defaultColor==999)
paint.setColor(Color.GREEN);
else
paint.setColor(defaultColor);
Point point2 = new Point();
projection.toPixels(gp2, point2);
paint.setStrokeWidth(5);
paint.setAlpha(120);
canvas.drawLine(point.x, point.y, point2.x,point2.y, paint);
RectF oval=new RectF(point2.x - mRadius,point2.y - mRadius,
point2.x + mRadius,point2.y + mRadius);
/* end point */
paint.setAlpha(255);
canvas.drawOval(oval, paint);
}
/* mode=4¡Gcar */
else if(mode==4)
{
if(defaultColor==999)
paint.setColor(Color.GREEN);
else
paint.setColor(defaultColor);
Point point2 = new Point();
projection.toPixels(gp2, point2);
paint.setTextSize(20);
paint.setAntiAlias(true);
canvas.drawBitmap(img, point2.x, point2.y,paint);
canvas.drawText(this.text, point2.x, point2.y, paint);
// Log.d(TAG, "Draw the text="+this.text+ " at point="+point2.x + "," + point2.y);
}
else if(mode==5)
{
if(defaultColor==999)
paint.setColor(Color.GREEN);
else
paint.setColor(defaultColor);
Point point2 = new Point();
projection.toPixels(gp2, point2);
paint.setTextSize(20);
paint.setAntiAlias(true);
canvas.drawBitmap(img, point2.x, point2.y,paint);
// Log.d(TAG, "Draw the text="+this.text+ " at point="+point2.x + "," + point2.y);
}
}
return super.draw(canvas, mapView, shadow, when);
}
}
step 2: Create a class name DrivingDirectionActivity.java
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import com.android.code.R;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
/**
* #author ashish
*
*
*
*/
public class DrivingDirectionActivity extends MapActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.google_map_driving_direction_view);
MapView mapView = (MapView) findViewById(R.id.myMapView1);
double src_lat = 28.632808;
double src_long = 77.218276;
double dest_lat = 28.588535;
double dest_long = 77.255130;
GeoPoint srcGeoPoint = new GeoPoint((int) (src_lat * 1E6),
(int) (src_long * 1E6));
GeoPoint destGeoPoint = new GeoPoint((int) (dest_lat * 1E6),
(int) (dest_long * 1E6));
DrawPath(srcGeoPoint, destGeoPoint, Color.GREEN, mapView);
mapView.getController().animateTo(srcGeoPoint);
mapView.getController().setZoom(15);
}
/* (non-Javadoc)
* #see com.google.android.maps.MapActivity#isRouteDisplayed()
*/
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
private void DrawPath(GeoPoint src, GeoPoint dest, int color,MapView mMapView01) {
// connect to map web service
Document doc = getDocument(makeUrl(src,dest));
System.out.println(doc.toString());
try {
if (doc.getElementsByTagName("GeometryCollection").getLength() > 0) {
// String path =
// doc.getElementsByTagName("GeometryCollection").item(0).getFirstChild().getFirstChild().getNodeName();
String path = doc.getElementsByTagName("GeometryCollection")
.item(0).getFirstChild().getFirstChild()
.getFirstChild().getNodeValue();
Log.d("xxx", "path=" + path);
String[] pairs = path.split(" ");
String[] lngLat = pairs[0].split(","); // lngLat[0]=longitude
// lngLat[1]=latitude
// lngLat[2]=height
// src
GeoPoint startGP = new GeoPoint((int) (Double.parseDouble(lngLat[1]) * 1E6),
(int) (Double.parseDouble(lngLat[0]) * 1E6));
mMapView01.getOverlays().add(new MyOverLay(startGP, startGP, 1));
GeoPoint gp1;
GeoPoint gp2 = startGP;
for (int i = 1; i < pairs.length; i++) {
// the last one would be crash
lngLat = pairs[i].split(",");
gp1 = gp2;
// watch out! For GeoPoint, first:latitude, second:longitude
gp2 = new GeoPoint((int) (Double.parseDouble(lngLat[1]) * 1E6),
(int) (Double.parseDouble(lngLat[0]) * 1E6));
mMapView01.getOverlays().add(new MyOverLay(gp1, gp2, 2, color));
Log.d("xxx", "pair:" + pairs[i]);
}
mMapView01.getOverlays().add(new MyOverLay(dest, dest, 3)); // use
// the
// default
// color
}
} catch ( Exception e) {
e.printStackTrace();
}
}
private Document getDocument(String urlString) {
// TODO Auto-generated method stub
Document doc = null;
HttpURLConnection urlConnection = null;
URL url = null;
try {
url = new URL(urlString.toString());
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.connect();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
doc = db.parse(urlConnection.getInputStream());
} catch (Exception e) {
// TODO: handle exception
}
return doc;
}
private String makeUrl(GeoPoint src, GeoPoint dest) {
// TODO Auto-generated method stub
// get the kml (XML) doc. And parse it to get the coordinates(direction
// route).
StringBuilder urlString = new StringBuilder();
urlString.append("http://maps.google.com/maps?f=d&hl=en");
urlString.append("&saddr=");// from
urlString.append(Double.toString((double) src.getLatitudeE6() / 1.0E6));
urlString.append(",");
urlString.append(Double.toString((double) src.getLongitudeE6() / 1.0E6));
urlString.append("&daddr=");// to
urlString.append(Double.toString((double) dest.getLatitudeE6() / 1.0E6));
urlString.append(",");
urlString.append(Double.toString((double) dest.getLongitudeE6() / 1.0E6));
urlString.append("&ie=UTF8&0&om=0&output=kml");
Log.d("xxx", "URL=" + urlString.toString());
return urlString.toString();
}
}
now execute..
If this is not good then you can pass the your current location as source loaction
No the Map Activity doesn't provide such a feature. You have to request it via the Google Maps Web API.

Android MapView Projection returning negative numbers

I'm using the below code to put an overlay on my MapView. For some reason after itterating through the location data it draws the point in the same place.
It seems to be happening on conversion from Location to GeoPoint to Projection:
Location (lat : lon) 51.2651789188385 : -0.5398589372634888
Projection (x : y) 239 : 361
Location (lat : lon) 51.26375198364258 : -0.5417096614837646
Projection (x : y) 239 : 361
Locations are Doubles
GeoPoints are Integers
Projections are screen coordinates.
Could someone please look over the below code and find if i am doing something wrong?
thanks
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.RectF;
import android.location.Location;
import android.util.Log;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;
public class ROverlay extends Overlay {
private Context context;
private HashMap<String, Location> friendLocations;
private ArrayList<LocationData> locD;
private Location location;
private GeoPoint locationPoint;
private Paint paint;
private Paint backPaint;
private static int markerRadius = 7;
/** Get your current location */
public Location getLocation() {
return location;
}
/** Set your current location */
public void setLocation(Location location) {
this.location = location;
Double latitude = location.getLatitude()*1E6;
Double longitude = location.getLongitude()*1E6;
locationPoint = new GeoPoint(latitude.intValue(),longitude.intValue());
}
/** Refresh the locations of each of the contacts */
public void refreshLocation() {
locD = RMapView.getARR();
}
/**
* Create a new FriendLocationOverlay to show your contact's locations on a map
* #param _context Parent application context
*/
public ROverlay(Context _context) {
super();
context = _context;
friendLocations = new HashMap<String, Location>();
locD = new ArrayList<LocationData>();
//refreshFriendLocations();
// Create the paint objects
backPaint = new Paint();
backPaint.setARGB(200, 200, 200, 200);
backPaint.setAntiAlias(true);
paint = new Paint();
paint.setDither(true);
paint.setColor(Color.RED);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeCap(Paint.Cap.ROUND);
paint.setStrokeWidth(5);
}
#Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
// Get the map projection to convert lat/long to screen coordinates
Projection projection = mapView.getProjection();
for (int q = 1; q < locD.size(); q++){
Double latitude = locD.get(q-1).mLocation.getLatitude()*1e6;
Double longitude = locD.get(q-1).mLocation.getLongitude()*1e6;
GeoPoint geopoint = new GeoPoint(latitude.intValue(),longitude.intValue());
//Log.d("Double", ""+latitude+" : "+longitude);
//Log.d("Geopoint", ""+geopoint.getLatitudeE6()+" : "+geopoint.getLongitudeE6());
Point point = new Point();
projection.toPixels(geopoint, point);
Double latitude2 = locD.get(q).mLocation.getLatitude()*1e6;
Double longitude2 = locD.get(q).mLocation.getLongitude()*1e6;
GeoPoint geopoint2 = new GeoPoint(latitude2.intValue(),longitude2.intValue());
Point point2 = new Point();
projection.toPixels(geopoint2, point2);
canvas.drawLine(point.x, point.y, point2.x, point2.y, paint);
canvas.drawPoint(point.x, point.y, paint);
Log.d("Point", ""+point.x+" : "+point.y);
}
super.draw(canvas, mapView, shadow);
}
#Override
public boolean onTap(GeoPoint point, MapView mapView) {
// Do not react to screen taps.
return false;
}
}
</code>
I don't know what the problem is with this one. I couldn't get it working and the debug was showing no errors so in the end i used code from the Google Mytracks project to enable the map view and projection problems
http://code.google.com/p/mytracks/
You should look at this The projection value to pixel may vary.
as http://code.google.com/android/add-ons/google-apis/reference/com/google/android/maps/MapView.html#getProjection%28%29 says that "The Projection of the map in its current state. You should not hold on to this object for more than one draw, since the projection of the map could change."
Instead of writing
projection.toPixels(geopoint2, point2);
you must code as
mapView.getProjection().toPixels(geopoint2, point2);

Categories

Resources