Android的转换CID位置坐标 [英] Android convert CID location to Coordinates
问题描述
我建立了一个Android应用程序,它可以处理来自谷歌地图份额的意图,并显示它的坐标。
I built an android app which can handle a share intent from Google Maps and show it's coordinates.
现在的问题是,他们发送的短网址,我去code与谷歌的网址shortner API,并在某些情况下,结果长的链接是这种类型的:<一href="http://maps.google.com/?cid=3635533832900933072&hl=en&gl=us">http://maps.google.com/?cid=3635533832900933072&hl=en&gl=us.
The problem is that they send a short url which I decode with Google's url shortner api and in some cases, the result long link is of this type: http://maps.google.com/?cid=3635533832900933072&hl=en&gl=us.
谁能帮助我如何获得coresponding坐标的CID = 3635533832900933072的
Can anyone help me on how to get the coresponding coordinates to "cid=3635533832900933072"
推荐答案
据我所知,没有公开的API从CID获得的位置。
As far as I know there is no public API to get the location from a cid.
但是,一种可能的解决方法是解析谷歌地图输出获得的纬度和经度(尽管它可能是脆性的,如果他们改变的结果格式)。
However, a possible workaround would be to parse the Google Maps output to obtain the latitude and longitude (though it may be brittle, if they change the result format).
(虽然URL包含输出= JSON
,它不是真正的JSON - 这就是为什么我分析它与子()
键,这样,而不是使用的JSONObject
)。的
(Although the url contains output=json
, it's not actually json -- that's why I parse it with substring()
and such instead of using JSONObject
).
试试这个code:
public static LatLng getCidCoordinates(String cid)
{
final String URL_FORMAT = "http://maps.google.com/maps?cid=%s&q=a&output=json";
final String LATLNG_BEFORE = "viewport:{center:{";
final String LATLNG_AFTER = "}";
final String LATLNG_SEPARATOR = ",";
final String LAT_PREFIX = "lat:";
final String LNG_PREFIX = "lng:";
try
{
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(String.format(URL_FORMAT, cid));
HttpResponse response = client.execute(get);
String text = EntityUtils.toString(response.getEntity(), "UTF-8");
int startIndex = text.indexOf(LATLNG_BEFORE);
if (startIndex == -1)
return null;
startIndex += LATLNG_BEFORE.length();
int endIndex = text.indexOf(LATLNG_AFTER, startIndex);
// Should be "lat:<number>,lng:<number>"
String[] parts = text.substring(startIndex, endIndex).split(LATLNG_SEPARATOR);
if (parts.length != 2)
return null;
if (parts[0].startsWith(LAT_PREFIX))
parts[0] = parts[0].substring(LAT_PREFIX.length());
else
return null;
if (parts[1].startsWith(LNG_PREFIX))
parts[1] = parts[1].substring(LNG_PREFIX.length());
else
return null;
return new LatLng(Double.parseDouble(parts[0]), Double.parseDouble(parts[1]));
}
catch (NumberFormatException e)
{
e.printStackTrace();
return null;
}
catch (IOException e)
{
e.printStackTrace();
return null;
}
}
这篇关于Android的转换CID位置坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!