3

hi i have created app with integration of google map where i need to draw the path depends upon the driving movement. so here i can able to get the array of points which having current lat and lang at every 2 seconds now i want to draw the path by using of those lat and lang array of points and i need to put the marker at the source and destination point not in the intermediate points.

How can i do this, i have searched lot of time but still doesn't get solution what i expect so please tell me if u got any idea or solution to do.

UPATED MY CODE

i got the solution by using of this code

private void drawPath() {
    PolylineOptions polylineOptions = new PolylineOptions();

    int i = 0;
    if (langtitudearray != null && latitudearray != null) {
        for (i = 0; i < latitudearray.size(); i = i + 1) {
            polylineOptions.add(new LatLng(latitudearray.get(i),
                    langtitudearray.get(i)));

        }
        addMarkers(
                new LatLng(latitudearray.get(0), langtitudearray.get(0)),
                new LatLng(latitudearray.get(latitudearray.size() - 1), langtitudearray.get(latitudearray.size() - 1)));

    }
    polylineOptions.color(Color.BLUE);
    polylineOptions.width(6);
    polylineOptions.geodesic(false);
    map.addPolyline(polylineOptions);

}

just i could use the lat and lang array of points and add the markers as well as. Now the problem is. if i got more points of lat and lang in array, there will take a time to load on the map which makes app is hanging if even i could get those array of points from the service by sending broadcast at 30 sec once.

Note : i am getting those array of points from the API response which is running in background service and making lat and lang array then sending by broadcast to the activity.

How to resolve this issues and what is the better approach to do? Thanks in Advance.

siva
  • 375
  • 6
  • 24

1 Answers1

0

Use this: Call makeURL() method and then drawPath() method

public String makeURL (double sourcelat, double sourcelog, double destlat, double destlog ){
    StringBuilder urlString = new StringBuilder();
    urlString.append("http://maps.googleapis.com/maps/api/directions/json");
    urlString.append("?origin=");// from
    urlString.append(Double.toString(sourcelat));
    urlString.append(",");
    urlString
            .append(Double.toString( sourcelog));
    urlString.append("&destination=");// to
    urlString
            .append(Double.toString( destlat));
    urlString.append(",");
    urlString.append(Double.toString( destlog));
    urlString.append("&sensor=false&mode=driving&alternatives=true");
    return urlString.toString();

}

public void drawPath(String  result) {

    try {
            //Tranform the string into a json object
           final JSONObject json = new JSONObject(result);
           JSONArray routeArray = json.getJSONArray("routes");
           JSONObject routes = routeArray.getJSONObject(0);
           JSONObject overviewPolylines = routes.getJSONObject("overview_polyline");
           String encodedString = overviewPolylines.getString("points");
           List<LatLng> list = decodePoly(encodedString);

           for(int z = 0; z<list.size()-1;z++){
                LatLng src= list.get(z);
                LatLng dest= list.get(z+1);
                Polyline line = googleMap.addPolyline(new PolylineOptions()
                .add(new LatLng(src.latitude, src.longitude), new LatLng(dest.latitude,   dest.longitude))
                .width(5)
                .color(Color.BLUE).geodesic(true));
            }

    } 
    catch (JSONException e) {

    }
} 

private List<LatLng> decodePoly(String encoded) {

    List<LatLng> poly = new ArrayList<LatLng>();
    int index = 0, len = encoded.length();
    int lat = 0, lng = 0;

    while (index < len) {
        int b, shift = 0, result = 0;
        do {
            b = encoded.charAt(index++) - 63;
            result |= (b & 0x1f) << shift;
            shift += 5;
        } while (b >= 0x20);
        int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
        lat += dlat;

        shift = 0;
        result = 0;
        do {
            b = encoded.charAt(index++) - 63;
            result |= (b & 0x1f) << shift;
            shift += 5;
        } while (b >= 0x20);
        int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
        lng += dlng;

        LatLng p = new LatLng( (((double) lat / 1E5)),
                 (((double) lng / 1E5) ));
        poly.add(p);
    }

    return poly;
}
Rahul Sharma
  • 5,949
  • 5
  • 36
  • 46
  • Thanks for your reply, your code only for draw the line between source and destination but i have array of points(Lat, lang) and need to draw the line using of those points. so can u pls give me solution for that? – siva Mar 20 '15 at 13:56
  • i am just getting those array points from the json Array in Json Response, am getting like {"VechicleLocFinder":[{"VechicleLocFinderID":6,"VechicleID":"351540060876166","TripDate":"Mar 19 2015 12:00AM","TripTime":"20:17:34","CurrentLat":"13.0455132","CurrentLang":"80.2221575","CreatedOn_M":null,"CreatedOn":"2015-03-19T10:19:31.96"},{"VechicleLocFinderID":7,"VechicleID":"351540060876166","TripDate":"Mar 19 2015 12:00AM","TripTime":"20:29:32","CurrentLat":"13.0455097","CurrentLang":"80.2221559","CreatedOn_M":"03-19-2015 20:29:32","CreatedOn":"2015-03-19T10:27:11.243"}]} – siva Mar 21 '15 at 06:18
  • here i going to draw path by using of currentLat and currentLang.. how can i do that? – siva Mar 21 '15 at 06:21