Puis-je marquer deux points sur les cartes Google dans mon application, puis calculer la direction sur la carte ainsi que la navigation tour à tour?Google Maps Api Directions
Répondre
Non, l'activité de la carte ne fournit pas une telle fonctionnalité. Vous devez le demander via l'API Web Google Maps.
Je suis créer une application qui permet d'obtenir la direction d'entraînement entre deux points sur la carte
Étape être pas
Première étape 1 créer une classe MyOverlay.java qui sont utilisé pour tracer un chemin
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);
}
}
étape 2: créer un nom de classe 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();
}
}
exécuter maintenant ..
Si cela est bon, alors vous pouvez passer la votre position actuelle en tant que source loaction
J'ai suivi votre code et j'ai reçu une exception dans 'getDocument (...)' méthode à la ligne 'doc = db.parse (urlConnection.getInputStream());'. Le message d'exception est 'entité non terminée ref (position: ENTITY_REF & @ 1: 796 dans [email protected])'. Une idée? –
Il semble que cette méthode ne marche plus. La réponse de cette URL est désormais une page google maps html. – frankli
- 1. Google Maps JavaScript API v3 directions capacités
- 2. google maps api directions javascript variables
- 3. flash google maps api directions échec 400
- 4. erreur Google API Directions
- 5. google maps directions opacité
- 6. Google Maps Directions Only
- 7. API Google Maps [API Directions] Limitation des waypoints?
- 8. Routes suggérées - API Google Directions
- 9. Google Directions API
- 10. Google Directions API + URL Link
- 11. directions Google Maps format texte
- 12. google maps api, les directions de 2 points
- 13. Google Maps API v3 - marqueurs de directions personnalisées
- 14. Problèmes avec les directions API Google Maps V3 et jQuery
- 15. Erreur lors de l'utilisation de directions api Google Maps
- 16. iOS API Google Maps directions utilisées dans le code
- 17. comment ajouter google Directions dans google maps
- 18. Google API V3 et les directions (Javascript)
- 19. API Google Directions accessible via Java
- 20. Google Directions API limite de requête
- 21. Effacer itinéraire dans des directions Google Maps
- 22. Google Maps - Ajouter deux marqueurs (directions)
- 23. google maps ouvertes aux directions de bus
- 24. Android Google Maps Directions sur MapView
- 25. Google directions sur OpenLayers
- 26. Itinéraire distance dans Google Maps API v3
- 27. Google Maps API V3: passer la coordonnée du marqueur à la demande de service google directions
- 28. API Google Maps (Google Directions) ne fonctionnant pas pour un emplacement particulier
- 29. API Google Maps - ajout de plusieurs destinations ne fonctionnant pas (directions google)
- 30. Enregistrement de l'instruction Google Directions API v3
ok. donc je dois utiliser l'API Web et m'assurer du fichier KML et du rendu sur la carte? –
vous avez une idée de comment analyser le fichier KML? –
http://stackoverflow.com/questions/2023669/j2me-android-blackberry-driving-directions-route-between-two-locations – Flo