【问题标题】:How can I get Lat/Lng of two points (train station) on google maps?如何在谷歌地图上获得两点(火车站)的纬度/纬度?
【发布时间】:2012-11-14 19:38:50
【问题描述】:

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

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

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

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

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

【问题讨论】:

    标签: google-maps


    【解决方案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-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多