求助!!将公交线路上的所有站点在地图中标记出来的问题
正在做一个公交查询的应用,打算将选择的车次的所有站点都在地图上用图标标记,然后用直线连接,现在的问题是需要得到每个站点的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®ion=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; }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); }