2

I am not able to properly extract out the latitude and longitude point set to draw route further. Can anybody get me the code to do so?

Thanks in advance

Jav_Rock
  • 22,059
  • 20
  • 123
  • 164
Jai Pandit
  • 510
  • 1
  • 6
  • 18

2 Answers2

3
public class MapprojectActivity extends MapActivity {
    /** Called when the activity is first created. */
    static final String TAG_RESULTS = "results";
    static final String TAG_GEO = "geometry";
    static final String TAG_LOCATION = "location";
    static final String TAG_LAT = "lat";
    static final String TAG_LNG = "lng";
    JSONArray res = null;
    MapView mapView;
    List<Overlay> mapOverlays;
    Drawable drawable;
    MyItemizedOverlay itemizedOverlay;
    static String url = "http://maps.google.com/maps/api/geocode/json?address=guindy,+chennai,+IN&sensor=false";

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.map);
        TextView lattv=(TextView)findViewById(R.id.lat);
        TextView lngtv=(TextView)findViewById(R.id.lng);
        JSONParstring jParser = new JSONParstring();

        // getting JSON string from URL

        try
        {
            JSONObject jobj = new JSONObject(json);

            res = jobj.getJSONArray(TAG_RESULTS);
            for(int i = 0; i < res.length(); i++){
                JSONObject c = res.getJSONObject(i);

                JSONObject loc = c.optJSONObject(TAG_GEO).optJSONObject(TAG_LOCATION);

            String lat =loc.getString(TAG_LAT);

            String lng = loc.getString(TAG_LNG);

            lattv.setText(lat);
            lngtv.setText(lng);

            }
        }
        catch (JSONException e){ }
String i=(String) lattv.getText();
String j=(String) lngtv.getText();

double lat1 = Double.parseDouble(i);  
double lng1 = Double.parseDouble(j); 


        mapView = (MapView) findViewById(R.id.map_view);
        mapView.setBuiltInZoomControls(false);

        mapOverlays = mapView.getOverlays();
    drawable = getResources().getDrawable(R.drawable.mark1);
    itemizedOverlay = new MyItemizedOverlay(drawable, mapView);

    GeoPoint point = new GeoPoint((int)(lat1*1E6),(int)(lng1*1E6));
    OverlayItem overlayItem = new OverlayItem(point, "Amy Jones", 
            "(checked in Lemon-tree with friends lisa wong, paul jones)");
    itemizedOverlay.addOverlay(overlayItem);

    mapOverlays.add(itemizedOverlay);

    final MapController mc = mapView.getController();
    mc.animateTo(point);
    mc.setZoom(16);
//      Integer i = Integer.valueOf((String) lattv.getText());
//      Integer j = Integer.valueOf((String) lngtv.getText());
//      // first overlay


    }

    protected boolean isRouteDisplayed() {
        return false;
    }

}
kpndroid
  • 31
  • 3
1

Here is to get Latitude/Longitude from JSON http://blog.synyx.de/2010/06/routing-driving-directions-on-android-part-1-get-the-route/

And this is how to draw a route http://blog.synyx.de/2010/06/routing-driving-directions-on-android-%E2%80%93-part-2-draw-the-route/

u should study it. i hope it'll help.

April Smith
  • 1,810
  • 2
  • 28
  • 52
  • thankyou for the link, now i am able to draw the path on my mobile device, but the think is what i want to draw a exact road path, for that i am taking in account POLYLINE, decoding the encoded code line, so doingso i can draw a beautiful exact route. But the problem is what after joining the complete path my phone application slows down. It take more then 20 seconds to draw the path and once the path is draws, it becom very heavy for the mobile to redraw it all if touch the mobile scree and navigate he map. – Jai Pandit Jan 30 '12 at 11:27
  • so, what points should i actually take in account to draw the path, i mean what should be the culture to do it all. – Jai Pandit Jan 30 '12 at 11:27
  • presently, i collect all the geopoints in a arraylist, Then for every geo point i make a overlayItem of a ItemizedOverlay class, then in the draw() of that class i iterate over the arraylist of geopoints and applied logic to extract two points and draw and line between them and so on – Jai Pandit Jan 30 '12 at 11:29
  • before doing all this this, i was only taking the "start-point" and "end-position" of the steps which was returned form the API as json – Jai Pandit Jan 30 '12 at 11:30
  • the google directions api that i am using is http://maps.googleapis.com/maps/api/directions/json?origin="lat/lng of origion",&destination="lat/lng of destination,&sensor=false&mode=driving – Jai Pandit Jan 30 '12 at 11:42
  • so how should i optimize my code then , so that i shows the path with efficiency, coz with 200-300 geopoints, the mobile is dying – Jai Pandit Jan 30 '12 at 11:43