1

我即将用方向服务追踪两个车站之间的铁路。问题是我不能得到两个站的纬度/经度,它总是得到一个邻居点。

路线是(在谷歌地图网站上搜索):

Ceres, TO to Stazione Porta Nuova, Corso Vittorio Emanuele II, 58, Torino

我需要 Ceres(火车站)和 Stazione Porta Nuova, Corso Vittorio Emanuele II, 58, Torino(火车站)的纬度/纬度。我怎样才能得到它们?

所以我可以在我的地图上画出同一条铁路(棕色线)。

4

1 回答 1

1
public class GetXMLTask
{
    static double longitute;
    static double latitude;
    public JSONObject getLocationInfo(String address)
    {
        StringBuilder stringBuilder = new StringBuilder();
        try 
        {
            address = address.replaceAll(" ","%20");    
            HttpPost httppost = new HttpPost("http://maps.google.com/maps/api/geocode/json?address=" + address + "&sensor=false");
            HttpClient client = new DefaultHttpClient();
            HttpResponse response;
            stringBuilder = new StringBuilder();
            response = client.execute(httppost);
            HttpEntity entity = response.getEntity();
            InputStream stream = entity.getContent();
            int b;
            while ((b = stream.read()) != -1) 
            {
                stringBuilder.append((char) b);
            }
        } 
        catch (ClientProtocolException e) 
        {
            Log.i("getLocationInfo ClientProtocolException", e.toString());
        } 
        catch (IOException e) 
        {
            Log.i("getLocationInfo IOException", e.toString());
        }
        JSONObject jsonObject = new JSONObject();
        try 
        {
            jsonObject = new JSONObject(stringBuilder.toString());
        } 
        catch (JSONException e) 
        {
            Log.i("getLocationInfo JSONException", e.toString());
        }
        return jsonObject;
    }

    public ArrayList<Double> getLatLong(JSONObject jsonObject) 
    {
        ArrayList<Double> latlng = new ArrayList<Double>();

        try 
        {
            longitute = ((JSONArray)jsonObject.get("results")).getJSONObject(0).getJSONObject("geometry").getJSONObject("location").getDouble("lng");
            latitude = ((JSONArray)jsonObject.get("results")).getJSONObject(0).getJSONObject("geometry").getJSONObject("location").getDouble("lat");
            latlng.add(latitude);
            latlng.add(longitute);
        } 
        catch (JSONException e) 
        {
            longitute = 0;
            latitude = 0;
            Log.i("getLatLong", e.toString());
        }
        return latlng;
    }
}


now we can call it inside our class by

latitude = new GetXMLTask().getLatLong(new GetXMLTask().getLocationInfo(address)).get(0);
longitude = new GetXMLTask().getLatLong(new GetXMLTask().getLocationInfo(address)).get(1);

我可能没有超级优化的代码,但这对我来说工作得很好,试一试,只是在你的调用类中传递了地址

于 2012-11-14T11:38:50.323 回答