简体   繁体   English

应用内的Android Google Maps路线和道路

[英]Android Google Maps directions and road path inside app

I'm searching for a few hours right now, and i can't seem to reach any solution. 我现在正在搜索几个小时,但似乎找不到任何解决方案。 I am developing an app that displays some reference points in a map. 我正在开发一个在地图上显示一些参考点的应用程序。 I want to draw the road path between these points, and show the respective directions inside my app. 我想绘制这些点之间的道路,并在我的应用程序中显示相应的方向。 I don't want to launch google maps app to show those informations there. 我不想启动Google Maps应用程序以在此处显示这些信息。

Is this possible? 这可能吗? If yes, can you point some tutorials? 如果是,您可以指出一些教程吗?

Thks in advance. 提前。

KML Output is not working anymore on google maps. KML输出在Google地图上不再起作用。 You must use the directions api to get routes from google. 您必须使用Directions api才能从Google获取路线。

http://maps.googleapis.com/maps/api/directions/xml?origin= {lat_lon_origin}&destination={lat_lon_dest}&ie=UTF8&oe=UTF8&0&om=0&sensor=false http://maps.googleapis.com/maps/api/directions/xml?origin= {lat_lon_origin}&destination = {lat_lon_dest}&ie = UTF8&oe = UTF8&0&om = 0&sensor = false

See this post for infos: 看到这篇文章的信息:

Google Maps API Version difference Google Maps API版本差异

Sure this is possible. 当然可以。 There are enough tutorials on the net about this. 网上有足够的有关此的教程。 One search request gives me the following results: 一个搜索请求给我以下结果:

Have a look at API demos at the Android Developer site : 在Android Developer网站上查看API演示:

http://developer.android.com/resources/tutorials/views/hello-mapview.html http://developer.android.com/resources/tutorials/views/hello-mapview.html

for a starting point for implementing a MapView. 作为实现MapView的起点。

In this stack overflow thread, your can find an answer on your question about drawing route on map: 在此堆栈溢出线程中,您可以找到有关在地图上绘制路线的问题的答案:

How to display a route between two geocoords in google maps? 如何显示谷歌地图中两个地理坐标之间的路线?

Tip: Search first on the Android Developers site and on stack-overflow before asking a question. 提示:在提出问题之前,请先在Android Developers网站和堆栈溢出上进行搜索。

Have nice development! 有不错的发展!

Kr

I have done walking and driving directions with drawing path on map. 我已经完成了在地图上绘制路径的步行和行车路线。 For that i use 2 locations.; 为此,我使用2个位置。 1)current location lat and lon 2) user click on marker to show path between point 1 and 2. for that you just need to get lat and long of clicked marker point than my code is useful. 1)当前位置lat和lon 2)用户单击标记以显示点1和点2之间的路径。为此,您只需获取lat和单击的标记点的长度,这比我的代码有用。

    drawPath(Utils.ConvertToFloat(kiosk.Lat),
    Utils.ConvertToFloat(kiosk.Lon), true);

Function is: 功能是:

private void drawPath(final double lat, final double lon,
        final boolean isWalking) {
    new Thread() {
        @Override
        public void run() {
            double fromLat = 0, fromLon = 0, toLat, toLon;
            if (curLocation != null) {
                fromLat = curLocation.getLatitude();
                fromLon = curLocation.getLongitude();
            }
            toLat = lat;
            toLon = lon;
            // For Testing
            // fromLat = 36.00;
            // fromLon = -115.16116333007813;
            String url = RoadProvider.getUrl(fromLat, fromLon, toLat,
                    toLon, isWalking);
            InputStream is = getConnection(url);
            mRoad = RoadProvider.getRoute(is);
            mHandler.sendEmptyMessage(0);
        }
    }.start();
}
   Handler mHandler = new Handler() {
    public void handleMessage(android.os.Message msg) {

        MapOverlay mapOverlay = new MapOverlay(mRoad, mapView);
        List<Overlay> listOfOverlays = mapView.getOverlays();
        ArrayList<Overlay> objs = new ArrayList<Overlay>();

        for (Overlay o : listOfOverlays) {
            if (o instanceof MapOverlay) {
                objs.add(o);
            }
        }
        for (Overlay overlay : objs) {
            listOfOverlays.remove(overlay);
        }
        // listOfOverlays.clear();
        listOfOverlays.add(mapOverlay);
        mapView.invalidate();
    };
};

here is where path is drawn:: 这是绘制路径的位置::

class MapOverlay extends com.google.android.maps.Overlay {
    Road mRoad;
    ArrayList<GeoPoint> mPoints;

