我即将跟踪两站之间的铁路方向服务。 问题是我无法获得两个站的Lat / Lng,它总是得到一个邻居点。
路线是(在谷歌地图网站上搜索):
Ceres, TO to Stazione Porta Nuova, Corso Vittorio Emanuele II, 58, Torino
我需要Lat / Lng的Ceres(火车站)和Stazione Porta Nuova,Corso Vittorio Emanuele II,58,都灵(火车站)。我怎么能得到它们?
所以我可以在我的地图中绘制相同的铁路(棕色线)。
答案 0 :(得分: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);
我可能没有超级优化的代码,但这对我来说非常好,试一试,只是在你的调用类中传递了地址