简体   繁体   中英

Driving paths on google maps

I am trying to draw paths on google maps using android i found this code online but i get some errors i can't fixed it.It created class for points and class for RoadProvider but i get confused between the built in class points and points he has created it.Here it is the errors

 Description    Resource    Path    Location    Type
mDescription cannot be resolved or is not a field   RoadProvider.java   /sbn/src/sbn/project/gp line 103    Java Problem
mIconUrl cannot be resolved or is not a field   RoadProvider.java   /sbn/src/sbn/project/gp line 109    Java Problem
mLatitude cannot be resolved or is not a field  RoadProvider.java   /sbn/src/sbn/project/gp line 117    Java Problem
mLongitude cannot be resolved or is not a field RoadProvider.java   /sbn/src/sbn/project/gp line 118    Java Problem
mName cannot be resolved or is not a field  RoadProvider.java   /sbn/src/sbn/project/gp line 90 Java Problem
The method addPoint(Point[]) in the type KMLHandler is not applicable for the arguments (Point[])   RoadProvider.java   /sbn/src/sbn/project/gp line 68 Java Problem
The method addPoint(Point[]) in the type KMLHandler is not applicable for the arguments (Point[])   RoadProvider.java   /sbn/src/sbn/project/gp line 69 Java Problem

ana here it is the code

package sbn.project.gp;

public class Point {

    String mName;
    String mDescription;
    String mIconUrl;
    double mLatitude;
    double mLongitude;
}

And this the RoadProvider class

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) {// 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");
            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);//there is an error here
                    mRoad.mPoints = addPoint(mRoad.mPoints);//there is an error here
            } 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; //there is an error here  
                                    }
                            } 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;//there is an error here 
                                    else
                                            mRoad.mDescription = description;//there is an error here 
                            }
                    } 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;
            }


        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;
    }

Refer this link to draw driving path in your application. You just need to add the four classes present in the link in your project and call the below lines when you need to display the route.

SharedData data = SharedData.getInstance();
data.setAPIKEY("0RUTLH7cqd6yrZ0FdS0NfQMO3lioiCbnH-BpNQQ");//set your map key here
data.setSrc_lat(17);//set your src lat
data.setSrc_lng(78);//set your src lng
data.setDest_lat(18);//set your dest lat
data.setDest_lng(77);//set your dest lng
startActivity(new Intent(YourActivity.this,RoutePath.class));//add RoutePath in your manifeast file

//Also add the permission to your manifeast file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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