正在做一个公交查询的应用,打算将选择的车次的所有站点都在地图上用图标标记,然后用直线连接,现在的问题是需要得到每个站点的GeoPoint就得知道每个站点的经纬度,我的公交数据都是取自公交查询网站的数据,但是Google的地名解析不一定每个站点都能解析,例如我作了个demo查询“长沙火车站”无法解析,但是用“长沙站”就可以解析得到经纬度,下面是我用的解析地址的类
public class ConvertUtil 
{
public static double[] getLocationInfo(String address)
{
//定义一个HttpClient,用于向指定地址发送请求
HttpClient client = new DefaultHttpClient();
//向指定地址发送Get请求
HttpGet httpGet = new HttpGet("http://maps.google."
+"com/maps/api/geocode/json?address="+address
+"ka&sensor=false");
StringBuilder sb = new StringBuilder();
try
{
HttpResponse response = client.execute(httpGet);
HttpEntity entity = response.getEntity();
InputStream stream = entity.getContent();
int b;
//循环读取服务器响应
while((b=stream.read())!=-1)
{
sb.append((char)b);
}
//将服务器返回的字符串转换成JSONObject对象
JSONObject jsonObject = new JSONObject(sb.toString());
//从JSONObject对象中取出代表位置的location属性
JSONObject location = jsonObject.getJSONArray("results").getJSONObject(0)
.getJSONObject("geometry").getJSONObject("location");
//获取经纬度信息
double longitude = location.getDouble("lng");
double latitude = location.getDouble("lat");
return new double[]{longitude,latitude};
}
catch(Exception e)
{
e.printStackTrace();
}
return null;
}

public static String getAddress(double longitude,double latitude)
{
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet("http://maps.google.com/maps/api/"
+"geocode/json?latlng="
+latitude+","+longitude
+"&sensor=false&region=cn");
StringBuilder sb = new StringBuilder();
try
{
HttpResponse response = client.execute(httpGet);
HttpEntity entity = response.getEntity();
InputStream stream = entity.getContent();
int b;
while((b=stream.read())!=-1)
{
sb.append((char) b);
}
JSONObject jsonObj = new JSONObject(sb.toString());
return jsonObj.getJSONArray("results").getJSONObject(0)
.getString("formatted_address");

}
catch(Exception e)
{
e.printStackTrace();
}
return null;
}
下面是我的显示地图的MapActivity
public class ShowMap extends MapActivity 
{
private MapView mapView;
private MapController controller;
private String busLine = "";
private List<Overlay> ol;
private Bitmap posBitmap;
private GeoPoint gp;
private double longitude = 113.05;//经度
private double latitude = 28.18; //纬度

@Override
protected void onCreate(Bundle icicle) 
{
super.onCreate(icicle);
setContentView(R.layout.browsemap);
mapView = (MapView)findViewById(R.id.mapView);
mapView.setBuiltInZoomControls(true);

controller = mapView.getController();
Bundle bundle = getIntent().getExtras();
posBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.pos);
busLine = bundle.getString("BusLine");
refreshMapView(busLine);
} @Override
protected boolean isRouteDisplayed() 
{
return false;
}

public void refreshMapView(String busline)
{
String[] results = new String[]{};
String address = "";

double[] positions = new double[]{};
results = busline.split(" - ");
ol = mapView.getOverlays();
ol.clear();
for(int i=0;i<results.length;i++)
{
address = results[i];
positions = ConvertUtil.getLocationInfo(address);
gp = new GeoPoint((int)(positions[1]*1E6),(int)(positions[0]*1E6));
ol.add(new PosOverLay(posBitmap,gp));
}
mapView.displayZoomControls(true);
controller.setZoom(11);
GeoPoint firstGP = new GeoPoint((int)(latitude*1E6),(int)(longitude*1E6));
controller.animateTo(firstGP);
}地图显示的是长沙市,例如旅1路的站点是长沙火车站 - 长岛路口 - 曙光路口 - 韭菜园 - 蔡锷中路口 - 华图教育(太平街口) - 市四医院 - 岳麓山北 - 二里半 - 湖南师大 - 岳麓山南 - 桃子湖 - 湖南大学 - 天马山东 - 科教新村,这些不是每个都能得到解析,求大侠指点啊,不会要自己把每个站点的经纬度解析了吧???

解决方案 »

  1.   

    没有基础数据就别加这个功能了geocoding不是万能的
      

  2.   

    我做公交路线都有每个站点的GPS坐标数据和每个公交路线的数据的
      

  3.   

    这个有个好的办法就是
    1、 必须得有公路信息(如果站点之间的连线只是直线就不需要了)
    2、必须得知道每个站点的经纬度
    3、直接用Graphic把所有的站点绘制出来,然后点之间两两连接就可以
      

  4.   

    多谢指点,我之前是把每个站点名称发送到服务器去解析,但是发现这样做很不现实,还是老老实实把每个站点的经纬度坐标准备好,不知道大侠们觉得是Google Map好还是百度地图API更好呢?
      

  5.   

    Google 快要被gcd日了。你就在国内的api里挑吧
    mapabc,baidu,sogou,tencent 什么的
      

  6.   

    问下楼主,每个站点的经纬度还是需要通过解析才能得到,如果不这样,楼主是怎样得到的呢???
    还有就是,用google map的时候,偏差很大,不知楼主有没有碰到了