1

2駅間の鉄道を案内サービスでたどろうとしています。問題は、2 つのステーションの緯度/経度を取得できないことです。常に近隣ポイントを取得します。

ルートは(グーグルマップのウェブサイトで検索):

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 に答える