    public MapOverlay(Road road, MapView mv) {
        mRoad = road;
        if (road.mRoute.length > 0) {
            mPoints = new ArrayList<GeoPoint>();
            for (int i = 0; i < road.mRoute.length; i++) {
                mPoints.add(new GeoPoint(
                        (int) (road.mRoute[i][1] * 1000000),
                        (int) (road.mRoute[i][0] * 1000000)));
            }
            int moveToLat = (mPoints.get(0).getLatitudeE6() + (mPoints.get(
                    mPoints.size() - 1).getLatitudeE6() - mPoints.get(0)
                    .getLatitudeE6()) / 2);
            int moveToLong = (mPoints.get(0).getLongitudeE6() + (mPoints
                    .get(mPoints.size() - 1).getLongitudeE6() - mPoints
                    .get(0).getLongitudeE6()) / 2);
            GeoPoint moveTo = new GeoPoint(moveToLat, moveToLong);

            MapController mapController = mv.getController();
            mapController.animateTo(moveTo);
            mapController.setZoom(13);
        }
    }

    @Override
    public boolean draw(Canvas canvas, MapView mv, boolean shadow, long when) {
        super.draw(canvas, mv, shadow);
        drawPath(mv, canvas);
        return true;
    }

    public void drawPath(MapView mv, Canvas canvas) {
        int x1 = -1, y1 = -1, x2 = -1, y2 = -1;
        Paint paint = new Paint();
        paint.setColor(Color.GREEN);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(3);
        if (mPoints != null && mPoints.size() > 0) {

            for (int i = 0; i < mPoints.size(); i++) {
                Point point = new Point();
                mv.getProjection().toPixels(mPoints.get(i), point);
                x2 = point.x;
                y2 = point.y;
                if (i > 0) {
                    canvas.drawLine(x1, y1, x2, y2, paint);
                }
                x1 = x2;
                y1 = y2;
            }
        }
    }
}

You need these 3 files in same or different package. 您需要将这3个文件放在相同或不同的软件包中。

point.java point.java

public class Point {
String mName;
String mDescription;
String mIconUrl;
double mLatitude;
double mLongitude;

} }

Road.java Road.java

public class Road {
public String mName;
public String mDescription;
public int mColor;
public int mWidth;
public double[][] mRoute = new double[][] {};
public Point[] mPoints = new Point[] {};

} }

