如何在谷歌地图上获得两点(火车站)的纬度/经度? [英] How can I get Lat/Lng of two points (train station) on google maps?

查看:118
本文介绍了如何在谷歌地图上获得两点(火车站)的纬度/经度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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

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

code>

我需要Ceres(火车站)和Stazione Porta Nuova的Lat / Lng,Corso Vittorio Emanuele II,58,都灵(火车站)。如何获得它们?

因此,我可以在我的地图中绘制相同的铁路线(棕线)。

解决方案

  public class GetXMLTask 
{
static double longitute;
静态双纬度;
public JSONObject getLocationInfo(String address)
{
StringBuilder stringBuilder = new StringBuilder();
尝试
{
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响应;
stringBuilder = new StringBuilder();
response = client.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream stream = entity.getContent();
int b; ((b = stream.read())!= -1)


)stringBuilder.append((char)b);

$ b $ catch(ClientProtocolException e)
{
Log.i(getLocationInfo ClientProtocolException,e.toString());

catch(IOException e)
{
Log.i(getLocationInfo IOException,e.toString());
}
JSONObject jsonObject = new JSONObject();
尝试
{
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());
}
返回latlng;



$ b现在我们可以在我们的类中调用它
$ b $ latitude = new GetXMLTask()。getLatLong(new GetXMLTask ().getLocationInfo(地址))得到(0);
longitude = new GetXMLTask()。getLatLong(new GetXMLTask()。getLocationInfo(address))。get(1);

我可能没有超级优化的代码,但这对我来说工作得很好,给它一个尝试,只是通过您的调用类中的地址


I'm about to trace with direction service the railway between two station. The problem is I can't get the Lat/Lng of the two station, it get always a neighbour points.

The route is (search on google maps website) :

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

I need Lat/Lng of Ceres (train station) and Stazione Porta Nuova, Corso Vittorio Emanuele II, 58, Torino (train station). How can I get them?

So I can draw in my maps the same railway (brown line).

解决方案

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

I may not have the super optimised code , but this is working pretty fine for me , give it a try , just passed the address in your calling class

这篇关于如何在谷歌地图上获得两点(火车站)的纬度/经度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