How to get Latitude/Longitude from JSON responsed by google map apis

时光总嘲笑我的痴心妄想 提交于 2019-12-24 00:11:11

问题


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


回答1:


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

}



回答2:


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.



来源:https://stackoverflow.com/questions/8983164/how-to-get-latitude-longitude-from-json-responsed-by-google-map-apis

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!