RoadProvider.java RoadProvider.java

  public class RoadProvider {

    public static Road getRoute(InputStream is) {
            KMLHandler handler = new KMLHandler();
            try {
                    SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
                    parser.parse(is, handler);
            } catch (ParserConfigurationException e) {
                    e.printStackTrace();
            } catch (SAXException e) {
                    e.printStackTrace();
            } catch (IOException e) {
                    e.printStackTrace();
            }
            return handler.mRoad;
    }

    public static String getUrl(double fromLat, double fromLon, double toLat,
                    double toLon, boolean isWalking) {// connect to map web service
            StringBuffer urlString = new StringBuffer();
            urlString.append("http://maps.google.com/maps?f=d&hl=en");
            urlString.append("&saddr=");// from
            urlString.append(Double.toString(fromLat));
            urlString.append(",");
            urlString.append(Double.toString(fromLon));
            urlString.append("&daddr=");// to
            urlString.append(Double.toString(toLat));
            urlString.append(",");
            urlString.append(Double.toString(toLon));
            urlString.append("&ie=UTF8&0&om=0&output=kml");
            if(isWalking){
                urlString.append("&dirflg=w");
            }
            Utils.LogInfo(urlString.toString());
            return urlString.toString();
    }
    }

    class KMLHandler extends DefaultHandler {
    Road mRoad;
    boolean isPlacemark;
    boolean isRoute;
    boolean isItemIcon;
    private Stack mCurrentElement = new Stack();
    private String mString;

    public KMLHandler() {
            mRoad = new Road();
    }

    public void startElement(String uri, String localName, String name,
                    Attributes attributes) throws SAXException {
            mCurrentElement.push(localName);
            if (localName.equalsIgnoreCase("Placemark")) {
                    isPlacemark = true;
                    mRoad.mPoints = addPoint(mRoad.mPoints);
            } else if (localName.equalsIgnoreCase("ItemIcon")) {
                    if (isPlacemark)
                            isItemIcon = true;
            }
            mString = new String();
    }

    public void characters(char[] ch, int start, int length)
                    throws SAXException {
            String chars = new String(ch, start, length).trim();
            mString = mString.concat(chars);
    }

    public void endElement(String uri, String localName, String name)
                    throws SAXException {
            if (mString.length() > 0) {
                    if (localName.equalsIgnoreCase("name")) {
                            if (isPlacemark) {
                                    isRoute = mString.equalsIgnoreCase("Route");
                                    if (!isRoute) {
                                            mRoad.mPoints[mRoad.mPoints.length - 1].mName = mString;
                                    }
                            } else {
                                    mRoad.mName = mString;
                            }
                    } else if (localName.equalsIgnoreCase("color") && !isPlacemark) {
                            mRoad.mColor = Integer.parseInt(mString, 16);
                    } else if (localName.equalsIgnoreCase("width") && !isPlacemark) {
                            mRoad.mWidth = Integer.parseInt(mString);
                    } else if (localName.equalsIgnoreCase("description")) {
                            if (isPlacemark) {
                                    String description = cleanup(mString);
                                    if (!isRoute)
                                            mRoad.mPoints[mRoad.mPoints.length - 1].mDescription = description;
                                    else
                                            mRoad.mDescription = description;
                            }
                    } else if (localName.equalsIgnoreCase("href")) {
                            if (isItemIcon) {
                                    mRoad.mPoints[mRoad.mPoints.length - 1].mIconUrl = mString;
                            }
                    } else if (localName.equalsIgnoreCase("coordinates")) {
                            if (isPlacemark) {
                                    if (!isRoute) {
                                            String[] xyParsed = split(mString, ",");
                                            double lon = Double.parseDouble(xyParsed[0]);
                                            double lat = Double.parseDouble(xyParsed[1]);
                                            mRoad.mPoints[mRoad.mPoints.length - 1].mLatitude = lat;
                                            mRoad.mPoints[mRoad.mPoints.length - 1].mLongitude = lon;
                                    } else {
                                            String[] coodrinatesParsed = split(mString, " ");
                                            int lenNew = coodrinatesParsed.length;
                                            int lenOld = mRoad.mRoute.length;
                                            double[][] temp = new double[lenOld + lenNew][2];
                                            for (int i = 0; i < lenOld; i++) {
                                                    temp[i] = mRoad.mRoute[i];
                                            }
                                            for (int i = 0; i < lenNew; i++) {
                                                    String[] xyParsed = split(coodrinatesParsed[i], ",");
                                                    for (int j = 0; j < 2 && j < xyParsed.length; j++)
                                                            temp[lenOld + i][j] = Double
                                                                            .parseDouble(xyParsed[j]);
                                            }
                                            mRoad.mRoute = temp;
                                    }
                            }
                    }
            }
            mCurrentElement.pop();
            if (localName.equalsIgnoreCase("Placemark")) {
                    isPlacemark = false;
                    if (isRoute)
                            isRoute = false;
            } else if (localName.equalsIgnoreCase("ItemIcon")) {
                    if (isItemIcon)
                            isItemIcon = false;
            }
    }

    private String cleanup(String value) {
            String remove = "<br/>";
            int index = value.indexOf(remove);
            if (index != -1)
                    value = value.substring(0, index);
            remove = "&#160;";
            index = value.indexOf(remove);
            int len = remove.length();
            while (index != -1) {
                    value = value.substring(0, index).concat(
                                    value.substring(index + len, value.length()));
                    index = value.indexOf(remove);
            }
            return value;
    }

    public Point[] addPoint(Point[] points) {
            Point[] result = new Point[points.length + 1];
            for (int i = 0; i < points.length; i++)
                    result[i] = points[i];
            result[points.length] = new Point();
            return result;
    }

    private static String[] split(String strString, String strDelimiter) {
            String[] strArray;
            int iOccurrences = 0;
            int iIndexOfInnerString = 0;
            int iIndexOfDelimiter = 0;
            int iCounter = 0;
            if (strString == null) {
                    throw new IllegalArgumentException("Input string cannot be null.");
            }
            if (strDelimiter.length() <= 0 || strDelimiter == null) {
                    throw new IllegalArgumentException(
                                    "Delimeter cannot be null or empty.");
            }
            if (strString.startsWith(strDelimiter)) {
                    strString = strString.substring(strDelimiter.length());
            }
            if (!strString.endsWith(strDelimiter)) {
                    strString += strDelimiter;
            }
            while ((iIndexOfDelimiter = strString.indexOf(strDelimiter,
                            iIndexOfInnerString)) != -1) {
                    iOccurrences += 1;
                    iIndexOfInnerString = iIndexOfDelimiter + strDelimiter.length();
            }
            strArray = new String[iOccurrences];
            iIndexOfInnerString = 0;
            iIndexOfDelimiter = 0;
            while ((iIndexOfDelimiter = strString.indexOf(strDelimiter,
                            iIndexOfInnerString)) != -1) {
                    strArray[iCounter] = strString.substring(iIndexOfInnerString,
                                    iIndexOfDelimiter);
                    iIndexOfInnerString = iIndexOfDelimiter + strDelimiter.length();
                    iCounter += 1;
            }

            return strArray;
    }

} }

Tell me if you need any other code.. 告诉我是否需要其他代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM